Home > Article > Backend Development > Two ways of Django caching in python (hard disk and redis)
The content of this article is about the two methods of Django caching in python (hard disk and redis). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
To set up cache, you can use the middleware method in CacheDemo, to set the client browser cache time; you can also use the following decorator method to cache individual things , such as: Function
Turn on caching: introduce the package from django.views.decorators.cache import cache_page in views and decorate it with @cache_page(5*60) on the function. The cache time of 300 seconds can be specified in brackets
Set cache storage location:
Cache to hard disk
CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', 'LOCATION': '/Users/LJB/Desktop', #缓存到硬盘(此处设置为保存缓存到桌面) } }
Cache to redis
CACHES = { #把缓存保存到Redis数据库 "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/1", #数字1为Redis数据库号, "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", "PASSWORD": "123456" } } }
Note: select 1 Switch Redis library keys * View all data auth 123456 Password login
The above is the detailed content of Two ways of Django caching in python (hard disk and redis). For more information, please follow other related articles on the PHP Chinese website!