Ticket #15144: fix_15144_regressions.patch
File fix_15144_regressions.patch, 7.8 KB (added by , 14 years ago) |
---|
-
tests/regressiontests/cache/tests.py
1133 1133 def setUp(self): 1134 1134 self.orig_cache_middleware_alias = settings.CACHE_MIDDLEWARE_ALIAS 1135 1135 self.orig_cache_middleware_key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX 1136 self.orig_cache_middleware_seconds = settings.CACHE_MIDDLEWARE_SECONDS 1137 self.orig_cache_middleware_anonymous_only = getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False) 1136 1138 self.orig_caches = settings.CACHES 1137 1139 1138 1140 settings.CACHE_MIDDLEWARE_ALIAS = 'other' 1139 1141 settings.CACHE_MIDDLEWARE_KEY_PREFIX = 'middlewareprefix' 1142 settings.CACHE_MIDDLEWARE_SECONDS = 30 1143 settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = False 1140 1144 settings.CACHES = { 1141 1145 'default': { 1142 1146 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache' … … 1151 1155 def tearDown(self): 1152 1156 settings.CACHE_MIDDLEWARE_ALIAS = self.orig_cache_middleware_alias 1153 1157 settings.CACHE_MIDDLEWARE_KEY_PREFIX = self.orig_cache_middleware_key_prefix 1158 settings.CACHE_MIDDLEWARE_SECONDS = self.orig_cache_middleware_seconds 1159 settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = self.orig_cache_middleware_anonymous_only 1154 1160 settings.CACHES = self.orig_caches 1161 1162 def test_constructor(self): 1163 """ 1164 Ensure the constructor is correctly distinguishing between usage of CacheMiddleware as 1165 Middleware vs. usage of CacheMiddleware as view decorator and setting attributes 1166 appropriately. 1167 """ 1168 # If no arguments are passed in construction, it's being used as middleware. 1169 middleware = CacheMiddleware() 1170 1171 # Now test object attributes against values defined in setUp above 1172 self.assertEquals(middleware.cache_timeout, 30) 1173 self.assertEquals(middleware.key_prefix, 'middlewareprefix') 1174 self.assertEquals(middleware.cache_alias, 'other') 1175 self.assertEquals(middleware.cache_anonymous_only, False) 1176 1177 # If arguments are being passed in construction, it's being used as a decorator. 1178 # First, test with "defaults": 1179 as_view_decorator = CacheMiddleware(cache_alias=None, key_prefix=None) 1180 1181 self.assertEquals(as_view_decorator.cache_timeout, 300) # Timeout value for 'default' cache, i.e. 300 1182 self.assertEquals(as_view_decorator.key_prefix, '') 1183 self.assertEquals(as_view_decorator.cache_alias, 'default') # Value of DEFAULT_CACHE_ALIAS from django.core.cache 1184 self.assertEquals(as_view_decorator.cache_anonymous_only, False) 1185 1186 # Next, test with custom values: 1187 as_view_decorator_with_custom = CacheMiddleware(cache_anonymous_only=True, cache_timeout=60, cache_alias='other', key_prefix='foo') 1188 1189 self.assertEquals(as_view_decorator_with_custom.cache_timeout, 60) 1190 self.assertEquals(as_view_decorator_with_custom.key_prefix, 'foo') 1191 self.assertEquals(as_view_decorator_with_custom.cache_alias, 'other') 1192 self.assertEquals(as_view_decorator_with_custom.cache_anonymous_only, True) 1155 1193 1156 1194 def test_middleware(self): 1157 1195 def view(request, value): … … 1201 1239 1202 1240 other_view = cache_page(cache='other')(view) 1203 1241 other_with_prefix_view = cache_page(cache='other', key_prefix='prefix2')(view) 1204 1242 other_with_timeout_view = cache_page(4, cache='other', key_prefix='prefix3')(view) 1243 1205 1244 factory = RequestFactory() 1206 1245 request = factory.get('/view/') 1207 1246 … … 1240 1279 # And prefixing the alternate cache yields yet another cache entry 1241 1280 response = other_with_prefix_view(request, '9') 1242 1281 self.assertEquals(response.content, 'Hello World 9') 1243 1282 1283 # Request from the alternate cache with a new prefix and a custom timeout 1284 response = other_with_timeout_view(request, '20') 1285 self.assertEquals(response.content, 'Hello World 20') 1286 1244 1287 # But if we wait a couple of seconds... 1245 1288 time.sleep(2) 1246 1289 … … 1268 1311 # .. even if it has a prefix 1269 1312 response = other_with_prefix_view(request, '15') 1270 1313 self.assertEquals(response.content, 'Hello World 15') 1314 1315 # ... but a view with a custom timeout will still hit 1316 response = other_with_timeout_view(request, '21') 1317 self.assertEquals(response.content, 'Hello World 20') 1318 1319 # And if we wait a few more seconds 1320 time.sleep(2) 1321 1322 # the custom timeouot cache will miss 1323 response = other_with_timeout_view(request, '22') 1324 self.assertEquals(response.content, 'Hello World 22') 1271 1325 1272 1326 if __name__ == '__main__': 1273 1327 unittest.main() -
django/core/cache/__init__.py
153 153 backend, location, params = parse_backend_uri(backend) 154 154 if backend in BACKENDS: 155 155 backend = 'django.core.cache.backends.%s' % BACKENDS[backend] 156 params.update(kwargs)157 156 mod = importlib.import_module(backend) 158 157 backend_cls = mod.CacheClass 159 158 else: … … 164 163 except (AttributeError, ImportError), e: 165 164 raise InvalidCacheBackendError( 166 165 "Could not find backend '%s': %s" % (backend, e)) 166 params.update(kwargs) 167 167 return backend_cls(location, params) 168 168 169 169 cache = get_cache(DEFAULT_CACHE_ALIAS) -
django/middleware/cache.py
152 152 # we need to use middleware defaults. 153 153 154 154 cache_kwargs = {} 155 try: 156 self.key_prefix = kwargs.get('key_prefix') 157 if self.key_prefix is not None: 155 156 if 'key_prefix' in kwargs: 157 self.key_prefix = kwargs['key_prefix'] 158 if self.key_prefix: 158 159 cache_kwargs['KEY_PREFIX'] = self.key_prefix 159 160 else: 160 161 self.key_prefix = '' 161 e xcept KeyError:162 else: 162 163 self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX 163 164 cache_kwargs['KEY_PREFIX'] = self.key_prefix 164 try: 165 cache_alias = kwargs.get('cache_alias') 166 if cache_alias is None: 167 cache_alias = DEFAULT_CACHE_ALIAS 168 if cache_timeout is not None: 165 166 if 'cache_alias' in kwargs: 167 self.cache_alias = kwargs['cache_alias'] or DEFAULT_CACHE_ALIAS 168 if cache_timeout: 169 169 cache_kwargs['TIMEOUT'] = cache_timeout 170 except KeyError:171 cache_alias = settings.CACHE_MIDDLEWARE_ALIAS172 if cache_timeout is None:173 cache_kwargs['TIMEOUT'] = settings.CACHE_MIDDLEWARE_SECONDS174 else:175 cache_kwargs['TIMEOUT'] = cache_timeout176 177 if cache_anonymous_only is None:178 self.cache_anonymous_only = getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False)179 170 else: 180 self.cache_anonymous_only = cache_anonymous_only 171 self.cache_alias = settings.CACHE_MIDDLEWARE_ALIAS 172 cache_kwargs['TIMEOUT'] = cache_timeout or settings.CACHE_MIDDLEWARE_SECONDS 181 173 182 self.cache = get_cache(cache_alias, **cache_kwargs) 174 self.cache_anonymous_only = cache_anonymous_only or getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False) 175 176 self.cache = get_cache(self.cache_alias, **cache_kwargs) 183 177 self.cache_timeout = self.cache.default_timeout