6

I'm looking for a way to list out the cache keys from a specific template file when using Django's (Django 2+; Python 3.5+) template caching. For example:

{% load cache %}

{% block header %}
  {% cache 600 contact_page_header %}
    Contact page header, this will create a cache file.
  {% endcache %}
{% endblock header %}

{% block content %}
  <!-- HTML stuff here -->

  {% cache 600 contact_page_content %} {# Fragment #}
    Contact page header, this will create a cache file.
  {% endcache %}

  <!-- More HTML stuff -->
{% endblock content %}

In here, there's contact_page_header and contact_page_content. Both of these will have their own .djcache file.

I want to check for these keys (dynamically) whenever the model is saved, and delete contact_page_header.djcache and contact_page_content.djcache. I know I can do this specifically with cache.delete('contact_page_header') and cache.delete('contact_page_content').

I'd like to find a way to dynamically find the cache fragments from a template and delete them without delete all the other template cache, and without specifically specifying which cache files to delete because the template cache might change, and I want to use re-usable code in other views. Ideally, I'd like to write a Mixin that, on save, will check for cache fragments from any model (and its respective template) and delete just those cache files instead of writing a custom method for every Django view.

Kalob Taulien
  • 1,817
  • 17
  • 22

0 Answers0