Ticket #11269: callable_key_prefix.diff
File callable_key_prefix.diff, 2.4 KB (added by , 15 years ago) |
---|
-
django/middleware/cache.py
89 89 # max-age was set to 0, don't bother caching. 90 90 return response 91 91 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) 94 100 cache.set(cache_key, response, timeout) 95 101 return response 96 102 … … 115 121 if self.cache_anonymous_only: 116 122 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." 117 123 118 if not request.method in ('GET', 'HEAD') or request.GET:124 if not request.method in ('GET', 'HEAD'): 119 125 request._cache_update_cache = False 120 126 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. 121 131 122 132 if self.cache_anonymous_only and request.user.is_authenticated(): 123 133 request._cache_update_cache = False 124 134 return None # Don't cache requests from authenticated users. 125 135 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) 127 145 if cache_key is None: 128 146 request._cache_update_cache = True 129 147 return None # No cache information available, need to rebuild.