1.设置setting
CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://10.133.3.34:6379/1", ‘TIMEOUT‘: 50, # 过期时间(秒) "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", # "PASSWORD": "123456" # Redis 密码 } } }
2.cache基本使用
from django.core.cache import cache 设置:cache.set(键,值,有效时间 秒) 获取:cache.get(键) 删除:cache.delete(键) 清空:cache.clear() from django.views.decorators.cache import cache_page @cache_page(60 * 15) def my_view(request, param): # ...