Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@spookylukey spookylukey authored
View
14 django/views/decorators/cache.py
@@ -20,27 +20,33 @@
from django.utils.cache import patch_cache_control, add_never_cache_headers
from django.middleware.cache import CacheMiddleware
-def cache_page(*args):
+def cache_page(*args, **kwargs):
# We need backwards compatibility with code which spells it this way:
# def my_view(): pass
# my_view = cache_page(my_view, 123)
# and this way:
# my_view = cache_page(123)(my_view)
+ # and this:
+ # my_view = cache_page(my_view, 123, key_prefix="foo")
+ # and this:
+ # my_view = cache_page(123, key_prefix="foo")(my_view)
# and possibly this way (?):
# my_view = cache_page(123, my_view)
# We also add some asserts to give better error messages in case people are
# using other ways to call cache_page that no longer work.
+ key_prefix = kwargs.pop('key_prefix', None)
+ assert not kwargs, "The only keyword argument accepted is key_prefix"
if len(args) > 1:
assert len(args) == 2, "cache_page accepts at most 2 arguments"
if callable(args[0]):
- return decorator_from_middleware_with_args(CacheMiddleware)(args[1])(args[0])
+ return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[1], key_prefix=key_prefix)(args[0])
elif callable(args[1]):
- return decorator_from_middleware_with_args(CacheMiddleware)(args[0])(args[1])
+ return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], key_prefix=key_prefix)(args[1])
else:
assert False, "cache_page must be passed either a single argument (timeout) or a view function and a timeout"
else:
- return decorator_from_middleware_with_args(CacheMiddleware)(args[0])
+ return decorator_from_middleware_with_args(CacheMiddleware)(cache_timeout=args[0], key_prefix=key_prefix)
def cache_control(**kwargs):
View
11 docs/topics/cache.txt
@@ -361,6 +361,17 @@ then requests to ``/foo/1/`` and ``/foo/23/`` will be cached separately, as
you may expect. But once a particular URL (e.g., ``/foo/23/``) has been
requested, subsequent requests to that URL will use the cache.
+``cache_page`` can also take an optional keyword argument, ``key_prefix``, which
+works in the same way as the ``CACHE_MIDDLEWARE_KEY_PREFIX`` setting for the
+middleware. It can be used like this::
+
+ my_view = cache_page(my_view, 60 * 15, key_prefix="site1")
+
+Or, using Python 2.4's decorator syntax::
+
+ @cache_page(60 * 15, key_prefix="site1")
+ def my_view(request):
+
Specifying per-view cache in the URLconf
----------------------------------------
View
4 tests/regressiontests/decorators/tests.py
@@ -98,6 +98,8 @@ def my_view(request):
return "response"
my_view_cached = cache_page(123)(my_view)
self.assertEqual(my_view_cached(HttpRequest()), "response")
+ my_view_cached2 = cache_page(123, key_prefix="test")(my_view)
+ self.assertEqual(my_view_cached2(HttpRequest()), "response")
def test_cache_page_old_style(self):
"""
@@ -107,6 +109,8 @@ def my_view(request):
return "response"
my_view_cached = cache_page(my_view, 123)
self.assertEqual(my_view_cached(HttpRequest()), "response")
+ my_view_cached2 = cache_page(my_view, 123, key_prefix="test")
+ self.assertEqual(my_view_cached2(HttpRequest()), "response")
class MethodDecoratorAdapterTests(TestCase):
def test_auto_adapt_to_methods(self):
Please sign in to comment.
Something went wrong with that request. Please try again.