66---------
77@author: Boris
88"""
9- import os
109import time
1110from typing import Union , List
1211
1312import redis
13+ from redis .cluster import ClusterNode , RedisCluster
1414from redis .connection import Encoder as _Encoder
1515from redis .exceptions import ConnectionError , TimeoutError
1616from redis .exceptions import DataError
@@ -140,29 +140,24 @@ def get_connect(self):
140140 startup_nodes = []
141141 for ip_port in ip_ports :
142142 ip , port = ip_port .split (":" )
143- startup_nodes .append ({ "host" : ip , " port" : port } )
143+ startup_nodes .append (ClusterNode ( ip , int ( port )) )
144144
145145 if self ._service_name :
146146 # log.debug("使用redis哨兵模式")
147- hosts = [(node [ " host" ] , node [ " port" ] ) for node in startup_nodes ]
147+ hosts = [(node . host , node . port ) for node in startup_nodes ]
148148 sentinel = Sentinel (hosts , socket_timeout = 3 , ** self ._kwargs )
149149 self ._redis = sentinel .master_for (
150150 self ._service_name ,
151151 password = self ._user_pass ,
152152 db = self ._db ,
153- redis_class = redis .StrictRedis ,
153+ redis_class = redis .Redis ,
154154 decode_responses = self ._decode_responses ,
155155 max_connections = self ._max_connections ,
156156 ** self ._kwargs ,
157157 )
158+ self ._is_redis_cluster = False
158159
159160 else :
160- try :
161- from rediscluster import RedisCluster
162- except ModuleNotFoundError as e :
163- log .error ('请安装 pip install "feapder[all]"' )
164- os ._exit (0 )
165-
166161 # log.debug("使用redis集群模式")
167162 self ._redis = RedisCluster (
168163 startup_nodes = startup_nodes ,
@@ -171,11 +166,10 @@ def get_connect(self):
171166 max_connections = self ._max_connections ,
172167 ** self ._kwargs ,
173168 )
174-
175- self ._is_redis_cluster = True
169+ self ._is_redis_cluster = True
176170 else :
177171 ip , port = ip_ports [0 ].split (":" )
178- self ._redis = redis .StrictRedis (
172+ self ._redis = redis .Redis (
179173 host = ip ,
180174 port = port ,
181175 db = self ._db ,
@@ -186,7 +180,7 @@ def get_connect(self):
186180 )
187181 self ._is_redis_cluster = False
188182 else :
189- self ._redis = redis .StrictRedis .from_url (
183+ self ._redis = redis .Redis .from_url (
190184 self ._url , decode_responses = self ._decode_responses , ** self ._kwargs
191185 )
192186 self ._is_redis_cluster = False
@@ -573,7 +567,8 @@ def zexists(self, table, values):
573567
574568 if isinstance (values , list ):
575569 pipe = self ._redis .pipeline ()
576- pipe .multi ()
570+ if not self ._is_redis_cluster :
571+ pipe .multi ()
577572 for value in values :
578573 pipe .zscore (table , value )
579574 is_exists_temp = pipe .execute ()
@@ -773,7 +768,8 @@ def setbit(
773768 else :
774769 assert len (offsets ) == len (values ), "offsets值要与values值一一对应"
775770 pipe = self ._redis .pipeline ()
776- pipe .multi ()
771+ if not self ._is_redis_cluster :
772+ pipe .multi ()
777773
778774 for offset , value in zip (offsets , values ):
779775 pipe .setbit (table , offset , value )
@@ -792,7 +788,8 @@ def getbit(self, table, offsets):
792788 """
793789 if isinstance (offsets , list ):
794790 pipe = self ._redis .pipeline ()
795- pipe .multi ()
791+ if not self ._is_redis_cluster :
792+ pipe .multi ()
796793 for offset in offsets :
797794 pipe .getbit (table , offset )
798795
0 commit comments