diff --git a/django/views/decorators/cache.py b/django/views/decorators/cache.py index 6e6835ad96..f051f096a0 100644 --- a/django/views/decorators/cache.py +++ b/django/views/decorators/cache.py @@ -20,27 +20,33 @@ from django.utils.decorators import decorator_from_middleware_with_args, auto_ad 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): diff --git a/docs/topics/cache.txt b/docs/topics/cache.txt index 174dbae121..c9fd1b4012 100644 --- a/docs/topics/cache.txt +++ b/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 ---------------------------------------- diff --git a/tests/regressiontests/decorators/tests.py b/tests/regressiontests/decorators/tests.py index 64d2d7df73..b81a5d665a 100644 --- a/tests/regressiontests/decorators/tests.py +++ b/tests/regressiontests/decorators/tests.py @@ -98,6 +98,8 @@ class DecoratorsTest(TestCase): 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 @@ class DecoratorsTest(TestCase): 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):