django2.0.6 连接使用redis集群

环境需要:

  django >= 1.8.x

  python 2.7 或者python >= 3.4

安装django-cluster-redis包:

  pip install django-redis  # 注意 django-redis版本需要 >= 4.7.0 

  pip install django-cluster-redis

在django项目中的settings文件中:

CACHES = {
  'default': {
    'BACKEND': 'django_redis.cache.RedisCache',
    'LOCATION': [
    'redis节点列表',
  ], # 格式为 redis://IP:PORT/db_index,数据库编号可为空,默认为0号 'OPTIONS': { 'REDIS_CLIENT_CLASS': 'rediscluster.RedisCluster', 'CONNECTION_POOL_CLASS': 'rediscluster.connection.ClusterConnectionPool', 'CONNECTION_POOL_KWARGS': { 'skip_full_coverage_check': True # AWS ElasticCache has disabled CONFIG commands } } } }

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

在view中使用方法:

from django_redis import get_redis_connection


conn = get_redis_connection()
conn.hgetall('key')
....

conn对象基本上拥有所有的redis命令。

使用方法为conn.redis命令(参数...)

posted @ 2019-03-24 10:58  September·  阅读(2541)  评论(0编辑  收藏  举报