Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Beefed up the tests for multi-cache handling of the cache middleware …

…and view decorators, and made a couple of tweaks for edge cases as a result.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15021 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b22415214a7bdeaf8ccd7b8b21872038ab865991 1 parent 0fef92f
Russell Keith-Magee authored December 22, 2010
39  django/middleware/cache.py
@@ -85,7 +85,7 @@ def process_response(self, request, response):
85 85
             return response
86 86
         patch_response_headers(response, timeout)
87 87
         if timeout:
88  
-            cache_key = learn_cache_key(request, response, timeout, self.key_prefix)
  88
+            cache_key = learn_cache_key(request, response, timeout, self.key_prefix, cache=self.cache)
89 89
             self.cache.set(cache_key, response, timeout)
90 90
         return response
91 91
 
@@ -120,17 +120,14 @@ def process_request(self, request):
120 120
             return None # Don't cache requests from authenticated users.
121 121
 
122 122
         # try and get the cached GET response
123  
-        cache_key = get_cache_key(request, self.key_prefix, 'GET')
124  
-
  123
+        cache_key = get_cache_key(request, self.key_prefix, 'GET', cache=self.cache)
125 124
         if cache_key is None:
126 125
             request._cache_update_cache = True
127 126
             return None # No cache information available, need to rebuild.
128  
-
129 127
         response = self.cache.get(cache_key, None)
130  
-
131 128
         # if it wasn't found and we are looking for a HEAD, try looking just for that
132 129
         if response is None and request.method == 'HEAD':
133  
-            cache_key = get_cache_key(request, self.key_prefix, 'HEAD')
  130
+            cache_key = get_cache_key(request, self.key_prefix, 'HEAD', cache=self.cache)
134 131
             response = self.cache.get(cache_key, None)
135 132
 
136 133
         if response is None:
@@ -149,28 +146,33 @@ class CacheMiddleware(UpdateCacheMiddleware, FetchFromCacheMiddleware):
149 146
     using the decorator-from-middleware utility.
150 147
     """
151 148
     def __init__(self, cache_timeout=None, cache_anonymous_only=None, **kwargs):
152  
-        self.cache_timeout = cache_timeout
153  
-        if cache_timeout is None:
154  
-            self.cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
155  
-
156 149
         # We need to differentiate between "provided, but using default value",
157 150
         # and "not provided". If the value is provided using a default, then
158 151
         # we fall back to system defaults. If it is not provided at all,
159 152
         # we need to use middleware defaults.
  153
+
  154
+        cache_kwargs = {}
  155
+        try:
  156
+            self.key_prefix = kwargs.get('key_prefix')
  157
+            if self.key_prefix is not None:
  158
+                cache_kwargs['KEY_PREFIX'] = self.key_prefix
  159
+            else:
  160
+                self.key_prefix = ''
  161
+        except KeyError:
  162
+            self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
  163
+            cache_kwargs['KEY_PREFIX'] = self.key_prefix
160 164
         try:
161 165
             cache_alias = kwargs.get('cache_alias')
162 166
             if cache_alias is None:
163 167
                 cache_alias = DEFAULT_CACHE_ALIAS
  168
+            if cache_timeout is not None:
  169
+                cache_kwargs['TIMEOUT'] = cache_timeout
164 170
         except KeyError:
165 171
             cache_alias = settings.CACHE_MIDDLEWARE_ALIAS
166  
-
167  
-        cache_kwargs = {}
168  
-        try:
169  
-            key_prefix = kwargs.get('key_prefix')
170  
-            if key_prefix is not None:
171  
-                cache_kwargs['KEY_PREFIX'] = key_prefix
172  
-        except KeyError:
173  
-            cache_kwargs['KEY_PREFIX'] = settings.CACHE_MIDDLEWARE_KEY_PREFIX
  172
+            if cache_timeout is None:
  173
+                cache_kwargs['TIMEOUT'] = settings.CACHE_MIDDLEWARE_SECONDS
  174
+            else:
  175
+                cache_kwargs['TIMEOUT'] = cache_timeout
174 176
 
175 177
         if cache_anonymous_only is None:
176 178
             self.cache_anonymous_only = getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False)
@@ -178,3 +180,4 @@ def __init__(self, cache_timeout=None, cache_anonymous_only=None, **kwargs):
178 180
             self.cache_anonymous_only = cache_anonymous_only
179 181
 
180 182
         self.cache = get_cache(cache_alias, **cache_kwargs)
  183
+        self.cache_timeout = self.cache.default_timeout
10  django/utils/cache.py
@@ -21,7 +21,7 @@
21 21
 import time
22 22
 
23 23
 from django.conf import settings
24  
-from django.core.cache import cache
  24
+from django.core.cache import get_cache
25 25
 from django.utils.encoding import smart_str, iri_to_uri
26 26
 from django.utils.http import http_date
27 27
 from django.utils.hashcompat import md5_constructor
@@ -162,7 +162,7 @@ def _generate_cache_header_key(key_prefix, request):
162 162
         key_prefix, path.hexdigest())
163 163
     return _i18n_cache_key_suffix(request, cache_key)
164 164
 
165  
-def get_cache_key(request, key_prefix=None, method='GET'):
  165
+def get_cache_key(request, key_prefix=None, method='GET', cache=None):
166 166
     """
167 167
     Returns a cache key based on the request path. It can be used in the
168 168
     request phase because it pulls the list of headers to take into account
@@ -175,13 +175,15 @@ def get_cache_key(request, key_prefix=None, method='GET'):
175 175
     if key_prefix is None:
176 176
         key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
177 177
     cache_key = _generate_cache_header_key(key_prefix, request)
  178
+    if cache is None:
  179
+        cache = get_cache(settings.CACHE_MIDDLEWARE_ALIAS)
178 180
     headerlist = cache.get(cache_key, None)
179 181
     if headerlist is not None:
180 182
         return _generate_cache_key(request, method, headerlist, key_prefix)
181 183
     else:
182 184
         return None
183 185
 
184  
-def learn_cache_key(request, response, cache_timeout=None, key_prefix=None):
  186
+def learn_cache_key(request, response, cache_timeout=None, key_prefix=None, cache=None):
185 187
     """
186 188
     Learns what headers to take into account for some request path from the
187 189
     response object. It stores those headers in a global path registry so that
@@ -199,6 +201,8 @@ def learn_cache_key(request, response, cache_timeout=None, key_prefix=None):
199 201
     if cache_timeout is None:
200 202
         cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
201 203
     cache_key = _generate_cache_header_key(key_prefix, request)
  204
+    if cache is None:
  205
+        cache = get_cache(settings.CACHE_MIDDLEWARE_ALIAS)
202 206
     if response.has_header('Vary'):
203 207
         headerlist = ['HTTP_'+header.upper().replace('-', '_')
204 208
                       for header in cc_delim_re.split(response['Vary'])]
154  tests/regressiontests/cache/tests.py
@@ -14,12 +14,14 @@
14 14
 from django.core.cache import get_cache, DEFAULT_CACHE_ALIAS
15 15
 from django.core.cache.backends.base import InvalidCacheBackendError, CacheKeyWarning
16 16
 from django.http import HttpResponse, HttpRequest
17  
-from django.middleware.cache import FetchFromCacheMiddleware, UpdateCacheMiddleware
  17
+from django.middleware.cache import FetchFromCacheMiddleware, UpdateCacheMiddleware, CacheMiddleware
  18
+from django.test import RequestFactory
18 19
 from django.test.utils import get_warnings_state, restore_warnings_state
19 20
 from django.utils import translation
20 21
 from django.utils import unittest
21 22
 from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key
22 23
 from django.utils.hashcompat import md5_constructor
  24
+from django.views.decorators.cache import cache_page
23 25
 from regressiontests.cache.models import Poll, expensive_calculation
24 26
 
25 27
 # functions/classes for complex data type tests
@@ -759,6 +761,15 @@ def test_old_initialization(self):
759 761
         self.cache = get_cache('locmem://?max_entries=30&cull_frequency=0')
760 762
         self.perform_cull_test(50, 19)
761 763
 
  764
+    def test_multiple_caches(self):
  765
+        "Check that multiple locmem caches are isolated"
  766
+        mirror_cache = get_cache('django.core.cache.backends.locmem.LocMemCache')
  767
+        other_cache = get_cache('django.core.cache.backends.locmem.LocMemCache', LOCATION='other')
  768
+
  769
+        self.cache.set('value1', 42)
  770
+        self.assertEquals(mirror_cache.get('value1'), 42)
  771
+        self.assertEquals(other_cache.get('value1'), None)
  772
+
762 773
 # memcached backend isn't guaranteed to be available.
763 774
 # To check the memcached backend, the test settings file will
764 775
 # need to contain a cache backend setting that points at
@@ -1117,5 +1128,146 @@ def tearDown(self):
1117 1128
         else:
1118 1129
             settings.CACHES['default']['KEY_PREFIX'] = self.old_cache_key_prefix
1119 1130
 
  1131
+class CacheMiddlewareTest(unittest.TestCase):
  1132
+
  1133
+    def setUp(self):
  1134
+        self.orig_cache_middleware_alias = settings.CACHE_MIDDLEWARE_ALIAS
  1135
+        self.orig_cache_middleware_key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
  1136
+        self.orig_caches = settings.CACHES
  1137
+
  1138
+        settings.CACHE_MIDDLEWARE_ALIAS = 'other'
  1139
+        settings.CACHE_MIDDLEWARE_KEY_PREFIX = 'middlewareprefix'
  1140
+        settings.CACHES = {
  1141
+            'default': {
  1142
+                'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'
  1143
+            },
  1144
+            'other': {
  1145
+                'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
  1146
+                'LOCATION': 'other',
  1147
+                'TIMEOUT': '1'
  1148
+            }
  1149
+        }
  1150
+
  1151
+    def tearDown(self):
  1152
+        settings.CACHE_MIDDLEWARE_ALIAS = self.orig_cache_middleware_alias
  1153
+        settings.CACHE_MIDDLEWARE_KEY_PREFIX = self.orig_cache_middleware_key_prefix
  1154
+        settings.CACHES = self.orig_caches
  1155
+
  1156
+    def test_middleware(self):
  1157
+        def view(request, value):
  1158
+            return HttpResponse('Hello World %s' % value)
  1159
+
  1160
+        factory = RequestFactory()
  1161
+
  1162
+        middleware = CacheMiddleware()
  1163
+        prefix_middleware = CacheMiddleware(key_prefix='prefix1')
  1164
+        timeout_middleware = CacheMiddleware(cache_timeout=1)
  1165
+
  1166
+        request = factory.get('/view/')
  1167
+
  1168
+        # Put the request through the request middleware
  1169
+        result = middleware.process_request(request)
  1170
+        self.assertEquals(result, None)
  1171
+
  1172
+        response = view(request, '1')
  1173
+
  1174
+        # Now put the response through the response middleware
  1175
+        response = middleware.process_response(request, response)
  1176
+
  1177
+        # Repeating the request should result in a cache hit
  1178
+        result = middleware.process_request(request)
  1179
+        self.assertNotEquals(result, None)
  1180
+        self.assertEquals(result.content, 'Hello World 1')
  1181
+
  1182
+        # The same request through a different middleware won't hit
  1183
+        result = prefix_middleware.process_request(request)
  1184
+        self.assertEquals(result, None)
  1185
+
  1186
+        # The same request with a timeout _will_ hit
  1187
+        result = timeout_middleware.process_request(request)
  1188
+        self.assertNotEquals(result, None)
  1189
+        self.assertEquals(result.content, 'Hello World 1')
  1190
+
  1191
+    def test_view_decorator(self):
  1192
+        def view(request, value):
  1193
+            return HttpResponse('Hello World %s' % value)
  1194
+
  1195
+        # decorate the same view with different cache decorators
  1196
+        default_view = cache_page(view)
  1197
+        default_with_prefix_view = cache_page(key_prefix='prefix1')(view)
  1198
+
  1199
+        explicit_default_view = cache_page(cache='default')(view)
  1200
+        explicit_default_with_prefix_view = cache_page(cache='default', key_prefix='prefix1')(view)
  1201
+
  1202
+        other_view = cache_page(cache='other')(view)
  1203
+        other_with_prefix_view = cache_page(cache='other', key_prefix='prefix2')(view)
  1204
+
  1205
+        factory = RequestFactory()
  1206
+        request = factory.get('/view/')
  1207
+
  1208
+        # Request the view once
  1209
+        response = default_view(request, '1')
  1210
+        self.assertEquals(response.content, 'Hello World 1')
  1211
+
  1212
+        # Request again -- hit the cache
  1213
+        response = default_view(request, '2')
  1214
+        self.assertEquals(response.content, 'Hello World 1')
  1215
+
  1216
+        # Requesting the same view with the explicit cache should yield the same result
  1217
+        response = explicit_default_view(request, '3')
  1218
+        self.assertEquals(response.content, 'Hello World 1')
  1219
+
  1220
+        # Requesting with a prefix will hit a different cache key
  1221
+        response = explicit_default_with_prefix_view(request, '4')
  1222
+        self.assertEquals(response.content, 'Hello World 4')
  1223
+
  1224
+        # Hitting the same view again gives a cache hit
  1225
+        response = explicit_default_with_prefix_view(request, '5')
  1226
+        self.assertEquals(response.content, 'Hello World 4')
  1227
+
  1228
+        # And going back to the implicit cache will hit the same cache
  1229
+        response = default_with_prefix_view(request, '6')
  1230
+        self.assertEquals(response.content, 'Hello World 4')
  1231
+
  1232
+        # Requesting from an alternate cache won't hit cache
  1233
+        response = other_view(request, '7')
  1234
+        self.assertEquals(response.content, 'Hello World 7')
  1235
+
  1236
+        # But a repeated hit will hit cache
  1237
+        response = other_view(request, '8')
  1238
+        self.assertEquals(response.content, 'Hello World 7')
  1239
+
  1240
+        # And prefixing the alternate cache yields yet another cache entry
  1241
+        response = other_with_prefix_view(request, '9')
  1242
+        self.assertEquals(response.content, 'Hello World 9')
  1243
+
  1244
+        # But if we wait a couple of seconds...
  1245
+        time.sleep(2)
  1246
+
  1247
+        # ... the default cache will still hit
  1248
+        cache = get_cache('default')
  1249
+        response = default_view(request, '10')
  1250
+        self.assertEquals(response.content, 'Hello World 1')
  1251
+
  1252
+        # ... the default cache with a prefix will still hit
  1253
+        response = default_with_prefix_view(request, '11')
  1254
+        self.assertEquals(response.content, 'Hello World 4')
  1255
+
  1256
+        # ... the explicit default cache will still hit
  1257
+        response = explicit_default_view(request, '12')
  1258
+        self.assertEquals(response.content, 'Hello World 1')
  1259
+
  1260
+        # ... the explicit default cache with a prefix will still hit
  1261
+        response = explicit_default_with_prefix_view(request, '13')
  1262
+        self.assertEquals(response.content, 'Hello World 4')
  1263
+
  1264
+        # .. but a rapidly expiring cache won't hit
  1265
+        response = other_view(request, '14')
  1266
+        self.assertEquals(response.content, 'Hello World 14')
  1267
+
  1268
+        # .. even if it has a prefix
  1269
+        response = other_with_prefix_view(request, '15')
  1270
+        self.assertEquals(response.content, 'Hello World 15')
  1271
+
1120 1272
 if __name__ == '__main__':
1121 1273
     unittest.main()

0 notes on commit b224152

Please sign in to comment.
Something went wrong with that request. Please try again.