First, as usual, to explain the role of Django caching and cache mode can be used:
The role of the cache is again loaded for data items within a set period of time may be stress-free access or refresh the data again
- Way a database cache (Django native --- have code hints)
- Second way (Django non-native) redis cache ----------------------------------------- -windows redis under the recommended configuration of installation blog ----- " https://blog.csdn.net/AkiraNicky/article/details/82533316
- Multi-level cache (Django built to handle multiple types of caches exist in the way)
A, django several cache configuration
Of which there are several points to note configuration,
- Cache Cache The default is default (where names can take their own, just like the following redis)
- Each method has its LOCATION field, called the storage place, default is cached database table names, redis_backend for No. 1 packet redis in the library, write a query with respect to management (as if into more like folders)
- Cache in Django each storage, cache mode and has decorative custom cache way, also explains, Django only need to change the configuration information on ok, view is no need to change, very convenient
Second, the application view view cache layer (see details comment)
Here is a decorator
@cache_page (timeout = 30) is fixed format code hints (encapsulation method in nature) ------------ also described, a method in which two is based on a custom ---- ------- (where the two methods can only take one of them, so I commented out code decorator)
# ------- ------------ first method ******* database cache instance, for example pretend to be time-consuming page # one way: --- ------------ 1. import decorators (essentially a determination method of packaging), in order to use the cache @cache_page # @cache_page (timeout = 30) DEF News (Request): # Second way: -------------- Custom Method 2 # With the function name means all access to the common set of routing cache, cache (second step) Cache = Caches [ ' redis_backend ' ] # ------------------- -------------- two lines at the same time there is no conflict, using a multi-level cache (redis caching and custom cache) Result = cache.get ( " News " ) corresponding to # -------------------------------------- provision of the relevant parameters set below, here is calling # If there IF the Result: return HttpResponse (the Result) new_list=[] for i in range(10): new_list.append ( " recently began a trade war d% " % i) # Disguise after consuming ---------------------- use caching sleep (5) # five seconds data={ 'news_list':new_list, } Response = the render (Request, ' news.html web ' , context = Data) # Second way (Step :): set the cache --- key = 'news', the page value = content, the expiration time of 60 seconds cache.set ( " News " , response.content, timeout = 60 ) # ----------------------------------- care get above return response
After the run time, set for 60 seconds without pressure can refresh
Third, the decorator of multi-level cache (which is used in setting the configuration)
#cache_page()配置多级缓存--------cache="指定缓存的位置,写上面的redis_backend,则作相应的变换" # default 为默认配置中 指定的数据库缓存方式 @cache_page(timeout=60,cache='default') def jokes(request): sleep(5) return HttpResponse("jokes_list")
从这里我们可以看出 标红的说明名字可以随便取,无所谓
如果redis装不好或者启动不了可以先用数据库学习一下