Fixing Site-Wide Caching in Django

Posted on Sept. 17, 2012, 8:30 p.m.

Caching in Django is elegant and simple, especially the site-wide cache for anonymous users. But if you use Google Analytics and other external cookies, it might not be working the way you intended. Read this tutorial to find out how to make your site as fast as it can be.

By enabling the site-wide cache in Django, you probably intended for pages every page to be cached for every anonymous users. That's the point, at least. The problem is, the page cache is set to Vary: Cookie. That means that if you have a session cookie or anything of the like, the cache will be different for each user.

Learning Django? Subscribe to my Django articles, tips and tutorials.

Fortunately, that's not a problem with Django itself, since anonymous users won't have cookies set. But it is a problem if you're using Google Analytics. That's because Google Analytics sets cookies that trigger the site-wide cache's Vary: Cookie condition. Luckily, fixing this is as easy as using a small piece of middleware and a touch of inheritance.

Let's create a new middleware file, call it and place it in your project root. Add the following snippet to the file1:

from django.middleware.cache import UpdateCacheMiddleware

import re

class SmartUpdateCacheMiddleware(UpdateCacheMiddleware):                        
    STRIP_RE = re.compile(r'\b(_[^=]+=.+?(?:; |$))')

    def process_request(self, request):                                         
        cookie = self.STRIP_RE.sub('', request.META.get('HTTP_COOKIE', ''))   
        request.META['HTTP_COOKIE'] = cookie

So what does this do? It replaces the middleware that comes with Django. Before passing on the cookies to the rest of the application, it strips out the cookies set by Google Analytics. That way, the Vary: Cookie condition won't be triggered, as your application won't see any of these cookies. To deploy these changes, simply find your middleware classes in, and replace django.middleware.cache.UpdateCacheMiddleware with middleware.SmartUpdateCacheMiddleware. Your middleware settings should look like so:

MIDDLEWARE_CLASSES = (                                                        
    # All your other middleware.                                              

And that's it. You're done.

  1. Many thanks to the people over at for the middleware. 

Tags: caching Django