django缓存借助redis数据库,需要下载redis数据库
下载redis数据库后,在命令行进入到redis目录执行redis-server.exe redis.conf
redis-server.exe redis.conf
出现下面这个则是启动成功
安装
安装hiredis使用pip install hiredis,注意python需要3.9以上版本,django4.0版本
pip install hiredis
配置
在setting.py文件中配置
CACHES = {
'default':{
'BACKEND':'django.core.cache.backends.redis.RedisCache',
'LOCATION':'redis://:asd2013@127.0.0.1:6379', #asd2013为redis密码默认没有
'OPTIONS':{ 'db':'1',
'parser_class':'redis.connection.HiredisParser',
'pool_class':'redis.ConnectionPool',
}
}
}
在Views.py文件中导入缓存
form django.core.cache import cache
设置缓存
cache.set(key,value,有效时长)
获取缓存
cache.get(key)