{"id":2310,"date":"2021-04-13T10:10:00","date_gmt":"2021-04-13T09:10:00","guid":{"rendered":"http:\/\/www.jurecuhalev.com\/blog\/?p=2310"},"modified":"2021-04-13T10:10:00","modified_gmt":"2021-04-13T09:10:00","slug":"conditional-cache-mixin-for-django-drf","status":"publish","type":"post","link":"https:\/\/www.jurecuhalev.com\/blog\/conditional-cache-mixin-for-django-drf\/","title":{"rendered":"Conditional Cache Mixin for Django DRF"},"content":{"rendered":"\n<p>For a project I&#8217;m doing I&#8217;m looking at adding a conditional header to bypass cache when an X-No-Cache is present. In my case this allows external system to flush cache when certain conditions are met.<\/p>\n\n\n\n<p>I&#8217;ve modified code from <a href=\"https:\/\/github.com\/chibisov\/drf-extensions\">Django Rest Framework Extension<\/a> to allow for such behaviour. There might be a better way to do it, but at the moment the flow of the code is clear to me. It also needs <code>drf-extensions<\/code> as it&#8217;s just an additional mixin that offloads the code to <code>cache_response<\/code> decorator.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: false; title: ; notranslate\" title=\"\">\nfrom rest_framework_extensions.cache.decorators import cache_response\nfrom rest_framework_extensions.settings import extensions_api_settings\n\n\nclass BaseCacheResponseMixin(object):\n    object_cache_key_func = extensions_api_settings.DEFAULT_OBJECT_CACHE_KEY_FUNC\n    list_cache_key_func = extensions_api_settings.DEFAULT_LIST_CACHE_KEY_FUNC\n\n\nclass ConditionalListCacheResponseMixin(BaseCacheResponseMixin):\n    @cache_response(key_func=&quot;list_cache_key_func&quot;)\n    def _cached_list(self, request, *args, **kwargs):\n        return super().list(request, *args, **kwargs)\n\n    def list(self, request, *args, **kwargs):\n        if request.META.get(&quot;HTTP_X_NO_CACHE&quot;) == &quot;1&quot;:\n            return super().list(request, *args, **kwargs)\n        else:\n            return self._cached_list(request, *args, **kwargs)\n\n\nclass ConditionalRetrieveCacheResponseMixin(BaseCacheResponseMixin):\n    @cache_response(key_func=&quot;object_cache_key_func&quot;)\n    def _cached_retrieve(self, request, *args, **kwargs):\n        return super().retrieve(request, *args, **kwargs)\n\n    def retrieve(self, request, *args, **kwargs):\n        if request.META.get(&quot;HTTP_X_NO_CACHE&quot;) == &quot;1&quot;:\n            return super().retrieve(request, *args, **kwargs)\n        else:\n            return self._cached_retrieve(request, *args, **kwargs)\n\n\nclass ConditionalCacheResponseMixin(\n    ConditionalRetrieveCacheResponseMixin, ConditionalListCacheResponseMixin\n):\n    pass\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>For a project I&#8217;m doing I&#8217;m looking at adding a conditional header to bypass cache when an X-No-Cache is present. In my case this allows external system to flush cache when certain conditions are met. I&#8217;ve modified code from Django Rest Framework Extension to allow for such behaviour. There might be a better way to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[925,15,14],"tags":[],"class_list":["post-2310","post","type-post","status-publish","format-standard","hentry","category-ember-js","category-python","category-tech"],"acf":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts\/2310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/comments?post=2310"}],"version-history":[{"count":1,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts\/2310\/revisions"}],"predecessor-version":[{"id":2315,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts\/2310\/revisions\/2315"}],"wp:attachment":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/media?parent=2310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/categories?post=2310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/tags?post=2310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}