=== modified file 'django/conf/global_settings.py'
|
|
|
275 | 275 | # possible values. |
276 | 276 | CACHE_BACKEND = 'simple://' |
277 | 277 | CACHE_MIDDLEWARE_KEY_PREFIX = '' |
| 278 | CACHE_MIDDLEWARE_SECONDS = 60 * 10 |
278 | 279 | |
279 | 280 | #################### |
280 | 281 | # COMMENTS # |
=== modified file 'django/utils/decorators.py'
|
|
|
1 | 1 | "Functions that help with dynamically creating decorators for views." |
2 | 2 | |
3 | | def decorator_from_middleware(middleware_class): |
4 | | """ |
5 | | Given a middleware class (not an instance), returns a view decorator. This |
6 | | lets you use middleware functionality on a per-view basis. |
7 | | """ |
8 | | def _decorator_from_middleware(view_func, *args, **kwargs): |
9 | | middleware = middleware_class(*args, **kwargs) |
| 3 | def make_decorator_from_middleware(middleware_class, *deco_args, |
| 4 | **deco_kwargs): |
| 5 | """ |
| 6 | A decorator factory that returns a decorator from a middleware class. |
| 7 | """ |
| 8 | def _decorator_from_middleware(view_func): |
| 9 | middleware = middleware_class(*deco_args, **deco_kwargs) |
10 | 10 | def _wrapped_view(request, *args, **kwargs): |
11 | 11 | if hasattr(middleware, 'process_request'): |
12 | 12 | result = middleware.process_request(request) |
… |
… |
|
31 | 31 | return response |
32 | 32 | return _wrapped_view |
33 | 33 | return _decorator_from_middleware |
| 34 | |
| 35 | def decorator_from_middleware(middleware_class): |
| 36 | """ |
| 37 | Given a middleware class (not an instance), returns a view decorator. This |
| 38 | lets you use middleware functionality on a per-view basis. |
| 39 | """ |
| 40 | return make_decorator_from_middleware(middleware_class) |
| 41 | |
| 42 | def decorator_with_arguments_from_middleware(middleware_class): |
| 43 | """ |
| 44 | Given a middleware class (not an instance), return a function that, |
| 45 | when called, returns a view decorator. This lets you use middleware |
| 46 | functionality on a per-view basis for middleware classes whose |
| 47 | contructors take arguments. |
| 48 | """ |
| 49 | def _decorator_factory(*deco_args, **deco_kwargs): |
| 50 | return make_decorator_from_middleware(middleware_class, *deco_args, |
| 51 | **deco_kwargs) |
| 52 | return _decorator_factory |
=== modified file 'django/views/decorators/cache.py'
|
|
|
11 | 11 | account on caching -- just like the middleware does. |
12 | 12 | """ |
13 | 13 | |
14 | | from django.utils.decorators import decorator_from_middleware |
| 14 | from django.utils.decorators import decorator_with_arguments_from_middleware |
15 | 15 | from django.utils.cache import patch_cache_control, add_never_cache_headers |
16 | 16 | from django.middleware.cache import CacheMiddleware |
17 | 17 | |
18 | | cache_page = decorator_from_middleware(CacheMiddleware) |
| 18 | cache_page = decorator_with_arguments_from_middleware(CacheMiddleware) |
19 | 19 | |
20 | 20 | def cache_control(**kwargs): |
21 | 21 | |
=== modified file 'docs/cache.txt'
|
|
|
274 | 274 | def slashdot_this(request): |
275 | 275 | ... |
276 | 276 | |
277 | | slashdot_this = cache_page(slashdot_this, 60 * 15) |
| 277 | slashdot_this = cache_page(60 * 15)(slashdot_this) |
278 | 278 | |
279 | 279 | Or, using Python 2.4's decorator syntax:: |
280 | 280 | |
… |
… |
|
282 | 282 | def slashdot_this(request): |
283 | 283 | ... |
284 | 284 | |
285 | | ``cache_page`` takes a single argument: the cache timeout, in seconds. In the |
| 285 | ``cache_page`` can be passed the following optional arguments: |
| 286 | * ``cache_timeout`` - the cache timeout in seconds. Defaults to |
| 287 | ``settings.CACHE_MIDDLEWARE_SECONDS``, which defaults to 600 seconds |
| 288 | (10 minutes). |
| 289 | * ``key_prefix`` - the prefix to use for cache keys. Defaults to |
| 290 | ``settings.CACHE_MIDDLEWARE_KEY_PREFIX``, which defaults to ``''``. |
| 291 | * ``cache_anonymous_only`` - if True, only cache anonymous requests. Defaults |
| 292 | to ``setings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY``. |
286 | 293 | above example, the result of the ``slashdot_this()`` view will be cached for 15 |
287 | 294 | minutes. |
288 | 295 | |