Previous Page Next Page

Implementing a Per-View Cache

Django's caching makes it possible to implement the cache at the view level as well. Instead of caching every page in the website, you might want to cache only a few specific views.

Use the django.views.decorators.cache.cache_page decorator function to implement caching for a specific view. The cache_page decorator function caches the web page generated by a view function. The cache_page decorator accepts one argument that specifies how many seconds to keep the web page cached.

The following code shows an example of implementing the cache_page decorator function to cache the web page generated by myView for 3 minutes:

@cache_page(180)
def myView(request):
    . . .

By the Way

The cache_page decorator keys of the URL are just like the CacheMiddleware framework. Different URLs that point to the same view are cached as different entries.


Previous Page Next Page