Django 模板读取错误的缓存值
我正在像这样缓存我的 django 模板。
{% load cache %}
{% cache cache_timeout key_name model_id %}
<div>
{{some_variable}}
</div>
{% endcache %}
其中 cache_timeout
是我在视图中设置的超时变量。
我还使用 这个问题
它工作正常,因为我知道模板使用我使用的相同密钥设置值。问题是,当我更新缓存时(我正在检查更新的值,它是我设置的值),模板在更新之前会在一段时间内(远小于超时)显示先前的值。
我需要模板来立即更新读取的值。
I am caching in my django template like this.
{% load cache %}
{% cache cache_timeout key_name model_id %}
<div>
{{some_variable}}
</div>
{% endcache %}
Where cache_timeout
is a variable I set in the view with the timeout.
I am also manually changing the cache value using the snippet on the accepted answer to this question
It is working fine as I know the template sets the value with the same key I use. The thing is that when I update the cache (I am checking the updated value and it is the one I set) the template keeps showing the previous value for some time(way smaller than the timeout) before updating.
I need the template to update the value read immediately.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查随响应返回的 HTTP 标头。听起来像浏览器缓存。
Check the HTTP headers that are coming back with the response. Sounds like browser caching.
查看 Django 的代码: https://github.com/django /django/blob/main/django/templatetags/cache.py
看来是在设置缓存key的时候设置了缓存过期时间。
当第一次设置缓存键时,它将在缓存中,直到调用它的
cache_timeout
到期。具有可变超时的 Django 缓存无法按您的预期工作。
Looking at Django's code: https://github.com/django/django/blob/main/django/templatetags/cache.py
It seems that cache expiry time is set when setting up the cache key.
When cache key is set for the first time, it will be in cache until the
cache_timeout
with which it was called expires.Django's cache with variable timeout doesn't work as you would expect.