Skip to content

Commit d184d6b

Browse files
committed
Added API to return cache object
1 parent 6146d13 commit d184d6b

File tree

6 files changed

+227
-94
lines changed

6 files changed

+227
-94
lines changed

redis/asyncio/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
cast,
2727
)
2828

29+
from cachetools import Cache
30+
2931
from redis._parsers.helpers import (
3032
_RedisCallbacks,
3133
_RedisCallbacksRESP2,
@@ -651,6 +653,9 @@ async def parse_response(
651653
return await retval if inspect.isawaitable(retval) else retval
652654
return response
653655

656+
def get_cache(self) -> Optional[Cache]:
657+
return self.connection_pool.cache
658+
654659

655660
StrictRedis = Redis
656661

redis/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,9 @@ def parse_response(self, connection, command_name, **options):
601601
return self.response_callbacks[command_name](response, **options)
602602
return response
603603

604+
def get_cache(self) -> Optional[Cache]:
605+
return self.connection_pool.cache
606+
604607

605608
StrictRedis = Redis
606609

redis/cluster.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,9 @@ def remap_host_port(self, host: str, port: int) -> Tuple[str, int]:
16821682
return self.address_remap((host, port))
16831683
return host, port
16841684

1685+
def get_cache(self) -> Optional[Cache]:
1686+
return self.connection_pool.cache
1687+
16851688

16861689
class ClusterPubSub(PubSub):
16871690
"""

redis/connection.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ def read_response(self, disable_decoding=False, *, disconnect_on_error=True, pus
812812
self._current_command_hash in self._cache
813813
and self._cache[self._current_command_hash] != "caching-in-progress"
814814
):
815-
return self._cache[self._current_command_hash]
815+
return copy.deepcopy(self._cache[self._current_command_hash])
816816

817817
response = self._conn.read_response(
818818
disable_decoding=disable_decoding,
@@ -821,7 +821,7 @@ def read_response(self, disable_decoding=False, *, disconnect_on_error=True, pus
821821
)
822822

823823
with self._cache_lock:
824-
# If response is None prevent from caching and remove temporary cache entry.
824+
# If response is None prevent from caching.
825825
if response is None:
826826
self._cache.pop(self._current_command_hash)
827827
return response
@@ -839,7 +839,7 @@ def read_response(self, disable_decoding=False, *, disconnect_on_error=True, pus
839839

840840
cache_entry = self._cache.get(self._current_command_hash, None)
841841

842-
# Cache only responses that still valid and wasn't invalidated by another connection in meantime
842+
# Cache only responses that still valid and wasn't invalidated by another connection in meantime.
843843
if cache_entry is not None:
844844
self._cache[self._current_command_hash] = response
845845

@@ -1244,7 +1244,7 @@ def __init__(
12441244
self.connection_class = connection_class
12451245
self.connection_kwargs = connection_kwargs
12461246
self.max_connections = max_connections
1247-
self._cache = None
1247+
self.cache = None
12481248
self._cache_conf = None
12491249
self.cache_lock = None
12501250
self.scheduler = None
@@ -1258,9 +1258,9 @@ def __init__(
12581258

12591259
cache = self.connection_kwargs.get("cache")
12601260
if cache is not None:
1261-
self._cache = cache
1261+
self.cache = cache
12621262
else:
1263-
self._cache = TTLCache(self.connection_kwargs["cache_size"], self.connection_kwargs["cache_ttl"])
1263+
self.cache = TTLCache(self.connection_kwargs["cache_size"], self.connection_kwargs["cache_ttl"])
12641264

12651265
self.scheduler = BackgroundScheduler()
12661266
self.scheduler.add_job(self._perform_health_check, "interval", seconds=2, id="cache_health_check")
@@ -1378,7 +1378,7 @@ def get_connection(self, command_name: str, *keys, **options) -> "Connection":
13781378
# pool before all data has been read or the socket has been
13791379
# closed. either way, reconnect and verify everything is good.
13801380
try:
1381-
if connection.can_read() and self._cache is None:
1381+
if connection.can_read() and self.cache is None:
13821382
raise ConnectionError("Connection has data")
13831383
except (ConnectionError, OSError):
13841384
connection.disconnect()
@@ -1408,10 +1408,10 @@ def make_connection(self) -> "ConnectionInterface":
14081408
raise ConnectionError("Too many connections")
14091409
self._created_connections += 1
14101410

1411-
if self._cache is not None and self._cache_conf is not None:
1411+
if self.cache is not None and self._cache_conf is not None:
14121412
return CacheProxyConnection(
14131413
self.connection_class(**self.connection_kwargs),
1414-
self._cache,
1414+
self.cache,
14151415
self._cache_conf,
14161416
self._cache_lock
14171417
)
@@ -1558,10 +1558,10 @@ def reset(self):
15581558

15591559
def make_connection(self):
15601560
"Make a fresh connection."
1561-
if self._cache is not None and self._cache_conf is not None:
1561+
if self.cache is not None and self._cache_conf is not None:
15621562
connection = CacheProxyConnection(
15631563
self.connection_class(**self.connection_kwargs),
1564-
self._cache,
1564+
self.cache,
15651565
self._cache_conf,
15661566
self._cache_lock
15671567
)

tests/conftest.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,17 +446,16 @@ def sentinel_setup(request):
446446
protocol=3,
447447
**kwargs,
448448
)
449-
yield sentinel, cache
449+
yield sentinel
450450
for s in sentinel.sentinels:
451451
s.close()
452452

453453

454454
@pytest.fixture()
455455
def master(request, sentinel_setup):
456456
master_service = request.config.getoption("--master-service")
457-
sentinel, cache = sentinel_setup
458-
master = sentinel.master_for(master_service)
459-
yield master, cache
457+
master = sentinel_setup.master_for(master_service)
458+
yield master
460459
master.close()
461460

462461

0 commit comments

Comments
 (0)