1.设置setting
REDIS_HOST = '10.133.3.26'
REDIS_POST = 6379
REDIS_DATABASE = 3
REDIS_PASSWORD = ''
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://{}:{}/{}".format(REDIS_HOST, REDIS_POST, REDIS_DATABASE),
'TIMEOUT': 3600 * 24 * 7200, # 过期时间(秒)
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"PASSWORD": REDIS_PASSWORD # 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):
# ...