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

Take disabled status of CachedSession for CacheActions #183

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aiohttp_client_cache/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
12 changes: 8 additions & 4 deletions aiohttp_client_cache/cache_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion aiohttp_client_cache/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
25 changes: 25 additions & 0 deletions test/integration/base_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading