本篇文章给大家带来的内容是关于python中django缓存的两种方式(硬盘和redis),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
设置缓存可采用cachedemo中的中间件方法,对客户端浏览器缓存时间进行设定;也可采用下面的装饰器方法,可对单独的东西进行缓存,如:函数
开启缓存:在views中引入包 from django.views.decorators.cache import cache_page 装饰在函数上@cache_page(5*60)即可,括号中可指定缓存时间300秒
设置缓存存放位置:
缓存到硬盘
caches = { 'default': { 'backend': 'django.core.cache.backends.filebased.filebasedcache', 'location': '/users/ljb/desktop', #缓存到硬盘(此处设置为保存缓存到桌面) }}
缓存到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" } }}
注:select 1 切换redis库 keys * 查看所有数据 auth 123456 密码登录
以上就是python中django缓存的两种方式(硬盘和redis)的详细内容。