Issue #918 resolved

caching does not respect Cache-Control: max-age header

guest
created an issue

With caching turned on, requesting a resource with 'Cache-Control: max-age=0, must-revalidate' header still results in serving the original cached result where one exists for the requested url.

Comments (5)

  1. Anonymous

    The following lines inserted to caching.py at line 152 will fix this; sorry I don't really know (yet?) how to commit this code formally:

    1. some people still use Pragma : no-cache to demand a fresh resource pragma_no_cache=False pragma_header=request.headers.get('Pragma') if pragma_header: pragma_values=[header.strip() for header in pragma_header.split(',')] for this_value in pragma_values: if 'no-cache' in this_value: pragma_no_cache=True break
    2. added by shaun, look at cache-control max_acceptable_age=MemoryCache.delay age_seconds=int(response.time - create_time) cache_control_header=request.headers.get('Cache-Control') if cache_control_header:
    3. split string on commas to get the multiple words cache_control_values=[header.strip() for header in cache_control_header.split(',')]
    4. look for max-age for this_value in cache_control_values: if ('max-age' in this_value) and ('=' in this_value): age_pair=[age.strip() for age in this_value.split('=')] if age_pair[1].isdigit(): max_acceptable_age=int(age_pair[1]) break
    1. return if the cache is older than the acceptable age if (age_seconds > max_acceptable_age) or pragma_no_cache: request.cached = False request.cacheable = True return False
    1. Add the required Age header response.headers["Age"] = str(age_seconds)
  2. Log in to comment