当前位置: 移动技术网 > IT编程>脚本编程>Python > Django——缓存

Django——缓存

2018年09月19日  | 移动技术网IT编程  | 我要评论

花红小黑膏,神墓电视剧演员表,辽宁省公安厅厅长

设置缓存可采用cachedemo中的中间件方法(https://www.cnblogs.com/siplips/p/9618034.html),对客户端浏览器缓存时间进行设定;也可采用下面的装饰器方法,可对单独的东西进行缓存,如:函数

  • 开启缓存:在views中引入包 from django.views.decorators.cache import cache_page 装饰在函数上@cache_page(5*60)即可,括号中可指定缓存时间300秒
  • 设置缓存存放位置:
    • 缓存到硬盘
      1 caches = {
      2     'default': {
      3         'backend': 'django.core.cache.backends.filebased.filebasedcache',
      4         'location': '/users/ljb/desktop',   #缓存到硬盘(此处设置为保存缓存到桌面)
      5     }
      6 }

       

    • 缓存到redis
       1 caches = {          #把缓存保存到redis数据库
       2     "default": {
       3         "backend": "django_redis.cache.rediscache",
       4         "location": "redis://127.0.0.1:6379/1",   #数字1为redis数据库号,
       5         "options": {
       6             "client_class": "django_redis.client.defaultclient",
       7             "password": "123456"
       8         }
       9     }
      10 }

      注:select 1  切换redis库    keys * 查看所有数据   auth 123456  密码登录

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网