11
11
from redis ._parsers import CommandsParser , Encoder
12
12
from redis ._parsers .helpers import parse_scan
13
13
from redis .backoff import default_backoff
14
- from redis .cache import CacheMixin
15
14
from redis .client import CaseInsensitiveDict , PubSub , Redis
16
15
from redis .commands import READ_COMMANDS , RedisClusterCommands
17
16
from redis .commands .helpers import list_or_args
@@ -446,7 +445,7 @@ def replace_default_node(self, target_node: "ClusterNode" = None) -> None:
446
445
self .nodes_manager .default_node = random .choice (replicas )
447
446
448
447
449
- class RedisCluster (AbstractRedisCluster , RedisClusterCommands , CacheMixin ):
448
+ class RedisCluster (AbstractRedisCluster , RedisClusterCommands ):
450
449
@classmethod
451
450
def from_url (cls , url , ** kwargs ):
452
451
"""
@@ -504,7 +503,10 @@ def __init__(
504
503
dynamic_startup_nodes : bool = True ,
505
504
url : Optional [str ] = None ,
506
505
address_remap : Optional [Callable [[Tuple [str , int ]], Tuple [str , int ]]] = None ,
507
- use_cache : Optional [bool ] = False ,
506
+ use_cache : bool = False ,
507
+ cache : Optional [Cache ] = None ,
508
+ cache_size : int = 128 ,
509
+ cache_ttl : int = 300 ,
508
510
** kwargs ,
509
511
):
510
512
"""
@@ -628,6 +630,10 @@ def __init__(
628
630
kwargs .get ("encoding_errors" , "strict" ),
629
631
kwargs .get ("decode_responses" , False ),
630
632
)
633
+ protocol = kwargs .get ("protocol" , None )
634
+ if use_cache and protocol not in [3 , "3" ]:
635
+ raise RedisError ("Client caching is only supported with RESP version 3" )
636
+
631
637
self .cluster_error_retry_attempts = cluster_error_retry_attempts
632
638
self .command_flags = self .__class__ .COMMAND_FLAGS .copy ()
633
639
self .node_flags = self .__class__ .NODE_FLAGS .copy ()
@@ -641,6 +647,9 @@ def __init__(
641
647
dynamic_startup_nodes = dynamic_startup_nodes ,
642
648
address_remap = address_remap ,
643
649
use_cache = use_cache ,
650
+ cache = cache ,
651
+ cache_size = cache_size ,
652
+ cache_ttl = cache_ttl ,
644
653
** kwargs ,
645
654
)
646
655
@@ -649,11 +658,6 @@ def __init__(
649
658
)
650
659
self .result_callbacks = CaseInsensitiveDict (self .__class__ .RESULT_CALLBACKS )
651
660
652
- protocol = kwargs .get ("protocol" , None )
653
- if use_cache and protocol not in [3 , "3" ]:
654
- raise RedisError ("Client caching is only supported with RESP version 3" )
655
- CacheMixin .__init__ (self , use_cache , None )
656
-
657
661
self .commands_parser = CommandsParser (self )
658
662
self ._lock = threading .Lock ()
659
663
@@ -1057,8 +1061,6 @@ def _parse_target_nodes(self, target_nodes):
1057
1061
return nodes
1058
1062
1059
1063
def execute_command (self , * args , ** options ):
1060
- if self .use_cache :
1061
- return self .cached_call (self ._execute_command , * args , ** options )
1062
1064
return self ._internal_execute_command (* args , ** options )
1063
1065
1064
1066
def _internal_execute_command (self , * args , ** kwargs ):
@@ -1163,7 +1165,7 @@ def _execute_command(self, target_node, *args, **kwargs):
1163
1165
connection .send_command ("ASKING" )
1164
1166
redis_node .parse_response (connection , "ASKING" , ** kwargs )
1165
1167
asking = False
1166
- connection .send_command (* args )
1168
+ connection .send_command (* args , ** kwargs )
1167
1169
response = redis_node .parse_response (connection , command , ** kwargs )
1168
1170
if command in self .cluster_response_callbacks :
1169
1171
response = self .cluster_response_callbacks [command ](
@@ -1317,7 +1319,7 @@ def reset(self) -> None:
1317
1319
self .primary_to_idx .clear ()
1318
1320
1319
1321
1320
- class NodesManager (CacheMixin ):
1322
+ class NodesManager ():
1321
1323
def __init__ (
1322
1324
self ,
1323
1325
startup_nodes ,
@@ -1327,8 +1329,10 @@ def __init__(
1327
1329
dynamic_startup_nodes = True ,
1328
1330
connection_pool_class = ConnectionPool ,
1329
1331
address_remap : Optional [Callable [[Tuple [str , int ]], Tuple [str , int ]]] = None ,
1330
- use_cache : Optional [ bool ] = False ,
1332
+ use_cache : bool = False ,
1331
1333
cache : Optional [Cache ] = None ,
1334
+ cache_size : int = 128 ,
1335
+ cache_ttl : int = 300 ,
1332
1336
** kwargs ,
1333
1337
):
1334
1338
self .nodes_cache = {}
@@ -1342,13 +1346,15 @@ def __init__(
1342
1346
self .connection_pool_class = connection_pool_class
1343
1347
self .address_remap = address_remap
1344
1348
self .use_cache = use_cache
1349
+ self .cache = cache
1350
+ self .cache_size = cache_size
1351
+ self .cache_ttl = cache_ttl
1345
1352
self ._moved_exception = None
1346
1353
self .connection_kwargs = kwargs
1347
1354
self .read_load_balancer = LoadBalancer ()
1348
1355
if lock is None :
1349
1356
lock = threading .Lock ()
1350
1357
self ._lock = lock
1351
- CacheMixin .__init__ (self , use_cache , None , cache )
1352
1358
self .initialize ()
1353
1359
1354
1360
def get_node (self , host = None , port = None , node_name = None ):
@@ -1486,9 +1492,21 @@ def create_redis_node(self, host, port, **kwargs):
1486
1492
# Create a redis node with a costumed connection pool
1487
1493
kwargs .update ({"host" : host })
1488
1494
kwargs .update ({"port" : port })
1489
- r = Redis (connection_pool = self .connection_pool_class (** kwargs ), use_cache = self .use_cache , cache = self .cache )
1495
+ kwargs .update ({"use_cache" : self .use_cache })
1496
+ kwargs .update ({"cache" : self .cache })
1497
+ kwargs .update ({"cache_size" : self .cache_size })
1498
+ kwargs .update ({"cache_ttl" : self .cache_ttl })
1499
+ r = Redis (connection_pool = self .connection_pool_class (** kwargs ))
1490
1500
else :
1491
- r = Redis (host = host , port = port , use_cache = self .use_cache , cache = self .cache , ** kwargs )
1501
+ r = Redis (
1502
+ host = host ,
1503
+ port = port ,
1504
+ use_cache = self .use_cache ,
1505
+ cache = self .cache ,
1506
+ cache_size = self .cache_size ,
1507
+ cache_ttl = self .cache_ttl ,
1508
+ ** kwargs ,
1509
+ )
1492
1510
return r
1493
1511
1494
1512
def _get_or_create_cluster_node (self , host , port , role , tmp_nodes_cache ):
0 commit comments