Add HIT/MISS info to headers with Django cache middleware

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 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 django.views.decorators.cache) that will call our new class something like this:

Josip Lazić

Josip Lazić wrote 21 posts

Post navigation


Comments

  • Tripthi

    Hello Josip Lazić
    Could you please complete the remaining steps for the topic “Monitor HAProxy via PRTG”.

    “Next step would be to setup alerting based on queue length, server backend availability, and UP->DOWN events in HAProxy. I will try and do that in next few days. ”

    Waiting for your post on the above topic. Really appreciate your reply

    • Josip Lazić

      @Tripthi
      Unfortunately I no longer have access to the PRTG installation and am unable to complete that task.

      • Tripthi

        Thanks for your response Josip. Your PS script is cool. Instead of whitelisting IP for stats page authentication in HAProxy can we add stats page authentication in PS script ? If so could you let me know how can we achieve this.

  • Tripthi

    Thanks for your response. I got it done by myself and can set up alerts based on the backend status. However I’m looking for PS script to be modified with ha proxy stats page credentials instead of whitelisting IP in configuration file. Any tips ?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>