diff --git a/aiohttp_client_cache/backends/base.py b/aiohttp_client_cache/backends/base.py index 066897c..6fa189c 100644 --- a/aiohttp_client_cache/backends/base.py +++ b/aiohttp_client_cache/backends/base.py @@ -127,6 +127,7 @@ async def request( session_expire_after=self.expire_after, urls_expire_after=self.urls_expire_after, cache_control=self.cache_control, + cache_disabled=self.disabled, **kwargs, ) diff --git a/aiohttp_client_cache/cache_control.py b/aiohttp_client_cache/cache_control.py index fd864e9..d1b7ff3 100644 --- a/aiohttp_client_cache/cache_control.py +++ b/aiohttp_client_cache/cache_control.py @@ -58,15 +58,19 @@ def from_request( cls, key: str, cache_control: bool = False, + cache_disabled: bool = False, headers: Optional[Mapping] = None, **kwargs, ): """Initialize from request info and CacheBackend settings""" - headers = headers or {} - if cache_control and has_cache_headers(headers): - return cls.from_headers(key, headers) + if cache_disabled: + return cls(key=key, skip_read=True, skip_write=True, revalidate=True) else: - return cls.from_settings(key, cache_control=cache_control, **kwargs) + headers = headers or {} + if cache_control and has_cache_headers(headers): + return cls.from_headers(key, headers) + else: + return cls.from_settings(key, cache_control=cache_control, **kwargs) @classmethod def from_headers(cls, key: str, headers: Mapping): diff --git a/aiohttp_client_cache/session.py b/aiohttp_client_cache/session.py index ef81c77..41dcf0c 100644 --- a/aiohttp_client_cache/session.py +++ b/aiohttp_client_cache/session.py @@ -55,7 +55,10 @@ async def _request( return response # If the response was missing or expired, send and cache a new request else: - logger.debug(f'Cached response not found; making request to {str_or_url}') + if actions.skip_read: + logger.debug(f'Reading from cache was skipped; making request to {str_or_url}') + else: + logger.debug(f'Cached response not found; making request to {str_or_url}') new_response = await super()._request(method, str_or_url, **kwargs) # type: ignore actions.update_from_response(new_response) if await self.cache.is_cacheable(new_response, actions): diff --git a/test/integration/base_backend_test.py b/test/integration/base_backend_test.py index 41cfd5f..ecb5657 100644 --- a/test/integration/base_backend_test.py +++ b/test/integration/base_backend_test.py @@ -283,3 +283,28 @@ async def test_serializer__itsdangerous(self): session.cache.responses._serializer.secret_keys = ['a different key'] with pytest.raises(BadSignature): await session.cache.responses.read('key') + + async def test_disabled(self): + """With a disabled CachedSession, responses should not come from the cache + and the cache should not be modified + """ + async with self.init_session() as session: + # first request shall populate the cache + response = await session.request("GET", httpbin('cache/0')) + + assert response.from_cache is False + assert await session.cache.responses.size() == 1 + + # second request shall come from the cache + response = await session.request("GET", httpbin('cache/0')) + + assert response.from_cache is True + assert await session.cache.responses.size() == 1 + + # now disable the cache, the response should not come from the cache + # but the cache should be unmodified afterward. + 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