| 1 |
from functools import wraps |
| 2 |
|
| 3 |
from asgiref.sync import iscoroutinefunction |
| 4 |
|
| 5 |
from django.middleware.cache import CacheMiddleware |
| 6 |
from django.utils.cache import add_never_cache_headers, patch_cache_control |
| 7 |
from django.utils.decorators import decorator_from_middleware_with_args |
| 8 |
|
| 9 |
|
| 10 |
def cache_page(timeout, *, cache=None, key_prefix=None): |
| 11 |
""" |
| 12 |
Decorator for views that tries getting the page from the cache and |
| 13 |
populates the cache if the page isn't in the cache yet. |
| 14 |
|
| 15 |
The cache is keyed by the URL and some data from the headers. |
| 16 |
Additionally there is the key prefix that is used to distinguish different |
| 17 |
cache areas in a multi-site setup. You could use the |
| 18 |
get_current_site().domain, for example, as that is unique across a Django |
| 19 |
project. |
| 20 |
|
| 21 |
Additionally, all headers from the response's Vary header will be taken |
| 22 |
into account on caching -- just like the middleware does. |
| 23 |
""" |
| 24 |
return decorator_from_middleware_with_args(CacheMiddleware)( |
| 25 |
page_timeout=timeout, |
| 26 |
cache_alias=cache, |
| 27 |
key_prefix=key_prefix, |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
def _check_request(request, decorator_name): |
| 32 |
# Ensure argument looks like a request. |
| 33 |
if not hasattr(request, "META"): |
| 34 |
raise TypeError( |
| 35 |
f"{decorator_name} didn't receive an HttpRequest. If you are " |
| 36 |
"decorating a classmethod, be sure to use @method_decorator." |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
def cache_control(**kwargs): |
| 41 |
def _cache_controller(viewfunc): |
| 42 |
if iscoroutinefunction(viewfunc): |
| 43 |
|
| 44 |
async def _view_wrapper(request, *args, **kw): |
| 45 |
_check_request(request, "cache_control") |
| 46 |
response = await viewfunc(request, *args, **kw) |
| 47 |
patch_cache_control(response, **kwargs) |
| 48 |
return response |
| 49 |
|
| 50 |
else: |
| 51 |
|
| 52 |
def _view_wrapper(request, *args, **kw): |
| 53 |
_check_request(request, "cache_control") |
| 54 |
response = viewfunc(request, *args, **kw) |
| 55 |
patch_cache_control(response, **kwargs) |
| 56 |
return response |
| 57 |
|
| 58 |
return wraps(viewfunc)(_view_wrapper) |
| 59 |
|
| 60 |
return _cache_controller |
| 61 |
|
| 62 |
|
| 63 |
def never_cache(view_func): |
| 64 |
""" |
| 65 |
Decorator that adds headers to a response so that it will never be cached. |
| 66 |
""" |
| 67 |
|
| 68 |
if iscoroutinefunction(view_func): |
| 69 |
|
| 70 |
async def _view_wrapper(request, *args, **kwargs): |
| 71 |
_check_request(request, "never_cache") |
| 72 |
response = await view_func(request, *args, **kwargs) |
| 73 |
add_never_cache_headers(response) |
| 74 |
return response |
| 75 |
|
| 76 |
else: |
| 77 |
|
| 78 |
def _view_wrapper(request, *args, **kwargs): |
| 79 |
_check_request(request, "never_cache") |
| 80 |
response = view_func(request, *args, **kwargs) |
| 81 |
add_never_cache_headers(response) |
| 82 |
return response |
| 83 |
|
| 84 |
return wraps(view_func)(_view_wrapper) |