Code

Ticket #4992: cache_by_request_full_path.diff

File cache_by_request_full_path.diff, 3.9 KB (added by PeterKz, 5 years ago)

Cache by request.get_full_path() instead of request.path

Line 
1Index: django/utils/cache.py
2===================================================================
3--- django/utils/cache.py       (revision 9329)
4+++ django/utils/cache.py       (working copy)
5@@ -144,11 +144,11 @@
6         if value is not None:
7             ctx.update(value)
8     return 'views.decorators.cache.cache_page.%s.%s.%s' % (
9-               key_prefix, iri_to_uri(request.path), ctx.hexdigest())
10+               key_prefix, iri_to_uri(request.get_full_path()), ctx.hexdigest())
11 
12 def get_cache_key(request, key_prefix=None):
13     """
14-    Returns a cache key based on the request path. It can be used in the
15+    Returns a cache key based on the request path and query. It can be used in the
16     request phase because it pulls the list of headers to take into account
17     from the global path registry and uses those to build a cache key to check
18     against.
19@@ -159,7 +159,7 @@
20     if key_prefix is None:
21         key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
22     cache_key = 'views.decorators.cache.cache_header.%s.%s' % (
23-                    key_prefix, iri_to_uri(request.path))
24+                    key_prefix, iri_to_uri(request.get_full_path()))
25     headerlist = cache.get(cache_key, None)
26     if headerlist is not None:
27         return _generate_cache_key(request, headerlist, key_prefix)
28@@ -184,7 +184,7 @@
29     if cache_timeout is None:
30         cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
31     cache_key = 'views.decorators.cache.cache_header.%s.%s' % (
32-                    key_prefix, iri_to_uri(request.path))
33+                    key_prefix, iri_to_uri(request.get_full_path()))
34     if response.has_header('Vary'):
35         headerlist = ['HTTP_'+header.upper().replace('-', '_')
36                       for header in cc_delim_re.split(response['Vary'])]
37@@ -192,7 +192,7 @@
38         return _generate_cache_key(request, headerlist, key_prefix)
39     else:
40         # if there is no Vary header, we still need a cache key
41-        # for the request.path
42+        # for the request.get_full_path()
43         cache.set(cache_key, [], cache_timeout)
44         return _generate_cache_key(request, [], key_prefix)
45 
46Index: django/middleware/cache.py
47===================================================================
48--- django/middleware/cache.py  (revision 9329)
49+++ django/middleware/cache.py  (working copy)
50@@ -23,7 +23,7 @@
51 
52 More details about how the caching works:
53 
54-* Only parameter-less GET or HEAD-requests with status code 200 are cached.
55+* Only GET or HEAD-requests with status code 200 are cached.
56 
57 * The number of seconds each page is stored for is set by the "max-age" section
58   of the response's "Cache-Control" header, falling back to the
59@@ -115,7 +115,7 @@
60         if self.cache_anonymous_only:
61             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."
62 
63-        if not request.method in ('GET', 'HEAD') or request.GET:
64+        if not request.method in ('GET', 'HEAD'):
65             request._cache_update_cache = False
66             return None # Don't bother checking the cache.
67 
68Index: docs/topics/cache.txt
69===================================================================
70--- docs/topics/cache.txt       (revision 9329)
71+++ docs/topics/cache.txt       (working copy)
72@@ -487,8 +487,8 @@
73 said to "vary on language."
74 
75 By default, Django's cache system creates its cache keys using the requested
76-path -- e.g., ``"/stories/2005/jun/23/bank_robbed/"``. This means every request
77-to that URL will use the same cached version, regardless of user-agent
78+path and query -- e.g., ``"/stories/2005/?order_by=author"``. This means every
79+request to that URL will use the same cached version, regardless of user-agent
80 differences such as cookies or language preferences.
81 
82 That's where ``Vary`` comes in.