How to list Django Template Cache keys?












4














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.










share|improve this question



























    4














    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.










    share|improve this question

























      4












      4








      4







      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.










      share|improve this question













      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.







      django caching django-cache






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 '18 at 18:34









      Kalob Taulien

      415411




      415411





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354277%2fhow-to-list-django-template-cache-keys%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354277%2fhow-to-list-django-template-cache-keys%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Berounka

          Fiat S.p.A.

          Type 'String' is not a subtype of type 'int' of 'index'