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:
- Ability to override the key prefix with a callable.
- Ability to remember every URL that was cached so you can do invalidation by a URL pattern.
- Ability to modify the response before it's stored in the cache.
- Ability to ignore certain query string parameters that don't actually affect the view but does yield a different cache key.
- Ability to serve from cache but always do one last modification to the response.
- 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)
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.
- 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 category:
- How much faster is Redis at storing a blob of JSON compared to PostgreSQL?
28 September 2019
- Best practice with retries with requests
19 April 2017
- Fastest way to find out if a file exists in S3 (with boto3)
16 June 2017
- Interesting float/int casting in Python
25 April 2006
- Fastest way to unzip a zip file in Python
31 January 2018
- Related by keyword:
- Fastest Redis configuration for Django
11 May 2017
- Really simple Django view function timer decorator
08 December 2017
- cache_memoize - a pretty decent cache decorator for Django
11 September 2017
- How to use django-cache-memoize
03 November 2017
- A good Django view function cache decorator for http.JsonResponse
20 June 2018