Skip to content

Commit

Permalink
Add some more logging statements
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Feb 25, 2021
1 parent 2bdac93 commit b859975
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
28 changes: 19 additions & 9 deletions aiohttp_client_cache/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ def is_cacheable(self, response: Union[ClientResponse, CachedResponse, None]) ->
"""Perform all checks needed to determine if the given response should be cached"""
if not response:
return False
return all(
[
not self.disabled,
response.status in self.allowed_codes,
response.method in self.allowed_methods,
not self.is_expired(response),
self.filter_fn(response),
]
)
cache_criteria = [
not self.disabled,
response.status in self.allowed_codes,
response.method in self.allowed_methods,
not self.is_expired(response),
self.filter_fn(response),
]
logger.debug(f'is_cacheable checks for response from {response.url}: {cache_criteria}') # type: ignore
return all(cache_criteria)

def is_expired(self, response: AnyResponse) -> bool:
"""Determine if a given response is expired"""
Expand All @@ -105,17 +105,23 @@ async def get_response(self, key: str) -> Optional[CachedResponse]:
key: key of resource
"""
# Attempt to fetch response from the cache
logger.debug(f'Attempting to get cached response for key: {key}')
try:
if not await self.responses.contains(key):
key = str(await self.redirects.read(key))
response = await self.responses.read(key)
except (KeyError, TypeError):
logger.debug('No cached response found')
return None
if not isinstance(response, CachedResponse):
logger.debug('Cached response is invalid')
return None

logger.info(f'Cached response found for key: {key}')

# If the item is expired or filtered out, delete it from the cache
if not self.is_cacheable(response):
logger.info('Cached response expired; deleting')
await self.delete(key)
response.is_expired = True

Expand All @@ -130,6 +136,7 @@ async def save_response(self, key: str, response: ClientResponse):
"""
if not self.is_cacheable(response):
return
logger.info(f'Saving response for key: {key}')

cached_response = await CachedResponse.from_client_response(response)
await self.responses.write(key, cached_response)
Expand All @@ -140,6 +147,7 @@ async def save_response(self, key: str, response: ClientResponse):

async def clear(self):
"""Clear cache"""
logger.info('Clearing cache')
await self.responses.clear()
await self.redirects.clear()

Expand All @@ -152,6 +160,7 @@ async def delete_history(response):
for r in response.history:
await self.redirects.delete(self.create_key(r.method, r.url))

logger.debug(f'Deleting cached responses for key: {key}')
redirect_key = str(await self.redirects.pop(key))
await delete_history(await self.responses.pop(key))
await delete_history(await self.responses.pop(redirect_key))
Expand All @@ -167,6 +176,7 @@ async def delete_expired_responses(self):
**Note:** Also deletes any cache items that are filtered out according to ``filter_fn()``
and filter parameters (``allowable_*``)
"""
logger.info(f'Deleting all responses more than {self.expire_after} hours old')
keys_to_delete = set()

for key in await self.responses.keys():
Expand Down
4 changes: 4 additions & 0 deletions aiohttp_client_cache/session.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Core functions for cache configuration"""
import warnings
from contextlib import asynccontextmanager
from logging import getLogger

from aiohttp import ClientSession
from aiohttp.typedefs import StrOrURL
Expand All @@ -9,6 +10,8 @@
from aiohttp_client_cache.forge_utils import extend_signature, forge
from aiohttp_client_cache.response import AnyResponse

logger = getLogger(__name__)


class CacheMixin:
"""A mixin class for :py:class:`aiohttp.ClientSession` that adds caching support
Expand All @@ -33,6 +36,7 @@ async def _request(self, method: str, str_or_url: StrOrURL, **kwargs) -> AnyResp
if cached_response and not getattr(cached_response, 'is_expired', False):
return cached_response
else:
logger.info(f'Cached response not found; making request to {str_or_url}')
new_response = await super()._request(method, str_or_url, **kwargs) # type: ignore
await new_response.read()
await self.cache.save_response(cache_key, new_response)
Expand Down
5 changes: 5 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from logging import basicConfig, getLogger

# Configure logging for pytest session
basicConfig(level='INFO')
getLogger('aiohttp_client_cache').setLevel('DEBUG')

0 comments on commit b859975

Please sign in to comment.