Ticket #9249: cache_ignore_cookie.patch
File cache_ignore_cookie.patch, 5.4 KB (added by , 15 years ago) |
---|
-
django/conf/global_settings.py
331 331 CACHE_BACKEND = 'locmem://' 332 332 CACHE_MIDDLEWARE_KEY_PREFIX = '' 333 333 CACHE_MIDDLEWARE_SECONDS = 600 334 CACHE_MIDDLEWARE_IGNORE_COOKIES = () 334 335 335 336 #################### 336 337 # COMMENTS # -
django/utils/cache.py
142 142 ctx = md5_constructor() 143 143 for header in headerlist: 144 144 value = request.META.get(header, None) 145 if value is not None: 145 if header == "HTTP_COOKIE" and len(request.COOKIES) > 0: 146 cookies = request.COOKIES 147 for key in cookies.keys(): 148 for re in settings.CACHE_MIDDLEWARE_IGNORE_COOKIES: 149 if re.search(key) and cookies.has_key(key): 150 del cookies[key] 151 if len(cookies) > 0: 152 # Builds a new header value without ignored cookies 153 ctx.update(";".join(['%s=%s' % (k,v) for k,v in cookies.items() ])) 154 elif value is not None: 146 155 ctx.update(value) 147 156 path = md5_constructor(iri_to_uri(request.path)) 148 157 return 'views.decorators.cache.cache_page.%s.%s.%s' % ( -
docs/topics/cache.txt
287 287 sites using the same Django installation, set this to the name of the site, 288 288 or some other string that is unique to this Django instance, to prevent key 289 289 collisions. Use an empty string if you don't care. 290 * ``CACHE_MIDDLEWARE_IGNORE_COOKIES`` -- Tuple of compiled regular expression 291 patterns of request cookies that should not be considered if "Cookie" is set 292 in ``Vary`` header. See the `Using Vary headers`_ section for more on this 293 setting. 290 294 291 295 The cache middleware caches every page that doesn't have GET or POST 292 296 parameters. Optionally, if the ``CACHE_MIDDLEWARE_ANONYMOUS_ONLY`` setting is … … 647 651 def my_view(request): 648 652 # ... 649 653 654 If your application contains client-side Javascript code saving its 655 state in cookies you may meet a problem when varying on cookie makes 656 Django to cache a new copy of a page for each client cookie 657 variation. You can optionally specify those client-side cookie in an 658 optional ``CACHE_MIDDLEWARE_IGNORE_COOKIES`` setting, making Django to 659 ignore them when building a cache key thus improving hit performance 660 and memory consumption. For example, you may want to ignore Google 661 Analytics tracking cookies:: 662 663 import re 664 CACHE_MIDDLEWARE_IGNORE_COOKIES = ( 665 re.compile('^__utm'), 666 ) 667 650 668 The headers you pass to ``vary_on_headers`` are not case sensitive; 651 669 ``"User-Agent"`` is the same thing as ``"user-agent"``. 652 670 -
tests/regressiontests/cache/tests.py
4 4 # Uses whatever cache backend is set in the test settings file. 5 5 6 6 import os 7 import re 7 8 import shutil 8 9 import tempfile 9 10 import time … … 298 299 self.path = '/cache/test/' 299 300 self.old_settings_key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX 300 301 self.old_middleware_seconds = settings.CACHE_MIDDLEWARE_SECONDS 302 self.old_ignore_cookies = settings.CACHE_MIDDLEWARE_IGNORE_COOKIES 301 303 settings.CACHE_MIDDLEWARE_KEY_PREFIX = 'settingsprefix' 302 304 settings.CACHE_MIDDLEWARE_SECONDS = 1 305 settings.CACHE_MIDDLEWARE_IGNORE_COOKIES = ( re.compile(r'^testcookie$'), ) 303 306 304 307 def tearDown(self): 305 308 settings.CACHE_MIDDLEWARE_KEY_PREFIX = self.old_settings_key_prefix 306 309 settings.CACHE_MIDDLEWARE_SECONDS = self.old_middleware_seconds 310 settings.CACHE_MIDDLEWARE_IGNORE_COOKIES = self.old_ignore_cookies 307 311 308 312 def _get_request(self, path): 309 313 request = HttpRequest() … … 355 359 learn_cache_key(request, response) 356 360 self.assertEqual(get_cache_key(request), 'views.decorators.cache.cache_page.settingsprefix.a8c87a3d8c44853d7f79474f7ffe4ad5.d41d8cd98f00b204e9800998ecf8427e') 357 361 362 def test_ignore_cookies(self): 363 request = self._get_request(self.path) 364 response = HttpResponse() 365 response['Vary'] = 'Cookie' 366 learn_cache_key(request, response) 367 # Set request cookies not matched in CACHE_MIDDLEWARE_IGNORE_COOKIES 368 request.COOKIES = { 'foo': 1, 'bar': 2 } 369 self.assertEqual(get_cache_key(request), 'views.decorators.cache.cache_page.settingsprefix.a8c87a3d8c44853d7f79474f7ffe4ad5.b4cb935fc7540cefaa961599c74eab3d') 370 request.COOKIES = { 'foo': 1, 'bar': 2, 'testcookie': 'value' } 371 # Ensure same key as without ignored cookie 372 self.assertEqual(get_cache_key(request), 'views.decorators.cache.cache_page.settingsprefix.a8c87a3d8c44853d7f79474f7ffe4ad5.b4cb935fc7540cefaa961599c74eab3d') 373 request.COOKIES = { } 374 self.assertEqual(get_cache_key(request), 'views.decorators.cache.cache_page.settingsprefix.a8c87a3d8c44853d7f79474f7ffe4ad5.d41d8cd98f00b204e9800998ecf8427e') 375 358 376 if __name__ == '__main__': 359 377 unittest.main()