{"id":540,"date":"2016-05-21T19:51:32","date_gmt":"2016-05-21T18:51:32","guid":{"rendered":"http:\/\/lazic.info\/josip\/?p=540"},"modified":"2017-08-21T20:54:11","modified_gmt":"2017-08-21T19:54:11","slug":"add-hitmiss-info-to-headers-with-django-cache-middleware","status":"publish","type":"post","link":"https:\/\/lazic.info\/josip\/post\/add-hitmiss-info-to-headers-with-django-cache-middleware\/","title":{"rendered":"Add HIT\/MISS info to headers with Django cache middleware"},"content":{"rendered":"<p>Django cache framework is really great, and since 1.9 we have really useful decorator <em>cache_page<\/em> (<a href=\"https:\/\/docs.djangoproject.com\/en\/1.9\/topics\/cache\/#the-per-view-cache\" title=\"undefined\" target=\"\">https:\/\/docs.djangoproject.com\/en\/1.9\/topics\/cache\/#the-per-view-cache<\/a>) that handles per-view caching. However, I missed the ability to add hit\/miss info to headers of each request, something that both nginx and varnish add with simple directive in configuration file.<\/p>\n<p>What we can do in Django is to subclass CacheMiddleware and change process_request and process_response methods. First create new class that inherits from CacheMiddleware, and then create new decorator (c\/p the one from <em>django.views.decorators.cache<\/em>) that will call our new class something like this:<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/jlazic\/732a1ea45a0dc2b57b2463d3cf6f1b5e.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/jlazic\/732a1ea45a0dc2b57b2463d3cf6f1b5e\">Gist<\/a>.<\/noscript><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Django cache framework is really great, and since 1.9 we have really useful decorator cache_page (https:\/\/docs.djangoproject.com\/en\/1.9\/topics\/cache\/#the-per-view-cache) that handles per-view caching. However, I missed the ability to add hit\/miss info to headers of each request, something that both nginx and varnish add with simple directive in configuration file. What we can do in Django is to [&hellip;]<\/p>\n<div class=\"clearfix text-center more-button\"><a href=\"https:\/\/lazic.info\/josip\/post\/add-hitmiss-info-to-headers-with-django-cache-middleware\/\" class=\"btn btn-success\">Continue reading<\/a><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,19],"tags":[],"class_list":["post-540","post","type-post","status-publish","format-standard","hentry","category-django","category-python"],"_links":{"self":[{"href":"https:\/\/lazic.info\/josip\/wp-json\/wp\/v2\/posts\/540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lazic.info\/josip\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lazic.info\/josip\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lazic.info\/josip\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lazic.info\/josip\/wp-json\/wp\/v2\/comments?post=540"}],"version-history":[{"count":1,"href":"https:\/\/lazic.info\/josip\/wp-json\/wp\/v2\/posts\/540\/revisions"}],"predecessor-version":[{"id":549,"href":"https:\/\/lazic.info\/josip\/wp-json\/wp\/v2\/posts\/540\/revisions\/549"}],"wp:attachment":[{"href":"https:\/\/lazic.info\/josip\/wp-json\/wp\/v2\/media?parent=540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lazic.info\/josip\/wp-json\/wp\/v2\/categories?post=540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lazic.info\/josip\/wp-json\/wp\/v2\/tags?post=540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}