Django 缓存和条件装饰器
这是来自 django 文档:
condition(etag_func=None, last_modified_func=None)
etag(etag_func)
last_modified(last_modified_func)
These decorators can be used to generate ETag and Last-Modified headers
然而,这也来自django 文档:
Additionally, the cache middleware automatically sets a few headers in each HttpResponse:
Sets the Last-Modified header to the current date/time when a fresh (uncached) version of the page is requested.
Sets the Expires header to the current date/time plus the defined CACHE_MIDDLEWARE_SECONDS.
Sets the Cache-Control header to give a max age for the page -- again, from the CACHE_MIDDLEWARE_SECONDS setting.
所以我想知道当我按站点使用两者缓存和条件装饰器。
编辑:另外,您会建议同时使用它们,还是只使用其中之一?
This is from the django documentation:
condition(etag_func=None, last_modified_func=None)
etag(etag_func)
last_modified(last_modified_func)
These decorators can be used to generate ETag and Last-Modified headers
and yet, this is also from django documentation:
Additionally, the cache middleware automatically sets a few headers in each HttpResponse:
Sets the Last-Modified header to the current date/time when a fresh (uncached) version of the page is requested.
Sets the Expires header to the current date/time plus the defined CACHE_MIDDLEWARE_SECONDS.
Sets the Cache-Control header to give a max age for the page -- again, from the CACHE_MIDDLEWARE_SECONDS setting.
So I was wondering what value does Last-Modified takes when I use both sitewise caching and condition decorator.
Edit: And also, would you recommend using them both, or just using one of them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
缓存中间件只会设置
ETag
、Last-Modified
和Expires
标头(如果它们尚不存在)(请参阅 patch_response_headers 方法从 UpdateCacheMiddleware 调用)。并且由于装饰器将在从视图返回响应后直接执行,因此装饰器标头在中间件运行时已经存在。
简而言之:
Last-Modified
将具有来自 condition/last_modified 装饰器的值。The cache middleware will only set the
ETag
,Last-Modified
andExpires
header if they are not already present (see the patch_response_headers method that is called from UpdateCacheMiddleware ).And since the decorators will be executed directly after the response is returned from the view, the decorator headers will be already present when the the middleware runs.
In short:
Last-Modified
will have the value from the condition/last_modified decorator.