From 8a628bf0e340a0f52fd47d82a8394b6c30de2125 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Mon, 2 Oct 2023 20:17:20 +0200 Subject: [PATCH 1/2] Take disabled status of cache into account when determining CacheActions. --- aiohttp_client_cache/backends/base.py | 1 + aiohttp_client_cache/cache_control.py | 12 ++++++++---- aiohttp_client_cache/session.py | 5 ++++- test/integration/test_filesystem.py | 19 ++++++++++++++++++- 4 files changed, 31 insertions(+), 6 deletions(-) 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/test_filesystem.py b/test/integration/test_filesystem.py index dab975b..a4cd26f 100644 --- a/test/integration/test_filesystem.py +++ b/test/integration/test_filesystem.py @@ -9,7 +9,7 @@ from aiohttp_client_cache.backends.base import BaseCache from aiohttp_client_cache.backends.filesystem import FileBackend, FileCache from aiohttp_client_cache.session import CachedSession -from test.conftest import CACHE_NAME +from test.conftest import CACHE_NAME, httpbin from test.integration import BaseBackendTest, BaseStorageTest @@ -56,6 +56,23 @@ class TestFileBackend(BaseBackendTest): async def test_gather(self): super().test_gather() + async def test_disabled(self): + 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 + + response = await session.request("GET", httpbin('cache/0')) + + assert response.from_cache is True + 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 + async def test_redirect_cache_path(self): async with self.init_session() as session: assert isinstance(session, CachedSession) From 008428d39aa930c7213630fc74f6639dbd6d81ac Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Mon, 2 Oct 2023 20:28:20 +0200 Subject: [PATCH 2/2] Move disabled test to base class. --- test/integration/base_backend_test.py | 25 +++++++++++++++++++++++++ test/integration/test_filesystem.py | 19 +------------------ 2 files changed, 26 insertions(+), 18 deletions(-) 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 diff --git a/test/integration/test_filesystem.py b/test/integration/test_filesystem.py index a4cd26f..dab975b 100644 --- a/test/integration/test_filesystem.py +++ b/test/integration/test_filesystem.py @@ -9,7 +9,7 @@ from aiohttp_client_cache.backends.base import BaseCache from aiohttp_client_cache.backends.filesystem import FileBackend, FileCache from aiohttp_client_cache.session import CachedSession -from test.conftest import CACHE_NAME, httpbin +from test.conftest import CACHE_NAME from test.integration import BaseBackendTest, BaseStorageTest @@ -56,23 +56,6 @@ class TestFileBackend(BaseBackendTest): async def test_gather(self): super().test_gather() - async def test_disabled(self): - 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 - - response = await session.request("GET", httpbin('cache/0')) - - assert response.from_cache is True - 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 - async def test_redirect_cache_path(self): async with self.init_session() as session: assert isinstance(session, CachedSession)