def check_cache(self, request): """ Checks whether the page is already cached and returns the cached version if available. """ if not request.method in ('GET', 'HEAD'): request._cache_update_cache = False return None # Don't bother checking the cache. # if request said no-cache in header then don't return from cache if request.headers.has_key('Cache-Control') and request.headers['Cache-Control'] == 'no-cache': request._cache_update_cache = True return None # try and get the cached GET response cache_key = get_cache_key(request, self.key_prefix, 'GET', cache=self.cache) if cache_key is None: request._cache_update_cache = True return None # No cache information available, need to rebuild. response = self.cache.get(cache_key, None) # if it wasn't found and we are looking for a HEAD, try looking just for that if response is None and request.method == 'HEAD': cache_key = get_cache_key(request, self.key_prefix, 'HEAD', cache=self.cache) response = self.cache.get(cache_key, None) if response is None: request._cache_update_cache = True return None # No cache information available, need to rebuild. # hit, return cached response request._cache_update_cache = False return response
def process_304_response(self, request, response): cache_key = get_cache_key(request, LONG_TERM_CACHE_KEY_PREFIX+self.key_prefix, 'GET', cache=self.cache) if cache_key is None: return None cached_response = self.cache.get(cache_key, None) if cached_response is None: return None else: response._content = cached_response.content return response
def patch_if_none_match_header(self, request): """ Add 'If-None-Match' header to request if: 1. request does not have 'If-None-Match' already, and 2. Previous response has 'ETag' header. """ if 'If-None-Match' not in request.headers: cache_key = get_cache_key(request, LONG_TERM_CACHE_KEY_PREFIX+self.key_prefix, 'GET', cache=self.cache) if cache_key is not None: response = self.cache.get(cache_key, None) if response is not None: if response.has_header('ETag'): request.headers['If-None-Match'] = response['ETag']
def patch_if_modified_since_header(self, request): """ Add 'If-Modified-Since' header to request if: 1. request does not have 'If-Modified-Since' already, and 2. Previous response has 'Last-Modified' header. """ if 'If-Modified-Since' not in request.headers: cache_key = get_cache_key(request, LONG_TERM_CACHE_KEY_PREFIX+self.key_prefix, 'GET', cache=self.cache) if cache_key is not None: response = self.cache.get(cache_key, None) if response is not None: if response.has_header('Last-Modified'): request.headers['If-Modified-Since'] = response['Last-Modified']