A custom connection pool
The python as used in connection pool (singleton object)
Note : Each view function must have
conn = redis.Redis(connection_pool=POOL)
Second, the use of third-party modules (django-redis)
1, the installation
pip3 install django-redis
2, set the file setting.py
CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://ip:6379", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", "CONNECTION_POOL_KWARGS": {"max_connections": 100}, "PASSWORD " : " Password " , } } }
3, using
def index(request): # default setting的配置文件 con = get_redis_connection('default') con.set('k1', 'v1') print(con.get('k1')) # b'v1' return HttpResponse('设置ok')
Third, the advanced use
1, using the full stack (middleware)
MIDDLEWARE = [ 'django.middleware.cache.UpdateCacheMiddleware', 其它中间件, 'django.middleware.cache.FetchFromCacheMiddleware', ]
2, using separate views, precedence over the global view
from django.views.decorators.cache import cache_page @cache_page(60 * 15) def my_view(request): ...
3, template topical use
a. introducing templatetag { % Load Cache% } B. caching using { % cache Cache 5000% Key } cached content { %}% endCache