django使用redis缓存

遇到问题:开发过程中某一个功能模块加载速度慢且该功能所需数据更新频率低。

开发环境:window10,python2.7,django1.11.13

安装redis:redis安装链接:https//github.com/MSOpenTech/redis。找到Redis-x64-3.2.100.msi

                  django中安装pip install django-redis

Django中的中配置的Redis的: 

      setting.py配置:                   

CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': 'redis://127.0.0.1:6379',
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            # "PASSWORD": "密码",
        },
    },
}
REDIS_TIMEOUT=7*24*60*60
CUBES_REDIS_TIMEOUT=60*60
NEVER_REDIS_TIMEOUT=365*24*60*60

注:PASSWORD可有可无,如果需要参考设置https://www.cnblogs.com/GuoJunwen/p/9238624.html

   view.py使用:

#判断缓存中是否存在fund_strategies,如果存在使用get获取
if cache.has_key('fund_strategies'):
            message = cache.get('fund_strategies')
else:
    #...业务逻辑,message是存的字段
    cache.set('fund_strategies',message,24*60*60)

猜你喜欢

转载自blog.csdn.net/qq_769932247/article/details/83345270