The Django cache middleware is great, but has one drawback. If you are caching views (which can give a nice performance boost) Django will only use the path segment of the URL to create a cache key. If you are an avid reader of RFC 3986 you may remember that a URI consists of multiple components; path and query being of special interest here. The problem is documented in ticket 4992 (Update: it is now in Django).

Given the following URL:

http://example.com/items/?order_by=name

…Django will ignore the query part when determining a cache key so the key for the above request will be:

/items/

Any type of query parameter will make Django ignore the cached page. Django will not create a new cache item for the order_by request. This will be detrimental for the performance of your site. I had expected Django to create three different cache items for these URLs instead of one cache item and two ignored requests:

The solution

Fortunately Django’s source is very readable and easy to adapt to your own needs. A few minor changes to middleware/cache.py and utils/cache.py and you are ready to go. For details see the patch I attached to ticket 4992.

I recently deployed a Django site to get an overview of VPS hosting plans and the ability to cache items based on the filtering parameters gives much better performance.