summaryrefslogtreecommitdiff
path: root/requests_cache
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2021-10-10 13:25:11 -0500
committerJordan Cook <jordan.cook@pioneer.com>2021-10-10 14:02:19 -0500
commitd9ea0f1391ce3fdcf9e38bda1979ed7877e5b44a (patch)
tree185169048d1b0b584fdef39c9505f8ea6255b7c3 /requests_cache
parent6e10eb25a692cf4b240a628f14b2f3d31ae7e451 (diff)
downloadrequests-cache-d9ea0f1391ce3fdcf9e38bda1979ed7877e5b44a.tar.gz
Support immediate expiration + revalidation for Cache-Control: max-age=0 and Expires: 0
Diffstat (limited to 'requests_cache')
-rw-r--r--requests_cache/cache_control.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/requests_cache/cache_control.py b/requests_cache/cache_control.py
index 909ff5c..2cad7b4 100644
--- a/requests_cache/cache_control.py
+++ b/requests_cache/cache_control.py
@@ -136,12 +136,23 @@ class CacheActions:
directives = get_cache_directives(response.headers)
logger.debug(f'Cache directives from response headers: {directives}')
- # Check expiration headers and conditions for writing to the cache
+ # Check expiration headers
self.expire_after = coalesce(
directives.get('max-age'), directives.get('expires'), self.expire_after
)
+
+ # If expiration is 0 and there's a validator, save it to the cache and revalidate on use
+ has_validator = response.headers.get('ETag') or response.headers.get('Last-Modified')
+ if self.expire_after == DO_NOT_CACHE and has_validator:
+ self.expire_after = datetime.utcnow()
+
+ # Check conditions for writing to the cache
self.skip_write = any(
- [self.expire_after == DO_NOT_CACHE, 'no-store' in directives, self.skip_write]
+ [
+ self.expire_after == DO_NOT_CACHE,
+ 'no-store' in directives,
+ self.skip_write,
+ ]
)