1
1
import logging
2
- from typing import Optional
2
+ from typing import Optional , Type , Union
3
3
4
4
import redis
5
5
9
9
logger = logging .getLogger (__name__ )
10
10
11
11
12
- class RedisStandalone (BaseConnectionSync ):
12
+ class RedisConnection (BaseConnectionSync ):
13
13
def __init__ (
14
14
self ,
15
+ client : Type [Union [redis .Redis , redis .RedisCluster ]],
15
16
host : Optional [str ] = None ,
16
17
port : Optional [int ] = None ,
17
18
username : Optional [str ] = None ,
18
19
password : Optional [str ] = None ,
19
20
db_index : Optional [int ] = None ,
20
21
url : Optional [str ] = None ,
22
+ ** extra_options ,
21
23
) -> None :
22
- """
23
- Initialize the Redis standalone client
24
- Parameters
25
- ----------
26
- host: str
27
- Name of the host to connect to Redis instance/cluster
28
- port: int
29
- Number of the port to connect to Redis instance/cluster
30
- username: str
31
- Name of the username to connect to Redis instance/cluster in case of using ACL
32
- See: https://redis.io/docs/management/security/acl/
33
- password: str
34
- Passwod to connect to Redis instance/cluster
35
- db_index: int
36
- Index of Redis database
37
- See: https://redis.io/commands/select/
38
- url: str
39
- Redis client object configured from the given URL
40
- See: https://redis.readthedocs.io/en/latest/connections.html#redis.Redis.from_url
41
- """
24
+ self .extra_options : dict = {}
42
25
43
26
self .url = url
44
27
self .host = host
45
28
self .port = port
46
29
self .username = username
47
30
self .password = password
48
31
self .db_index = db_index
32
+ self .extra_options .update (** extra_options )
49
33
self ._connection = None
34
+ self ._client = client
50
35
51
36
def init_connection (self ):
52
37
"""
@@ -55,36 +40,40 @@ def init_connection(self):
55
40
if self ._connection :
56
41
return self ._connection
57
42
58
- logger .info (f"Trying to connect to Redis Host/Cluster : { self .host } " )
43
+ logger .info (f"Trying to connect to Redis: { self .host } " )
59
44
60
45
try :
61
46
if self .url :
62
47
logger .debug (f"Using URL format to connect to Redis: { self .host } " )
63
- self ._connection = redis . Redis .from_url (url = self .url )
48
+ self ._connection = self . _client .from_url (url = self .url )
64
49
else :
65
50
logger .debug (f"Using other parameters to connect to Redis: { self .host } " )
66
- self ._connection = redis . Redis (
51
+ self ._connection = self . _client (
67
52
host = self .host ,
68
53
port = self .port ,
69
54
username = self .username ,
70
55
password = self .password ,
71
56
db = self .db_index ,
72
57
decode_responses = True ,
58
+ ** self .extra_options ,
73
59
)
74
60
except redis .exceptions .ConnectionError as exc :
75
- logger .debug (f"Cannot connect in Redis Host : { self .host } " )
76
- raise RedisConnectionError ("Could not to connect to Redis Standalone " , exc ) from exc
61
+ logger .debug (f"Cannot connect in Redis: { self .host } " )
62
+ raise RedisConnectionError ("Could not to connect to Redis" , exc ) from exc
77
63
78
64
return self ._connection
79
65
80
66
81
- class RedisCluster ( BaseConnectionSync ):
67
+ class RedisStandalone ( RedisConnection ):
82
68
def __init__ (
83
69
self ,
84
70
host : Optional [str ] = None ,
85
71
port : Optional [int ] = None ,
86
- read_from_replicas : Optional [bool ] = False ,
72
+ username : Optional [str ] = None ,
73
+ password : Optional [str ] = None ,
74
+ db_index : Optional [int ] = None ,
87
75
url : Optional [str ] = None ,
76
+ ** extra_options ,
88
77
) -> None :
89
78
"""
90
79
Initialize the Redis standalone client
@@ -106,36 +95,40 @@ def __init__(
106
95
Redis client object configured from the given URL
107
96
See: https://redis.readthedocs.io/en/latest/connections.html#redis.Redis.from_url
108
97
"""
98
+ print (extra_options )
99
+ super ().__init__ (redis .Redis , host , port , username , password , db_index , url , ** extra_options )
109
100
110
- self .url = url
111
- self .host = host
112
- self .port = port
113
- self .read_from_replicas = read_from_replicas
114
- self ._connection = None
115
101
116
- def init_connection (self ):
102
+ class RedisCluster (RedisConnection ):
103
+ def __init__ (
104
+ self ,
105
+ host : Optional [str ] = None ,
106
+ port : Optional [int ] = None ,
107
+ username : Optional [str ] = None ,
108
+ password : Optional [str ] = None ,
109
+ db_index : Optional [int ] = None ,
110
+ url : Optional [str ] = None ,
111
+ ** extra_options ,
112
+ ) -> None :
117
113
"""
118
- Connection is cached, so returning this
114
+ Initialize the Redis standalone client
115
+ Parameters
116
+ ----------
117
+ host: str
118
+ Name of the host to connect to Redis instance/cluster
119
+ port: int
120
+ Number of the port to connect to Redis instance/cluster
121
+ username: str
122
+ Name of the username to connect to Redis instance/cluster in case of using ACL
123
+ See: https://redis.io/docs/management/security/acl/
124
+ password: str
125
+ Passwod to connect to Redis instance/cluster
126
+ db_index: int
127
+ Index of Redis database
128
+ See: https://redis.io/commands/select/
129
+ url: str
130
+ Redis client object configured from the given URL
131
+ See: https://redis.readthedocs.io/en/latest/connections.html#redis.Redis.from_url
119
132
"""
120
- if self ._connection :
121
- return self ._connection
122
133
123
- logger .info (f"Trying to connect to Redis Cluster: { self .host } " )
124
-
125
- try :
126
- if self .url :
127
- logger .debug (f"Using URL format to connect to Redis Cluster: { self .host } " )
128
- self ._connection = redis .Redis .from_url (url = self .url )
129
- else :
130
- logger .debug (f"Using other parameters to connect to Redis Cluster: { self .host } " )
131
- self ._connection = redis .cluster .RedisCluster (
132
- host = self .host ,
133
- port = self .port ,
134
- server_type = None ,
135
- read_from_replicas = self .read_from_replicas ,
136
- )
137
- except redis .exceptions .ConnectionError as exc :
138
- logger .debug (f"Cannot connect in Redis Cluster: { self .host } " )
139
- raise RedisConnectionError ("Could not to connect to Redis Cluster" , exc ) from exc
140
-
141
- return self ._connection
134
+ super ().__init__ (redis .cluster .RedisCluster , host , port , username , password , db_index , url , ** extra_options )
0 commit comments