Django Redis

安装

pip install django-redis

配置

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"

 

Python与redis集群交互

from rediscluster import StrictRedisCluster
 
if __name__ == '__main__':
    try:
        # redis集群中的所有主结点(所有master结点)
        startup_nodes = [
            {'host': '172.16.179.131', 'port': '7000'},
            {'host': '172.16.179.142', 'port': '7003'},
            {'host': '172.16.179.131', 'port': '7001'},
        ]
        # 构建StrictRedisCluster对象
        src = StrictRedisCluster(startup_nodes=startup_nodes,decode_responses=True)
        # 设置键为name、值为张三的数据
        result = src.set('name','张三')  # 返回Boolean。 与单机版redis的操作一致。
        print(result)  # True
        # 获取键为name
        name = src.get('name')
        print(name)
    except Exception as e:
        print(e)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



posted @ 2019-12-29 21:38  驰念  阅读(114)  评论(0编辑  收藏  举报