Esempio n. 1
0
    def process_request(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.

        # try and get the cached GET response
        cache_key = get_cache_key(request, get_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, get_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
Esempio n. 2
0
    def __init__(self, cache_timeout=None, cache_anonymous_only=None, **kwargs):
        # We need to differentiate between "provided, but using default value",
        # and "not provided". If the value is provided using a default, then
        # we fall back to system defaults. If it is not provided at all,
        # we need to use middleware defaults.

        cache_kwargs = {}

        try:
            self.key_prefix = kwargs["key_prefix"]
            if self.key_prefix is not None:
                cache_kwargs["KEY_PREFIX"] = self.key_prefix
            else:
                self.key_prefix = ""
        except KeyError:
            self.key_prefix = get_key_prefix()
            cache_kwargs["KEY_PREFIX"] = self.key_prefix

        try:
            self.cache_alias = kwargs["cache_alias"]
            if self.cache_alias is None:
                self.cache_alias = DEFAULT_CACHE_ALIAS
            if cache_timeout is not None:
                cache_kwargs["TIMEOUT"] = cache_timeout
        except KeyError:
            self.cache_alias = settings.CACHE_MIDDLEWARE_ALIAS
            if cache_timeout is None:
                cache_kwargs["TIMEOUT"] = settings.CACHE_MIDDLEWARE_SECONDS
            else:
                cache_kwargs["TIMEOUT"] = cache_timeout

        if cache_anonymous_only is None:
            self.cache_anonymous_only = getattr(settings, "CACHE_MIDDLEWARE_ANONYMOUS_ONLY", False)
        else:
            self.cache_anonymous_only = cache_anonymous_only

        self.cache = get_cache(self.cache_alias, **cache_kwargs)
        self.cache_timeout = self.cache.default_timeout
Esempio n. 3
0
 def process_response(self, request, response):
     """Sets the cache, if needed."""
     if not self._should_update_cache(request, response):
         # We don't need to update the cache, just return.
         return response
     if not response.status_code == 200:
         return response
     # Try to get the timeout from the "max-age" section of the "Cache-
     # Control" header before reverting to using the default cache_timeout
     # length.
     timeout = get_max_age(response)
     if timeout == None:
         timeout = self.cache_timeout
     elif timeout == 0:
         # max-age was set to 0, don't bother caching.
         return response
     patch_response_headers(response, timeout)
     if timeout:
         cache_key = learn_cache_key(request, response, timeout, get_key_prefix(), cache=self.cache)
         if hasattr(response, "render") and callable(response.render):
             response.add_post_render_callback(lambda r: self.cache.set(cache_key, r, timeout))
         else:
             self.cache.set(cache_key, response, timeout)
     return response