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命令(參數...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM