Ticket #11269: callable_key_prefix.diff

File callable_key_prefix.diff, 2.4 KB (added by Mikhail Korobov, 15 years ago)
  • django/middleware/cache.py

     
    8989            # max-age was set to 0, don't bother caching.
    9090            return response
    9191        patch_response_headers(response, timeout)
    92         if timeout:
    93             cache_key = learn_cache_key(request, response, timeout, self.key_prefix)
     92       
     93        if timeout:           
     94            if callable(self.key_prefix):
     95                key_prefix = self.key_prefix(request)
     96            else:
     97                key_prefix = self.key_prefix
     98           
     99            cache_key = learn_cache_key(request, response, timeout, key_prefix)
    94100            cache.set(cache_key, response, timeout)
    95101        return response
    96102
     
    115121        if self.cache_anonymous_only:
    116122            assert hasattr(request, 'user'), "The Django cache middleware with CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True requires authentication middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.auth.middleware.AuthenticationMiddleware' before the CacheMiddleware."
    117123
    118         if not request.method in ('GET', 'HEAD') or request.GET:
     124        if not request.method in ('GET', 'HEAD'):
    119125            request._cache_update_cache = False
    120126            return None # Don't bother checking the cache.
     127       
     128        if request.GET and not callable(self.key_prefix):
     129            request._cache_update_cache = False
     130            return None # Default behaviour for requests with GET parameters: don't bother checking the cache.
    121131
    122132        if self.cache_anonymous_only and request.user.is_authenticated():
    123133            request._cache_update_cache = False
    124134            return None # Don't cache requests from authenticated users.
    125135
    126         cache_key = get_cache_key(request, self.key_prefix)
     136        if callable(self.key_prefix):
     137            key_prefix = self.key_prefix(request)
     138            if key_prefix is None:
     139                request._cache_update_cache = False
     140                return None # Don't bother checking the cache if key_prefix function returns magic "None" value.
     141        else:
     142            key_prefix = self.key_prefix
     143               
     144        cache_key = get_cache_key(request, key_prefix)
    127145        if cache_key is None:
    128146            request._cache_update_cache = True
    129147            return None # No cache information available, need to rebuild.
Back to Top