Ticket #4992: cache_by_request_full_path_v2.diff
File cache_by_request_full_path_v2.diff, 7.0 KB (added by , 14 years ago) |
---|
-
docs/topics/cache.txt
731 731 said to "vary on language." 732 732 733 733 By default, Django's cache system creates its cache keys using the requested 734 path (e.g., ``"/stories/2005/jun/23/bank_robbed/"``). This means every request735 to that URL will use the same cached version, regardless of user-agent734 path and query -- e.g., ``"/stories/2005/?order_by=author"``. This means every 735 request to that URL will use the same cached version, regardless of user-agent 736 736 differences such as cookies or language preferences. However, if this page 737 737 produces different content based on some difference in request headers -- such 738 738 as a cookie, or a language, or a user-agent -- you'll need to use the ``Vary`` -
django/utils/cache.py
150 150 value = request.META.get(header, None) 151 151 if value is not None: 152 152 ctx.update(value) 153 path = md5_constructor(iri_to_uri(request. path))153 path = md5_constructor(iri_to_uri(request.get_full_path())) 154 154 cache_key = 'views.decorators.cache.cache_page.%s.%s.%s' % ( 155 155 key_prefix, path.hexdigest(), ctx.hexdigest()) 156 156 return _i18n_cache_key_suffix(request, cache_key) 157 157 158 158 def _generate_cache_header_key(key_prefix, request): 159 159 """Returns a cache key for the header cache.""" 160 path = md5_constructor(iri_to_uri(request. path))160 path = md5_constructor(iri_to_uri(request.get_full_path())) 161 161 cache_key = 'views.decorators.cache.cache_header.%s.%s' % ( 162 162 key_prefix, path.hexdigest()) 163 163 return _i18n_cache_key_suffix(request, cache_key) 164 164 165 165 def get_cache_key(request, key_prefix=None): 166 166 """ 167 Returns a cache key based on the request path . It can be used in the167 Returns a cache key based on the request path and query. It can be used in the 168 168 request phase because it pulls the list of headers to take into account 169 169 from the global path registry and uses those to build a cache key to check 170 170 against. … … 206 206 return _generate_cache_key(request, headerlist, key_prefix) 207 207 else: 208 208 # if there is no Vary header, we still need a cache key 209 # for the request. path209 # for the request.get_full_path() 210 210 cache.set(cache_key, [], cache_timeout) 211 211 return _generate_cache_key(request, [], key_prefix) 212 212 -
django/middleware/cache.py
23 23 24 24 More details about how the caching works: 25 25 26 * Only parameter-lessGET or HEAD-requests with status code 200 are cached.26 * Only GET or HEAD-requests with status code 200 are cached. 27 27 28 28 * The number of seconds each page is stored for is set by the "max-age" section 29 29 of the response's "Cache-Control" header, falling back to the … … 115 115 if self.cache_anonymous_only: 116 116 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 117 118 if not request.method in ('GET', 'HEAD') or request.GET:118 if not request.method in ('GET', 'HEAD'): 119 119 request._cache_update_cache = False 120 120 return None # Don't bother checking the cache. 121 121 -
django/http/__init__.py
61 61 return host 62 62 63 63 def get_full_path(self): 64 return '' 64 # RFC 3986 requires query string arguments to be in the ASCII range. 65 # Rather than crash if this doesn't happen, we encode defensively. 66 # taken from django/core/handlers/wsgi.py 67 return '%s%s' % (self.path, self.META.get('QUERY_STRING', '') and ('?' + iri_to_uri(self.META.get('QUERY_STRING', ''))) or '') 65 68 66 69 def build_absolute_uri(self, location=None): 67 70 """ -
tests/regressiontests/cache/tests.py
14 14 from django.core import management 15 15 from django.core.cache import get_cache 16 16 from django.core.cache.backends.base import InvalidCacheBackendError, CacheKeyWarning 17 from django.http import HttpResponse, HttpRequest 17 from django.http import HttpResponse, HttpRequest, QueryDict 18 18 from django.middleware.cache import FetchFromCacheMiddleware, UpdateCacheMiddleware 19 19 from django.utils import translation 20 20 from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key … … 588 588 request.path = request.path_info = self.path 589 589 return request 590 590 591 def _get_request_cache(self ):591 def _get_request_cache(self, query_string=None): 592 592 request = HttpRequest() 593 593 request.META = { 594 594 'SERVER_NAME': 'testserver', 595 595 'SERVER_PORT': 80, 596 596 } 597 if query_string: 598 request.META['QUERY_STRING']=query_string 599 request.GET=QueryDict(query_string) 597 600 request.path = request.path_info = self.path 598 601 request._cache_update_cache = True 599 602 request.method = 'GET' … … 629 632 settings.CACHE_MIDDLEWARE_KEY_PREFIX="test" 630 633 settings.CACHE_BACKEND='locmem:///' 631 634 settings.USE_I18N = True 635 636 # cache with non empty request.GET 637 request = self._get_request_cache(query_string='foo=bar&other=true') 638 get_cache_data = FetchFromCacheMiddleware().process_request(request) 639 # first access, cache must return None 640 self.assertEqual(get_cache_data, None) 641 response = HttpResponse() 642 content='Check for cache with QUERY_STRING' 643 response.content=content 644 UpdateCacheMiddleware().process_response(request, response) 645 get_cache_data = FetchFromCacheMiddleware().process_request(request) 646 # cache must return content 647 self.assertNotEqual(get_cache_data, None) # request with QUERY_STRING not cached. 648 self.assertEqual(get_cache_data.content, content) 649 request = self._get_request_cache(query_string='foo=bar&somethingelse=true') 650 get_cache_data = FetchFromCacheMiddleware().process_request(request) 651 # different QUERY_STRING, cache must be empty 652 self.assertEqual(get_cache_data, None) 653 654 # i18n tests 632 655 en_message ="Hello world!" 633 656 es_message ="Hola mundo!" 634 635 657 request = self._get_request_cache() 636 658 set_cache(request, 'en', en_message) 637 659 get_cache_data = FetchFromCacheMiddleware().process_request(request)