summaryrefslogtreecommitdiff
path: root/requests_cache/backends/base.py
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2021-03-29 22:26:46 -0500
committerJordan Cook <jordan.cook@pioneer.com>2021-03-30 14:35:30 -0500
commit163271150f49830f498d7966ac7ba2df4c2cce83 (patch)
tree856e8712d635365e86da3d56e2b866c0aba7d95d /requests_cache/backends/base.py
parent871f41c222da19a04c89691e64ec9f177bf13339 (diff)
downloadrequests-cache-163271150f49830f498d7966ac7ba2df4c2cce83.tar.gz
Add logging to main cache operations
Diffstat (limited to 'requests_cache/backends/base.py')
-rw-r--r--requests_cache/backends/base.py31
1 files changed, 23 insertions, 8 deletions
diff --git a/requests_cache/backends/base.py b/requests_cache/backends/base.py
index 26b5775..923e6f2 100644
--- a/requests_cache/backends/base.py
+++ b/requests_cache/backends/base.py
@@ -1,12 +1,12 @@
import hashlib
import json
import pickle
+import warnings
from abc import ABC
from collections.abc import MutableMapping
from logging import getLogger
from typing import Iterable, List, Union
from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse
-from warnings import warn
import requests
@@ -85,7 +85,7 @@ class BaseCache:
del self.redirects[key]
for r in response.history:
del self.redirects[self.create_key(r.request)]
- except KeyError:
+ except (AttributeError, KeyError):
pass
def delete_url(self, url: str):
@@ -96,6 +96,7 @@ class BaseCache:
def clear(self):
"""Delete all items from the cache"""
+ logger.info('Clearing all items from the cache')
self.responses.clear()
self.redirects.clear()
@@ -105,7 +106,19 @@ class BaseCache:
Args:
expire_after: A new expiration time used to revalidate the cache
"""
- for key, response in list(self.responses.items()):
+ logger.info(
+ 'Removing expired responses.'
+ + (f'Revalidating with: {expire_after}' if expire_after else '')
+ )
+ for key in list(self.responses.keys()):
+ # If a response is invalid, delete it
+ try:
+ response = self.responses[key]
+ except Exception as e:
+ logger.info(f'Unable to deserialize response with key {key}: {str(e)}')
+ self.delete(key)
+ continue
+
# If we're revalidating and it's not yet expired, update the cached item's expiration
if expire_after is not None and not response.revalidate(expire_after):
self.responses[key] = response
@@ -194,11 +207,13 @@ class BaseStorage(MutableMapping, ABC):
**kwargs,
):
self._serializer = serializer or self._get_serializer(secret_key, salt)
- if not secret_key and not suppress_warnings:
- warn(
- 'Using a secret_key to sign cached items is recommended for this backend.\n'
- 'Use suppress_warnings=True to ignore this message.'
- )
+ logger.info(f'Initializing {type(self).__name__} with serializer: {type(self._serializer)}')
+
+ if kwargs:
+ logger.warning(f'Unrecognized keyword arguments: {kwargs}')
+ if not secret_key:
+ warn_func = logger.info if suppress_warnings else warnings.warn
+ warn_func('Using a secret key to sign cached items is recommended for this backend')
def serialize(self, item: Union[CachedResponse, str]) -> bytes:
"""Serialize a URL or response into bytes"""