Skip to content

Commit 8b6003d

Browse files
authored
fix: remove extra redis calls (#125)
- cache server version - check the new hash name first
1 parent 8744bae commit 8b6003d

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

src/corva/cache_adapter.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,17 @@ def delete_all(self) -> None:
5353
class HashMigrator:
5454
MINIMUM_ALLOWED_REDIS_SERVER = semver.Version(major=7, minor=4, patch=0)
5555
NEW_HASH_PREFIX = "migrated/"
56+
_version_checked: bool = False
5657

5758
def __init__(self, hash_name: str, client: redis.Redis):
5859
self.hash_name = hash_name
5960
self.zset_name = f"{hash_name}.EXPIREAT"
6061
self.client = client
6162

6263
def check_redis_server_version(self) -> None:
64+
if HashMigrator._version_checked:
65+
return
66+
6367
# Require Redis 7.4+ for per-field TTL commands
6468
redis_version_str = self.client.info(section="server")["redis_version"]
6569
server_version = semver.Version.parse(version=redis_version_str)
@@ -72,6 +76,8 @@ def check_redis_server_version(self) -> None:
7276
f"less then {self.MINIMUM_ALLOWED_REDIS_SERVER} -> "
7377
f"incompatible with used python SDK version `{version('corva-sdk')}`")
7478

79+
HashMigrator._version_checked = True
80+
7581
def run(self) -> bool:
7682
"""Prepare parallel new-style cache while keeping legacy structures.
7783
@@ -91,16 +97,16 @@ def run(self) -> bool:
9197

9298
from corva import Logger
9399

94-
# Legacy structure must exist; otherwise nothing to do
95-
if not self.client.exists(self.zset_name):
96-
return False
97-
98100
new_hash_name = self.NEW_HASH_PREFIX + self.hash_name
99101

100102
# If new hash already exists, consider migration already done
101103
if self.client.exists(new_hash_name):
102104
return False
103105

106+
# Legacy structure must exist; otherwise nothing to do
107+
if not self.client.exists(self.zset_name):
108+
return False
109+
104110
# Current server time in ms
105111
sec, micro = self.client.time()
106112
now_ms = int(sec) * 1000 + int(micro) // 1000

tests/integration/test_cache_migration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def current_redis_server_time(redis_client):
1717

1818

1919
def test_server_version_incompatible_with_sdk(redis_client):
20+
HashMigrator._version_checked = False
2021

2122
with (pytest.raises(RuntimeError) as exc):
2223
with mock.patch.object(redis_client, "info",

0 commit comments

Comments
 (0)