Caching

From Django In Production

Jump to: navigation, search

The Django documentation provides the best information on how to deal with caching on your site.

Once you have enabled one of the cache backend as described in the documentation, there are several approaches to reaping the benefits of it.

Contents

Sitewide caching

Using the caching middleware is the bluntest approach to getting some cache benefits. It's mostly useful in fairly simple scenarios where you have a high number of unauthenticated users hitting a query heavy page.

The per view cache

This can be useful when the sitewide caching is too heavy-handed and you want to be more explicit about which views get cached and which do not.

Template fragment caching

For even more granular control, you can declare blocks of your template to be cached. This is nice if you have performance intensive custom template tags that are used on many pages and for many users that make database queries.

Directly on the cache API

For the finest granularity you can access Django's caching backend directly using:

   cache.set("key", "value")
   cache.get("key")

Cache Model Managers

Personal tools