def cache_on_auth(timeout):
"""Cache the response based on whether or not the user is authenticated.
Do NOT use on views that include user-specific information, e.g., CSRF tokens.
"""
# https://stackoverflow.com/questions/11661503/django-caching-for-authenticated-users-only
def _decorator(view_func):
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
key_prefix = "_auth_%s_" % request.user.is_authenticated()
return cache_page(timeout, key_prefix=key_prefix)(view_func)(request, *args, **kwargs)
return _wrapped_view
return _decorator
评论列表
文章目录