码迷,mamicode.com
首页 > 其他好文 > 详细

Django——缓存

时间:2018-09-18 20:42:24      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:select   demo   top   html   margin   width   127.0.0.1   rom   sed   

设置缓存可采用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  密码登录

Django——缓存

标签:select   demo   top   html   margin   width   127.0.0.1   rom   sed   

原文地址:https://www.cnblogs.com/siplips/p/9670966.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!