summaryrefslogtreecommitdiff
path: root/requests_cache/session.py
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2022-04-18 16:10:11 -0500
committerJordan Cook <jordan.cook@pioneer.com>2022-04-18 19:50:55 -0500
commita899d9231c38f11c28b3eb0310022c92d82262b8 (patch)
treecf09e91629447bbf17fc5ef98f76d1d2866cb1a0 /requests_cache/session.py
parentea326d16d82d86f4fda14f83745a5a399824257d (diff)
downloadrequests-cache-a899d9231c38f11c28b3eb0310022c92d82262b8.tar.gz
Add support for Cache-Control: stale-if-error
Diffstat (limited to 'requests_cache/session.py')
-rw-r--r--requests_cache/session.py10
1 files changed, 5 insertions, 5 deletions
diff --git a/requests_cache/session.py b/requests_cache/session.py
index 2f35d6e..59a1d4f 100644
--- a/requests_cache/session.py
+++ b/requests_cache/session.py
@@ -55,7 +55,7 @@ class CacheMixin(MIXIN_BASE):
match_headers: Union[Iterable[str], bool] = False,
filter_fn: FilterCallback = None,
key_fn: KeyCallback = None,
- stale_if_error: bool = False,
+ stale_if_error: Union[bool, int] = False,
**kwargs,
):
self.cache = init_backend(cache_name, backend, serializer=serializer, **kwargs)
@@ -256,17 +256,16 @@ class CacheMixin(MIXIN_BASE):
def _handle_error(self, cached_response: CachedResponse, actions: CacheActions) -> AnyResponse:
"""Handle a request error based on settings:
- * Default behavior: delete the stale cache item and re-raise the error
+ * Default behavior: re-raise the error
* stale-if-error: Ignore the error and and return the stale cache item
"""
- if self.settings.stale_if_error:
+ if actions.is_usable(cached_response, error=True):
logger.warning(
f'Request for URL {cached_response.request.url} failed; using cached response',
exc_info=True,
)
return cached_response
else:
- self.cache.delete(actions.cache_key)
raise
@contextmanager
@@ -331,7 +330,8 @@ class CachedSession(CacheMixin, OriginalSession):
a list of specific headers to match
ignored_parameters: Request paramters, headers, and/or JSON body params to exclude from both
request matching and cached request data
- stale_if_error: Return stale cache data if a new request raises an exception
+ stale_if_error: Return stale cache data if a new request raises an exception. Optionally
+ accepts a time value representing maximum staleness to accept.
filter_fn: Response filtering function that indicates whether or not a given response should
be cached. See :ref:`custom-filtering` for details.
key_fn: Request matching function for generating custom cache keys. See