Skip to content

Commit ba94215

Browse files
committed
Moved hardcoded values to constants, restricted dependency versions
1 parent 9e9b68c commit ba94215

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

dev_requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
black==24.3.0
2-
cachetools
2+
cachetools>=5.5.0
33
apscheduler
44
click==8.0.4
55
flake8-isort

redis/connection.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -730,25 +730,27 @@ def ensure_string(key):
730730

731731

732732
class CacheProxyConnection(ConnectionInterface):
733+
CACHE_DUMMY_STATUS = "caching-in-progress"
734+
KEYS_MAPPING_CACHE_SIZE = 10000
735+
733736
def __init__(
734737
self,
735738
conn: ConnectionInterface,
736739
cache: CacheInterface,
737-
conf: CacheConfiguration,
738-
cache_lock: threading.Lock,
740+
conf: CacheConfiguration
739741
):
740742
self.pid = os.getpid()
741743
self._conn = conn
742744
self.retry = self._conn.retry
743745
self.host = self._conn.host
744746
self.port = self._conn.port
745747
self._cache = cache
746-
self._cache_lock = cache_lock
748+
self._cache_lock = threading.Lock()
747749
self._conf = conf
748750
self._current_command_hash = None
749751
self._current_command_keys = None
750752
self._current_options = None
751-
self._keys_mapping = LRUCache(maxsize=10000)
753+
self._keys_mapping = LRUCache(maxsize=self.KEYS_MAPPING_CACHE_SIZE)
752754
self.register_connect_callback(self._enable_tracking_callback)
753755

754756
def repr_pieces(self):
@@ -772,6 +774,7 @@ def on_connect(self):
772774
def disconnect(self, *args):
773775
with self._cache_lock:
774776
self._cache.clear()
777+
self._keys_mapping.clear()
775778
self._conn.disconnect(*args)
776779

777780
def check_health(self):
@@ -811,7 +814,7 @@ def send_command(self, *args, **kwargs):
811814

812815
# Set temporary entry as a status to prevent
813816
# race condition from another connection.
814-
self._cache.set(self._current_command_hash, "caching-in-progress")
817+
self._cache.set(self._current_command_hash, self.CACHE_DUMMY_STATUS)
815818

816819
# Send command over socket only if it's allowed
817820
# read-only command that not yet cached.
@@ -827,7 +830,7 @@ def read_response(
827830
# Check if command response exists in a cache and it's not in progress.
828831
if (
829832
self._cache.exists(self._current_command_hash)
830-
and self._cache.get(self._current_command_hash) != "caching-in-progress"
833+
and self._cache.get(self._current_command_hash) != self.CACHE_DUMMY_STATUS
831834
):
832835
return copy.deepcopy(self._cache.get(self._current_command_hash))
833836

@@ -1264,15 +1267,13 @@ def __init__(
12641267
self.cache = None
12651268
self._cache_conf = None
12661269
self._cache_factory = cache_factory
1267-
self.cache_lock = None
12681270
self.scheduler = None
12691271

12701272
if connection_kwargs.get("use_cache"):
12711273
if connection_kwargs.get("protocol") not in [3, "3"]:
12721274
raise RedisError("Client caching is only supported with RESP version 3")
12731275

12741276
self._cache_conf = CacheConfiguration(**self.connection_kwargs)
1275-
self._cache_lock = threading.Lock()
12761277

12771278
cache = self.connection_kwargs.get("cache")
12781279

@@ -1443,8 +1444,7 @@ def make_connection(self) -> "ConnectionInterface":
14431444
return CacheProxyConnection(
14441445
self.connection_class(**self.connection_kwargs),
14451446
self.cache,
1446-
self._cache_conf,
1447-
self._cache_lock,
1447+
self._cache_conf
14481448
)
14491449

14501450
return self.connection_class(**self.connection_kwargs)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
async-timeout>=4.0.3
2-
cachetools
2+
cachetools>=5.5.0
33
apscheduler

0 commit comments

Comments
 (0)