Cache middleware. If enabled, each Django-powered page will be cached based on URL. The canonical way to enable cache middleware is to set UpdateCacheMiddleware as your first piece of middleware, and FetchFromCacheMiddleware as the last:
MIDDLEWARE = [ 'django.middleware.cache.UpdateCacheMiddleware', ... 'django.middleware.cache.FetchFromCacheMiddleware' ]
This is counter-intuitive, but correct: UpdateCacheMiddleware needs to run last during the response phase, which processes middleware bottom-up; FetchFromCacheMiddleware needs to run last during the request phase, which processes middleware top-down.
The single-class CacheMiddleware can be used for some simple sites. However, if any other piece of middleware needs to affect the cache key, you'll need to use the two-part UpdateCacheMiddleware and FetchFromCacheMiddleware. This'll most often happen when you're using Django's LocaleMiddleware.
More details about how the caching works:
Class | FetchFromCacheMiddleware |
Request-phase cache middleware that fetches a page from the cache. |
Class | UpdateCacheMiddleware |
Response-phase cache middleware that updates the cache if the response is cacheable. |
Class | CacheMiddleware |
Cache middleware that provides basic behavior for many simple sites. |