Skip to content
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
4 changes: 2 additions & 2 deletions cachecontrol/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class BaseCache(object):
def get(self, key):
raise NotImplementedError()

def set(self, key, value):
def set(self, key, value, expires=None):
raise NotImplementedError()

def delete(self, key):
Expand All @@ -33,7 +33,7 @@ def __init__(self, init_dict=None):
def get(self, key):
return self.data.get(key, None)

def set(self, key, value):
def set(self, key, value, expires=None):
with self.lock:
self.data.update({key: value})

Expand Down
2 changes: 1 addition & 1 deletion cachecontrol/caches/file_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def get(self, key):
except FileNotFoundError:
return None

def set(self, key, value):
def set(self, key, value, expires=None):
name = self._fn(key)

# Make sure the directory exists
Expand Down
40 changes: 36 additions & 4 deletions cachecontrol/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ def cache_response(self, request, response, body=None, status_codes=None):

response_headers = CaseInsensitiveDict(response.headers)

if 'date' in response_headers:
date = calendar.timegm(
parsedate_tz(response_headers['date'])
)
else:
date = 0

# If we've been given a body, our response has a Content-Length, that
# Content-Length is valid then we can check to see if the body we've
# been given matches the expected size, and if it doesn't we'll just
Expand Down Expand Up @@ -311,9 +318,20 @@ def cache_response(self, request, response, body=None, status_codes=None):

# If we've been given an etag, then keep the response
if self.cache_etags and "etag" in response_headers:
expires_time = 0
if response_headers.get('expires'):
expires = parsedate_tz(response_headers['expires'])
if expires is not None:
expires_time = calendar.timegm(expires) - date

expires_time = max(expires_time, 14 * 86400)

logger.debug('etag object cached for {0} seconds'.format(expires_time))
logger.debug("Caching due to etag")
self.cache.set(
cache_url, self.serializer.dumps(request, response, body)
cache_url,
self.serializer.dumps(request, response, body),
expires=expires_time
)

# Add to the cache any permanent redirects. We do this before looking
Expand All @@ -326,20 +344,34 @@ def cache_response(self, request, response, body=None, status_codes=None):
# is no date header then we can't do anything about expiring
# the cache.
elif "date" in response_headers:
date = calendar.timegm(
parsedate_tz(response_headers['date'])
)
# cache when there is a max-age > 0
if "max-age" in cc and cc["max-age"] > 0:
logger.debug("Caching b/c date exists and max-age > 0")
expires_time = cc['max-age']
self.cache.set(
cache_url, self.serializer.dumps(request, response, body)
cache_url,
self.serializer.dumps(request, response, body),
expires=expires_time
)

# If the request can expire, it means we should cache it
# in the meantime.
elif "expires" in response_headers:
if response_headers["expires"]:
logger.debug("Caching b/c of expires header")
expires = parsedate_tz(response_headers['expires'])
if expires is not None:
expires_time = calendar.timegm(expires) - date
else:
expires_time = None

logger.debug('Caching b/c of expires header. expires in {0} seconds'.format(expires_time))
self.cache.set(
cache_url, self.serializer.dumps(request, response, body)
cache_url,
self.serializer.dumps(request, response, body=body),
expires=expires_time,
)

def update_cached_response(self, request, response):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cache_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_cache_response_cache_max_age(self, cc):
req = self.req()
cc.cache_response(req, resp)
cc.serializer.dumps.assert_called_with(req, resp, None)
cc.cache.set.assert_called_with(self.url, ANY)
cc.cache.set.assert_called_with(self.url, ANY, expires=3600)

def test_cache_response_cache_max_age_with_invalid_value_not_cached(self, cc):
now = time.strftime(TIME_FMT, time.gmtime())
Expand Down