base context for wrappers updated
This commit is contained in:
@@ -45,7 +45,19 @@ class RedisRow:
|
||||
key: ClassVar[Union[str, bytes]]
|
||||
value: ClassVar[Any]
|
||||
delimiter: ClassVar[str] = ":"
|
||||
expires_at: ClassVar[Optional[str]] = None
|
||||
expires_at: Optional[dict] = {"seconds": 60 * 60 * 30}
|
||||
expires_at_string: Optional[str]
|
||||
|
||||
@classmethod
|
||||
def get_expiry_time(cls) -> int | None:
|
||||
"""Calculate expiry time in seconds from kwargs."""
|
||||
time_multipliers = {"days": 86400, "hours": 3600, "minutes": 60, "seconds": 1}
|
||||
if cls.expires_at:
|
||||
return sum(
|
||||
int(cls.expires_at.get(unit, 0)) * multiplier
|
||||
for unit, multiplier in time_multipliers.items()
|
||||
)
|
||||
return
|
||||
|
||||
@classmethod
|
||||
def merge(cls, set_values: List[Union[str, bytes]]) -> None:
|
||||
@@ -108,7 +120,7 @@ class RedisRow:
|
||||
# Add wildcard if first key was None
|
||||
if list_keys[0] is None:
|
||||
pattern = f"*{cls.delimiter}{pattern}"
|
||||
if '*' not in pattern:
|
||||
if "*" not in pattern:
|
||||
pattern = f"{pattern}:*"
|
||||
return pattern
|
||||
|
||||
@@ -182,6 +194,37 @@ class RedisRow:
|
||||
|
||||
cls.feed({**current_data, **add_dict})
|
||||
|
||||
@classmethod
|
||||
def save(cls):
|
||||
"""
|
||||
Save the data to Redis with optional expiration.
|
||||
|
||||
Raises:
|
||||
RedisKeyError: If key is not set
|
||||
RedisValueError: If value is not set
|
||||
"""
|
||||
import arrow
|
||||
from Services.Redis.conn import redis_cli
|
||||
|
||||
if not cls.key:
|
||||
raise RedisKeyError("Cannot save data without a key")
|
||||
if not cls.value:
|
||||
raise RedisValueError("Cannot save empty data")
|
||||
|
||||
if cls.expires_at:
|
||||
redis_cli.setex(name=cls.redis_key, time=cls.expires_at, value=cls.value)
|
||||
cls.expires_at_string = str(
|
||||
arrow.now()
|
||||
.shift(seconds=cls.get_expiry_time())
|
||||
.format("YYYY-MM-DD HH:mm:ss")
|
||||
)
|
||||
return cls.value
|
||||
|
||||
redis_cli.set(name=cls.redis_key, value=cls.value)
|
||||
cls.expires_at = None
|
||||
cls.expires_at_string = None
|
||||
return cls.value
|
||||
|
||||
@classmethod
|
||||
def remove(cls, key: str) -> None:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user