Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added 'key_prefix' keyword argument to cache_page()

This was available before r11586, but undocumented.  It has now been
re-added with documentation and explicit support, as it seems like a useful
feature and people were using it before.



git-svn-id: http://code.djangoproject.com/svn/django/trunk@11595 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6e3a72585a727d46ea03871f14a9a2f0f15fbf93 1 parent a97648a
Luke Plant authored September 28, 2009
14  django/views/decorators/cache.py
@@ -20,27 +20,33 @@
20 20
 from django.utils.cache import patch_cache_control, add_never_cache_headers
21 21
 from django.middleware.cache import CacheMiddleware
22 22
 
23  
-def cache_page(*args):
  23
+def cache_page(*args, **kwargs):
24 24
     # We need backwards compatibility with code which spells it this way:
25 25
     #   def my_view(): pass
26 26
     #   my_view = cache_page(my_view, 123)
27 27
     # and this way:
28 28
     #   my_view = cache_page(123)(my_view)
  29
+    # and this:
  30
+    #   my_view = cache_page(my_view, 123, key_prefix="foo")
  31
+    # and this:
  32
+    #   my_view = cache_page(123, key_prefix="foo")(my_view)
29 33
     # and possibly this way (?):
30 34
     #   my_view = cache_page(123, my_view)
31 35
 
32 36
     # We also add some asserts to give better error messages in case people are
33 37
     # using other ways to call cache_page that no longer work.
  38
+    key_prefix = kwargs.pop('key_prefix', None)
  39
+    assert not kwargs, "The only keyword argument accepted is key_prefix"
34 40
     if len(args) > 1:
35 41
         assert len(args) == 2, "cache_page accepts at most 2 arguments"
36 42
         if callable(args[0]):
37  
-            return decorator_from_middleware_with_args(CacheMiddleware)(args[1])(args[0])
  43
+            return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[1], key_prefix=key_prefix)(args[0])
38 44
         elif callable(args[1]):
39  
-            return decorator_from_middleware_with_args(CacheMiddleware)(args[0])(args[1])
  45
+            return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], key_prefix=key_prefix)(args[1])
40 46
         else:
41 47
             assert False, "cache_page must be passed either a single argument (timeout) or a view function and a timeout"
42 48
     else:
43  
-        return decorator_from_middleware_with_args(CacheMiddleware)(args[0])
  49
+        return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], key_prefix=key_prefix)
44 50
 
45 51
 def cache_control(**kwargs):
46 52
 
11  docs/topics/cache.txt
@@ -361,6 +361,17 @@ then requests to ``/foo/1/`` and ``/foo/23/`` will be cached separately, as
361 361
 you may expect. But once a particular URL (e.g., ``/foo/23/``) has been
362 362
 requested, subsequent requests to that URL will use the cache.
363 363
 
  364
+``cache_page`` can also take an optional keyword argument, ``key_prefix``, which
  365
+works in the same way as the ``CACHE_MIDDLEWARE_KEY_PREFIX`` setting for the
  366
+middleware.  It can be used like this::
  367
+
  368
+    my_view = cache_page(my_view, 60 * 15, key_prefix="site1")
  369
+
  370
+Or, using Python 2.4's decorator syntax::
  371
+
  372
+    @cache_page(60 * 15, key_prefix="site1")
  373
+    def my_view(request):
  374
+
364 375
 Specifying per-view cache in the URLconf
365 376
 ----------------------------------------
366 377
 
4  tests/regressiontests/decorators/tests.py
@@ -98,6 +98,8 @@ def my_view(request):
98 98
             return "response"
99 99
         my_view_cached = cache_page(123)(my_view)
100 100
         self.assertEqual(my_view_cached(HttpRequest()), "response")
  101
+        my_view_cached2 = cache_page(123, key_prefix="test")(my_view)
  102
+        self.assertEqual(my_view_cached2(HttpRequest()), "response")
101 103
 
102 104
     def test_cache_page_old_style(self):
103 105
         """
@@ -107,6 +109,8 @@ def my_view(request):
107 109
             return "response"
108 110
         my_view_cached = cache_page(my_view, 123)
109 111
         self.assertEqual(my_view_cached(HttpRequest()), "response")
  112
+        my_view_cached2 = cache_page(my_view, 123, key_prefix="test")
  113
+        self.assertEqual(my_view_cached2(HttpRequest()), "response")
110 114
 
111 115
 class MethodDecoratorAdapterTests(TestCase):
112 116
     def test_auto_adapt_to_methods(self):

0 notes on commit 6e3a725

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