Welcome to the world django-fancy-cache!

01 March 2013   3 comments   Python, Django


Powered by Fusion×

A Django cache_page on steroids

Django ships with an awesome view decorator called cache_page which is awesome. But a bit basic too.

What it does is that it stores the whole view response in memcache and the key to it is the URL it was called with including any query string. All you have to do is specify the length of the cache timeout and it just works.
Now, it's got some shortcomings which django-fancy-cache upgrades. These "steroids" are:

  1. Ability to override the key prefix with a callable.
  2. Ability to remember every URL that was cached so you can do invalidation by a URL pattern.
  3. Ability to modify the response before it's stored in the cache.
  4. Ability to ignore certain query string parameters that don't actually affect the view but does yield a different cache key.
  5. Ability to serve from cache but always do one last modification to the response.
  6. Incrementing counter of every hit and miss to satisfy your statistical curiosity needs.

The documentation is here:

You can see it in a real world implementation by seeing how it's used on my blog here. You basically use it like this::

from fancy_cache import cache_page

@cache_page(60 * 60)
def myview(request):
    return render(request, 'template.html', stuff)

What I'm doing with it here on my blog is that I make the full use of caching on each blog post but as soon as a new comment is posted, I wipe the cache by basically creating a new key prefix. That means that pages are never cache stale but the views never have to generate the same content more than once.

I'm also using django-fancy-cache to do some optimizations on the output before it's stored in cache.


Henri Witteveen
I was quite thrilled to find your django-fancy-cache solution and I included django-fancy-cache in my view file. I am using to add a datetime key_prefix to the cache_key. My view function also expects a few kwargs:

from fancy_cache import cache_page

def latest_business_entry(request, *args, **kwargs):
return return_date_string

@cache_page(settings.SHOW_ALL_BRANCH_CACHE_SECONDS, key_prefix=latest_business_entry)
def show_all(request, template, inet_code=None, language_code=None)

However in the function latest_business_entry I am getting an "KeyError" error. Now it appears the kwargs dict is empty so it seems cache_page is ignoring kwargs. Is this true or am I doing something wrong?
Peter Bengtsson
Hmm.. That could be a bug. We should fix that.

Just to confirm, you're saying that `template, inet_code=None, language_code=None` etc is not passed into latest_business_entry()?

Your email will never ever be published

Related posts

mincss version 0.8 is much much faster 27 February 2013
This site is now 100% inline CSS and no bytes are wasted 05 March 2013
Related by keywords:
Newfound love of @staticmethod in Python 02 July 2012
Secs sell! How I cache my entire pages (server-side) 10 May 2012
'Cache-Control' or Expires 16 September 2005
A great thing about Squid: Calamaris 04 July 2008
This site is now 100% inline CSS and no bytes are wasted 05 March 2013
django-fancy-cache with or without stats 11 March 2013
Custom CacheMiddleware that tells Javascript a page is cached in Django 24 August 2009
Spellcorrector 0.2 24 September 2007
ESI and Squid and Zope 17 December 2003
Squid now working in front of my Zope 30 July 2003
This site 7 months ago 19 February 2004