django - 动态更改页面缓存
问题描述
我想动态缓存一个页面
一般我们有:
from django.views.decorators.cache import cache_page
@cache_page(60 * 15)
def my_view(request):
...
或者
from django.views.decorators.cache import cache_page
urlpatterns = [
path('foo/<int:code>/', cache_page(60 * 15)(my_view)),
]
但是在视图里面我想根据一些逻辑来判断,改变缓存机制(比如清除缓存并重新启动新缓存)
def my_view(request):
if some logic is true:
then clear cache_page()
execute the view login
response = ......
add response to cache_page()
return response
else:
return the old cached page
解决方案
我认为在您的用例Conditional View Processing
中最合适,它可以满足您的需求,只是它依赖Etag
标头告诉浏览器内容没有更改。更多关于Conditional view processing
https://docs.djangoproject.com/en/2.1/topics/conditional-view-processing/
推荐阅读
- vuejs2 - 无法读取属性状态
- python - python pyautogui humanclicker 鼠标缓慢移动
- c - 在函数中传递地址后指向垃圾值的指针
- python - 后台任务不等待机器人准备好
- java - Whatsapp 自动发送消息而无需单击 android 上的发送按钮
- c++ - 如果我最终需要获取 char 指针,我可以安全地使用 string_view 吗?
- reactjs - 如何使用 react beautiful dnd 将任务项移动到初始列之外?
- sql - 仅从重复项中检索最低行
- linux - 为什么 file->private_data 不返回您的上下文数据?
- access-denied - 我无法在 cmd 中运行命令访问被拒绝以管理员身份