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中使用方法:

1

2

3

4

5

6

from django_redis import get_redis_connection

 

 

conn = get_redis_connection()

conn.hgetall('key')

....

conn對象基本上擁有所有的redis命令。

使用方法爲conn.redis命令(參數...)

來源:https://www.cnblogs.com/liang3044/p/10587326.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章