开发者

Django Caching and condition decorator

开发者 https://www.devze.com 2023-03-28 14:39 出处:网络
This is from the django documentation: condition(etag_func=None, last_modified_func=None) etag(etag_func)

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 dja开发者_开发百科ngo 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消