This article mainly introduces the View-level cache in the Django framework. Django is the most popular Pythonweb development framework, for more information about the cache framework, see cache the output of a single view. Django. views. decorators. cache defines a cache_page modifier that automatically caches View responses. It is easy to use:
From django. views. decorators. cache import cache_pagedef my_view (request): #... my_view = cache_page (my_view, 60*15)
You can also use the decorator syntax of Python2.4:
@ Cache_page (60*15) def my_view (request ):#...
Cache_page only accepts one parameter: cache timeout in seconds. In the response, the results of the "my_view ()" view will be cached for 15 minutes. (Note: to improve readability, this parameter is written as 60*15. 60*15 is calculated as 900, that is, 15 minutes multiplied by 60 seconds per minute .)
Like the site cache, the View Cache has nothing to do with the URL. If multiple URLs direct to the same view, each view is cached separately. Continue to the my_view example. if URLconf is as follows:
Urlpatterns = ('', (r' ^ foo/(\ d {1, 2})/$ ', my_view ),)
As expected, requests sent to/foo/1/and/foo/23/will be cached separately. However, once a specific request (such as/foo/23/) is sent, the request directed to the URL again will be cached.