Enable caching in the context of a view. If desired, instructions on the cache parameters can be included, such as if the data is fit for public caches (default: no, vary_user) and what values to include in the generation of an etag.
(vary_user=True, vary=None)
| 40 | |
| 41 | |
| 42 | def enable_cache(vary_user=True, vary=None): |
| 43 | """Enable caching in the context of a view. |
| 44 | |
| 45 | If desired, instructions on the cache parameters can be included, such as |
| 46 | if the data is fit for public caches (default: no, vary_user) and what |
| 47 | values to include in the generation of an etag. |
| 48 | """ |
| 49 | if not SETTINGS.CACHE: |
| 50 | return |
| 51 | |
| 52 | request._http_cache = True |
| 53 | request._http_revalidate = vary is not None |
| 54 | args = sorted(set(request.args.items())) |
| 55 | cache_parts = [args, vary, request._app_locale] |
| 56 | |
| 57 | if vary_user and request.authz.logged_in: |
| 58 | cache_parts.extend((request.authz.roles)) |
| 59 | request._http_private = True |
| 60 | |
| 61 | request._http_etag = hash_data(cache_parts) |
| 62 | if request._http_etag in request.if_none_match: |
| 63 | raise NotModified() |
| 64 | |
| 65 | |
| 66 | def _get_remote_ip(): |
no test coverage detected