A more granular way to use the caching framework is by caching the output of individual views. django.views.decorators.cache
defines a cache_page
decorator that will automatically cache the view's response for you. It's easy to use:
from django.views.decorators.cache import cache_page @cache_page(60 * 15) def my_view(request): ...
cache_page
takes a single argument: the cache timeout, in seconds. In the above example, the result of the my_view()
view will be cached for 15 minutes. (Note that I've written it as 60 * 15
for the purpose of readability. 60 * 15
will be evaluated to 900
-that is, 15 minutes multiplied by 60 seconds per minute.)
The per-view cache, like the per-site cache, is keyed off of the URL. If multiple URLs point at the same view, each URL will be cached separately. Continuing the my_view
example, if your URLconf looks like this:
urlpatterns = [ url(r'^foo/([0-9]{1,2})/$', my_view), ]
then requests...