環境需要:
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 |
|
conn對象基本上擁有所有的redis命令。
使用方法爲conn.redis命令(參數...)