def _process_headers(self, request, headers):

        cache_key = generate_cache_key(request, headers, self.key_prefix)
        if cache_key is None:
            # No cache information available, need to rebuild.
            request._cache_update_cache = True
            return

        response = cache_backend.get(cache_key, None)
        if response is None:
            # No cache information available, need to rebuild.
            request._cache_update_cache = True
            return None

        # Check the Etag/Last-Modified headers of the cache response.
        etag = response.has_header('Etag') and response['Etag']
        last_modified = response.has_header('Last-Modified') and response['Last-Modified']
        if not_modified(request, etag, last_modified):
            # Nothing changed since they last downloaded it.
            request._cache_update_cache = False
            return HttpResponseNotModified()

        request._cache_update_cache = False
        response['X-From-Cache'] = True
        return response
    def _process_headers(self, request, headers):

        cache_key = generate_cache_key(request, headers, self.key_prefix)
        if cache_key is None:
            # No cache information available, need to rebuild.
            request._cache_update_cache = True
            return

        response = cache_backend.get(cache_key, None)
        if response is None:
            # No cache information available, need to rebuild.
            request._cache_update_cache = True
            return None

        # Check the Etag/Last-Modified headers of the cache response.
        etag = response.has_header('Etag') and response['Etag']
        last_modified = response.has_header(
            'Last-Modified') and response['Last-Modified']
        if not_modified(request, etag, last_modified):
            # Nothing changed since they last downloaded it.
            request._cache_update_cache = False
            return HttpResponseNotModified()

        request._cache_update_cache = False
        response['X-From-Cache'] = True
        return response
    def process_request(self, request):

        if request.method == 'PURGE':
            # TODO: check request IP address against a setting
            if False:
                request.method = 'GET'
                request._cache_update_cache = True
                request._purging = True
                return
            else:
                request._cache_update_cache = False
                return HttpResponseForbidden(
                    'Your address is now allowed to make purge requests.')

        if request.method not in ('GET', 'HEAD') or request.is_secure():
            # Don't bother checking the cache.
            request._cache_update_cache = False
            return

        headers = get_cached_headers(request, self.key_prefix)
        if headers is None:
            # No cache information available, need to rebuild.
            request._cache_update_cache = True
            return

        if 'X-Vary-On-View' in headers:
            # Handle this request during the process_view phase.
            request._cache_middleware_headers = headers
            return

        # There is no vary, so we can check the Etag/Last-Modified headers.
        # Make sure that these headers are popped off the dictionary, because
        # they were not used when generating the response cache key.
        etag = headers.pop('HTTP_ETAG', None)
        last_modified = headers.pop('HTTP_LAST_MODIFIED', None)
        if not_modified(request, etag, last_modified):
            # Nothing changed since they last downloaded it.
            request._cache_update_cache = False
            return HttpResponseNotModified()

        # Try to return a cached response, using the request + cached headers.
        return self._process_headers(request, headers)
    def process_request(self, request):

        if request.method == 'PURGE':
            # TODO: check request IP address against a setting
            if False:
                request.method = 'GET'
                request._cache_update_cache = True
                request._purging = True
                return
            else:
                request._cache_update_cache = False
                return HttpResponseForbidden('Your address is now allowed to make purge requests.')

        if request.method not in ('GET', 'HEAD') or request.is_secure():
            # Don't bother checking the cache.
            request._cache_update_cache = False
            return

        headers = get_cached_headers(request, self.key_prefix)
        if headers is None:
            # No cache information available, need to rebuild.
            request._cache_update_cache = True
            return

        if 'X-Vary-On-View' in headers:
            # Handle this request during the process_view phase.
            request._cache_middleware_headers = headers
            return

        # There is no vary, so we can check the Etag/Last-Modified headers.
        # Make sure that these headers are popped off the dictionary, because
        # they were not used when generating the response cache key.
        etag = headers.pop('HTTP_ETAG', None)
        last_modified = headers.pop('HTTP_LAST_MODIFIED', None)
        if not_modified(request, etag, last_modified):
            # Nothing changed since they last downloaded it.
            request._cache_update_cache = False
            return HttpResponseNotModified()

        # Try to return a cached response, using the request + cached headers.
        return self._process_headers(request, headers)