Django 缓存和条件装饰器

发布于 2024-11-29 16:04:22 字数 920 浏览 7 评论 0原文

这是来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2024-12-06 16:04:22

缓存中间件只会设置 ETagLast-ModifiedExpires 标头(如果它们尚不存在)(请参阅 patch_response_headers 方法从 UpdateCacheMiddleware 调用)。

并且由于装饰器将在从视图返回响应后直接执行,因此装饰器标头在中间件运行时已经存在。

简而言之:Last-Modified 将具有来自 condition/last_modified 装饰器的值。

The cache middleware will only set the ETag, Last-Modified and Expires 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文