Skip to content

Commit 97b320a

Browse files
committed
feat(DEVC-1286): move decorator from class static method to outside
1 parent aa3d32a commit 97b320a

1 file changed

Lines changed: 37 additions & 37 deletions

File tree

src/corva/service/cache_sdk.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import datetime
22
from functools import wraps
3-
from typing import Dict, Optional, Protocol, Sequence, Tuple, Union, cast
3+
from typing import Callable, Dict, Optional, Protocol, Sequence, Tuple, Union, cast
44

55
import fakeredis
66
import redis
@@ -29,6 +29,42 @@ def delete_many(self, keys: Sequence[str]) -> None: ...
2929
def delete_all(self) -> None: ...
3030

3131

32+
def ensure_migrated_once(method: Callable) -> Callable:
33+
"""
34+
Decorator that ensures a specific migration process
35+
has been executed before invoking the decorated method. Once the
36+
migration is attempted, the decorator marks it as complete,
37+
regardless of the outcome, to optimize subsequent calls.
38+
39+
Args:
40+
method (Callable): The `UserRedisSdk.method` to be decorated.
41+
42+
Returns:
43+
Callable: The wrapped method which ensures that the migration process
44+
has been attempted before execution.
45+
"""
46+
47+
@wraps(method)
48+
def wrapper(self: 'UserRedisSdk', *args, **kwargs):
49+
50+
if not self._migrated:
51+
migrator = cache_adapter.HashMigrator(
52+
hash_name=self._original_hash_name,
53+
client=self._redis_client,
54+
)
55+
try:
56+
migrator.run()
57+
finally:
58+
# Regardless of outcome (True/False), mark as attempted to avoid
59+
# repeating the check on every call. Subsequent calls operate on
60+
# the new-hash namespace.
61+
self._migrated = True
62+
63+
return method(self, *args, **kwargs)
64+
65+
return wrapper
66+
67+
3268
class UserRedisSdk:
3369
"""User cache protocol implementation using Redis.
3470
@@ -64,42 +100,6 @@ def __init__(
64100
client=self._redis_client,
65101
)
66102

67-
@staticmethod
68-
def ensure_migrated_once(method):
69-
"""
70-
A static method decorator that ensures a specific migration process
71-
has been executed before invoking the decorated method. Once the
72-
migration is attempted, the decorator marks it as complete,
73-
regardless of the outcome, to optimize subsequent calls.
74-
75-
Args:
76-
method (Callable): The `UserRedisSdk.method` to be decorated.
77-
78-
Returns:
79-
Callable: The wrapped method which ensures that the migration process
80-
has been attempted before execution.
81-
"""
82-
83-
@wraps(method)
84-
def wrapper(self, *args, **kwargs):
85-
86-
if not self._migrated:
87-
migrator = cache_adapter.HashMigrator(
88-
hash_name=self._original_hash_name,
89-
client=self._redis_client,
90-
)
91-
try:
92-
migrator.run()
93-
finally:
94-
# Regardless of outcome (True/False), mark as attempted to avoid
95-
# repeating the check on every call. Subsequent calls operate on
96-
# the new-hash namespace.
97-
self._migrated = True
98-
99-
return method(self, *args, **kwargs)
100-
101-
return wrapper
102-
103103
@ensure_migrated_once
104104
def set(self, key: str, value: str, ttl: int = SIXTY_DAYS) -> None:
105105
self.cache_repo.set(key=key, value=value, ttl=ttl)

0 commit comments

Comments
 (0)