|
1 | 1 | import datetime |
2 | 2 | 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 |
4 | 4 |
|
5 | 5 | import fakeredis |
6 | 6 | import redis |
@@ -29,6 +29,42 @@ def delete_many(self, keys: Sequence[str]) -> None: ... |
29 | 29 | def delete_all(self) -> None: ... |
30 | 30 |
|
31 | 31 |
|
| 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 | + |
32 | 68 | class UserRedisSdk: |
33 | 69 | """User cache protocol implementation using Redis. |
34 | 70 |
|
@@ -64,42 +100,6 @@ def __init__( |
64 | 100 | client=self._redis_client, |
65 | 101 | ) |
66 | 102 |
|
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 | | - |
103 | 103 | @ensure_migrated_once |
104 | 104 | def set(self, key: str, value: str, ttl: int = SIXTY_DAYS) -> None: |
105 | 105 | self.cache_repo.set(key=key, value=value, ttl=ttl) |
|
0 commit comments