9
9
from redis ._parsers import CommandsParser , Encoder
10
10
from redis ._parsers .helpers import parse_scan
11
11
from redis .backoff import default_backoff
12
- from redis .cache import CacheConfig , CacheInterface
12
+ from redis .cache import CacheConfig , CacheInterface , CacheFactoryInterface , CacheFactory
13
13
from redis .client import CaseInsensitiveDict , PubSub , Redis
14
14
from redis .commands import READ_COMMANDS , RedisClusterCommands
15
15
from redis .commands .helpers import list_or_args
@@ -1327,6 +1327,7 @@ def __init__(
1327
1327
address_remap : Optional [Callable [[Tuple [str , int ]], Tuple [str , int ]]] = None ,
1328
1328
cache : Optional [CacheInterface ] = None ,
1329
1329
cache_config : Optional [CacheConfig ] = None ,
1330
+ cache_factory : Optional [CacheFactoryInterface ] = None ,
1330
1331
** kwargs ,
1331
1332
):
1332
1333
self .nodes_cache = {}
@@ -1339,8 +1340,9 @@ def __init__(
1339
1340
self ._dynamic_startup_nodes = dynamic_startup_nodes
1340
1341
self .connection_pool_class = connection_pool_class
1341
1342
self .address_remap = address_remap
1342
- self .cache = cache
1343
- self .cache_config = cache_config
1343
+ self ._cache = cache
1344
+ self ._cache_config = cache_config
1345
+ self ._cache_factory = cache_factory
1344
1346
self ._moved_exception = None
1345
1347
self .connection_kwargs = kwargs
1346
1348
self .read_load_balancer = LoadBalancer ()
@@ -1484,15 +1486,13 @@ def create_redis_node(self, host, port, **kwargs):
1484
1486
# Create a redis node with a costumed connection pool
1485
1487
kwargs .update ({"host" : host })
1486
1488
kwargs .update ({"port" : port })
1487
- kwargs .update ({"cache" : self .cache })
1488
- kwargs .update ({"cache_config" : self .cache_config })
1489
+ kwargs .update ({"cache" : self ._cache })
1489
1490
r = Redis (connection_pool = self .connection_pool_class (** kwargs ))
1490
1491
else :
1491
1492
r = Redis (
1492
1493
host = host ,
1493
1494
port = port ,
1494
- cache = self .cache ,
1495
- cache_config = self .cache_config ,
1495
+ cache = self ._cache ,
1496
1496
** kwargs ,
1497
1497
)
1498
1498
return r
@@ -1624,6 +1624,12 @@ def initialize(self):
1624
1624
f"one reachable node: { str (exception )} "
1625
1625
) from exception
1626
1626
1627
+ if self ._cache is None and self ._cache_config is not None :
1628
+ if self ._cache_factory is None :
1629
+ self ._cache = CacheFactory (self ._cache_config ).get_cache ()
1630
+ else :
1631
+ self ._cache = self ._cache_factory .get_cache ()
1632
+
1627
1633
# Create Redis connections to all nodes
1628
1634
self .create_redis_connections (list (tmp_nodes_cache .values ()))
1629
1635
0 commit comments