{"id":122,"date":"2015-01-29T20:21:22","date_gmt":"2015-01-29T12:21:22","guid":{"rendered":"http:\/\/vinta.ws\/code\/?p=122"},"modified":"2026-02-18T01:20:37","modified_gmt":"2026-02-17T17:20:37","slug":"cache-headers-in-django","status":"publish","type":"post","link":"https:\/\/vinta.ws\/code\/cache-headers-in-django.html","title":{"rendered":"HTTP Cache Headers in Django"},"content":{"rendered":"<h2>Conditional View Processing<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-py\">def latest_entry(request, blog_id):\n    return Entry.objects.filter(blog=blog_id).latest(\"published\").published\n\n@condition(last_modified_func=latest_entry)\ndef front_page(request, blog_id):\n    ...<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/docs.djangoproject.com\/en\/dev\/topics\/conditional-view-processing\/\">https:\/\/docs.djangoproject.com\/en\/dev\/topics\/conditional-view-processing\/<\/a><\/p>\n<h3>Cache Middlewares<\/h3>\n<p>\u5982\u679c\u6709\u555f\u7528 Django \u7684 cache middleware\uff08\u5c31\u662f <code>UpdateCacheMiddleware<\/code> \u548c <code>FetchFromCacheMiddleware<\/code>\uff09<br \/>\n\u6bcf\u4e00\u500b request \u90fd\u6703\u88ab\u6a19\u4e0a Cache-Control: max-age=600<br \/>\n\u90a3\u500b 600 \u662f\u6839\u64da <code>CACHE_MIDDLEWARE_SECONDS<\/code> \u8a2d\u5b9a\u7684\u503c<\/p>\n<p>\u53ea\u8981\u8a2d\u7f6e\u4e86 max-age &gt; 0<br \/>\nresponse header \u4e2d\u5c31\u6703\u88ab\u81ea\u52d5\u52a0\u5165 <code>Cache-Control<\/code>\u3001<code>Expires<\/code> \u548c <code>Last-Modified<\/code> \u5169\u500b\u6b04\u4f4d<\/p>\n<p>ref:<br \/>\n<a href=\"https:\/\/docs.djangoproject.com\/en\/dev\/ref\/settings\/#std:setting-CACHE_MIDDLEWARE_SECONDS\">https:\/\/docs.djangoproject.com\/en\/dev\/ref\/settings\/#std:setting-CACHE_MIDDLEWARE_SECONDS<\/a><\/p>\n<h3>Never Cache Decorator<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-py\">from django.views.decorators.cache import never_cache\n\n@never_cache\ndef myview(request):\n    pass<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u55ae\u7d14\u7684\u5c31\u662f\u4e0d\u5e0c\u671b\u88ab cache<br \/>\n\u5c31\u4f7f\u7528\u9019\u7a2e\u65b9\u5f0f<br \/>\n<code>@never_cache<\/code> \u53ea\u6703\u8a2d\u7f6e <code>Cache-Control: max-age=0<\/code>\uff08<code>max-age=0<\/code> \u662f\u99ac\u4e0a\u904e\u671f\uff09<\/p>\n<h3>Cache Control Decorator<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-py\">from django.views.decorators.cache import cache_control\n\nclass SongDetail(SVAPIDetailView):\n    serializer_class = api_serializers.SongDetailSerializer\n\n    @cache_control(no_store=True, no_cache=True, max_age=0)\n    def get(self, request, song_id):\n        do_something()\n\n        return Response(data)<\/code><\/pre>\n<p>\u4e0d\u77e5\u9053\u70ba\u4ec0\u9ebc\uff0c\u53ea\u8a2d\u7f6e no-store \u548c no-cache \u7684\u8a71<br \/>\niOS \u7684 AFNetworking \u9084\u662f\u6703 cache<br \/>\n\u7167\u9053\u7406\u8aaa no-store \u7684\u512a\u5148\u6b0a\u61c9\u8a72\u662f\u6700\u9ad8\u7684<br \/>\n\u76ee\u524d\u7684\u89e3\u6cd5\u662f\u4f7f\u7528 <code>Cache-Control: max-age=0<\/code><\/p>\n<p>ref:<br \/>\n<a href=\"https:\/\/docs.djangoproject.com\/en\/dev\/topics\/cache\/#controlling-cache-using-other-headers\">https:\/\/docs.djangoproject.com\/en\/dev\/topics\/cache\/#controlling-cache-using-other-headers<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>no-store vs no-cache <\/p>\n","protected":false},"author":1,"featured_media":767,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,116],"tags":[56,13,2],"class_list":["post-122","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-about-python","category-about-web-development","tag-cache","tag-django","tag-python"],"_links":{"self":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":0,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media\/767"}],"wp:attachment":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}