Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CachedSession does not correctly bypass the cache when caching is disabled via session.disabled() #182

Closed
netomi opened this issue Oct 2, 2023 · 0 comments · Fixed by #183
Labels
bug Something isn't working
Milestone

Comments

@netomi
Copy link
Contributor

netomi commented Oct 2, 2023

The problem

When disabling the cache, the cache backend is still being updated when making requests.

The problem is in the CacheBackend#request method, that creates actions based on the request itself.
This does not take into account whether the cache is disabled, and thus skip_read and skip_write are left at false.

Subsequently, the request is retrieved from the cache backend, but the get_response() method checks whether the response is cacheable, which takes the disabled state of the cache into account and deletes as a consequence the response from the cache.

Finally, the request is made to the real remote resulting in the expected behavior, but unfortunately, the cache has been modified in the process and unnecessary calls have been made.

Expected behavior

when session.disabled() is being used, the cache is completely disabled, both for reading requests as for writing responses.

Steps to reproduce the behavior

Simple testcase:

async with self.init_session() as session:
    response = await session.request("GET", httpbin('cache/0'))

    assert response.from_cache is False
    assert await session.cache.responses.size() == 1

    async with session.disabled():
        response = await session.request("GET", httpbin('cache/0'))
        assert response.from_cache is False
        assert await session.cache.responses.size() == 1

Workarounds

Create a new session instead.

Environment

  • aiohttp-client-cache version: 0.9.1
  • Python version: 3.10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants