在Django中,我可以使用manage cache's max-age这样的内容来获取请求:
from django.views.decorators.cache import cache_control
@cache_control(max_age=3600)
def my_view(request):
# ...
如何在视图函数中设置不同的max_age
值,以便它可以取决于request
内容是什么?
示例:
def my_view(request):
if is_good_to_cache(request):
# set max_age to 36000
else:
# set max_age to 42
# ...
答案 0 :(得分:4)
由于cache_control
是taking advantage of patch_cache_control
internally,直接放置是可以的:
from django.utils.cache import patch_cache_control
def my_view(request):
if is_good_to_cache(request):
max_age = 36000
else:
max_age = 42
resp = render(...)
patch_cache_control(resp, max_age=max_age)
return resp