Support using a common redis connection
If sentinel_host or sentinel_cluster_id is not provided it will try to connect with a redis_host parameter
This commit is contained in:
committed by
Mario de Frutos
parent
557f67c51f
commit
93463b3032
@@ -1,4 +1,5 @@
|
||||
from redis.sentinel import Sentinel
|
||||
from redis import StrictRedis
|
||||
|
||||
|
||||
class RedisConnection:
|
||||
@@ -6,24 +7,30 @@ class RedisConnection:
|
||||
REDIS_DEFAULT_USER_DB = 5
|
||||
REDIS_DEFAULT_TIMEOUT = 2 #seconds
|
||||
REDIS_SENTINEL_DEFAULT_PORT = 26379
|
||||
REDIS_DEFAULT_PORT = 6379
|
||||
|
||||
def __init__(self, sentinel_host, sentinel_port, sentinel_master_id,
|
||||
redis_db=REDIS_DEFAULT_USER_DB, **kwargs):
|
||||
redis_host, redis_db=REDIS_DEFAULT_USER_DB, **kwargs):
|
||||
self.sentinel_host = sentinel_host
|
||||
self.sentinel_port = sentinel_port
|
||||
self.sentinel_master_id = sentinel_master_id
|
||||
self.redis_host = redis_host
|
||||
self.timeout = kwargs['timeout'] if 'timeout' in kwargs else self.REDIS_DEFAULT_TIMEOUT
|
||||
self.redis_db = redis_db
|
||||
self.redis_port = self.REDIS_DEFAULT_PORT
|
||||
|
||||
def redis_connection(self):
|
||||
return self.__create_redis_connection()
|
||||
|
||||
def __create_redis_connection(self):
|
||||
sentinel = Sentinel([(self.sentinel_host,
|
||||
self.REDIS_SENTINEL_DEFAULT_PORT)],
|
||||
socket_timeout=self.timeout)
|
||||
return sentinel.master_for(
|
||||
self.sentinel_master_id,
|
||||
socket_timeout=self.timeout,
|
||||
db=self.redis_db
|
||||
)
|
||||
if (self.sentinel_host == None) or (self.sentinel_master_id == None):
|
||||
return StrictRedis(host=self.redis_host, port=self.redis_port, db=self.redis_db)
|
||||
else:
|
||||
sentinel = Sentinel([(self.sentinel_host,
|
||||
self.REDIS_SENTINEL_DEFAULT_PORT)],
|
||||
socket_timeout=self.timeout)
|
||||
return sentinel.master_for(
|
||||
self.sentinel_master_id,
|
||||
socket_timeout=self.timeout,
|
||||
db=self.redis_db
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user