updated Redis
This commit is contained in:
@@ -1,102 +1,33 @@
|
||||
import json
|
||||
import arrow
|
||||
from typing import (
|
||||
Any,
|
||||
Optional,
|
||||
List,
|
||||
Dict,
|
||||
)
|
||||
|
||||
from typing import Optional, List, Dict, Union
|
||||
|
||||
from AllConfigs.main import MainConfig
|
||||
from ApiServiceRedis.Redis.conn import redis_cli
|
||||
from ApiServiceRedis.Redis.Models.base import RedisRow
|
||||
from ApiServiceRedis.Redis.Models.response import RedisResponse
|
||||
|
||||
|
||||
class RedisRow:
|
||||
|
||||
key: str | bytes
|
||||
value: Any
|
||||
delimiter: str = "*"
|
||||
expires_at: Optional[str] = None
|
||||
|
||||
@classmethod
|
||||
def merge(cls, set_values: list[str | bytes]):
|
||||
for key, set_value in enumerate(set_values):
|
||||
set_value = str(set_value) if isinstance(set_value, bytes) else set_value
|
||||
cls.key += f"{set_value}" if key == len(set_values) - 1 else f"{set_value}{cls.delimiter}"
|
||||
cls.key = cls.key.encode()
|
||||
|
||||
@classmethod
|
||||
def regex(cls, list_keys: list[str | bytes]):
|
||||
search_regex = ""
|
||||
for key, list_key in enumerate(list_keys):
|
||||
list_key = list_key.decode() if isinstance(list_key, bytes) else str(list_key)
|
||||
if key == 0 and list_key:
|
||||
search_regex += f"{list_key}{cls.delimiter}*"
|
||||
elif key == len(list_keys) - 1 and list_key:
|
||||
search_regex += f"*{cls.delimiter}{list_key}"
|
||||
else:
|
||||
if list_key:
|
||||
search_regex += f"*{cls.delimiter}{list_key}{cls.delimiter}*"
|
||||
return search_regex
|
||||
|
||||
@classmethod
|
||||
def parse(cls):
|
||||
return cls.key.split(cls.delimiter) if cls.key else []
|
||||
|
||||
@classmethod
|
||||
def feed(cls, value: Any):
|
||||
cls.value = json.dumps(value)
|
||||
|
||||
@classmethod
|
||||
def modify(cls, add_dict):
|
||||
value = cls.data or {}
|
||||
cls.feed({**value, **add_dict})
|
||||
|
||||
@classmethod
|
||||
def remove(cls, key):
|
||||
value = cls.data or {}
|
||||
value.pop(key)
|
||||
cls.feed(value)
|
||||
|
||||
@property
|
||||
def keys(self):
|
||||
return self.key if isinstance(self.key, str) else self.key.decode()
|
||||
|
||||
@property
|
||||
def redis_key(self):
|
||||
return self.key if isinstance(self.key, bytes) else self.key.encode()
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
return json.loads(self.value)
|
||||
|
||||
@property
|
||||
def as_dict(self):
|
||||
return {
|
||||
"keys": self.keys,
|
||||
"value": self.data,
|
||||
}
|
||||
|
||||
|
||||
class RedisActions:
|
||||
"""Class for handling Redis operations with JSON data."""
|
||||
|
||||
@classmethod
|
||||
def get_expiry_time(cls, expiry_kwargs: dict) -> int:
|
||||
expiry_time = 0
|
||||
if "days" in expiry_kwargs:
|
||||
expiry_time += int(expiry_kwargs["days"]) * 24 * 60 * 60
|
||||
if "hours" in expiry_kwargs:
|
||||
expiry_time += int(expiry_kwargs["hours"]) * 60 * 60
|
||||
if "minutes" in expiry_kwargs:
|
||||
expiry_time += int(expiry_kwargs["minutes"]) * 60
|
||||
if "seconds" in expiry_kwargs:
|
||||
expiry_time += int(expiry_kwargs["seconds"])
|
||||
return expiry_time
|
||||
def get_expiry_time(cls, expiry_kwargs: Dict[str, int]) -> int:
|
||||
"""Calculate expiry time in seconds from kwargs."""
|
||||
time_multipliers = {"days": 86400, "hours": 3600, "minutes": 60, "seconds": 1}
|
||||
return sum(
|
||||
int(expiry_kwargs.get(unit, 0)) * multiplier
|
||||
for unit, multiplier in time_multipliers.items()
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def set_json(
|
||||
cls, list_keys: list[str | bytes], value: Optional[dict | list], expires: Optional[dict] = None
|
||||
):
|
||||
cls,
|
||||
list_keys: List[Union[str, bytes]],
|
||||
value: Optional[Union[Dict, List]],
|
||||
expires: Optional[Dict[str, int]] = None,
|
||||
) -> RedisResponse:
|
||||
"""Set JSON value in Redis with optional expiry."""
|
||||
redis_row = RedisRow()
|
||||
redis_row.merge(set_values=list_keys)
|
||||
redis_row.feed(value)
|
||||
@@ -108,9 +39,15 @@ class RedisActions:
|
||||
time=expiry_time,
|
||||
value=redis_row.value,
|
||||
)
|
||||
redis_row.expires_at = arrow.now().shift(seconds=expiry_time).format("YYYY-MM-DD HH:mm:ss")
|
||||
redis_row.expires_at = (
|
||||
arrow.now()
|
||||
.shift(seconds=expiry_time)
|
||||
.format(MainConfig.DATETIME_FORMAT)
|
||||
)
|
||||
|
||||
else:
|
||||
redis_cli.set(name=redis_row.redis_key, value=redis_row.value)
|
||||
|
||||
return RedisResponse(
|
||||
status=True,
|
||||
message="Value is set successfully.",
|
||||
@@ -124,21 +61,31 @@ class RedisActions:
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_json(cls, list_keys: list[str | bytes]):
|
||||
def resolve_expires_at(cls, redis_row: RedisRow) -> str:
|
||||
"""Resolve expiry time for Redis key."""
|
||||
expiry_time = redis_cli.ttl(redis_row.redis_key)
|
||||
if expiry_time == -1:
|
||||
return "Key has no expiry time."
|
||||
return arrow.now().shift(seconds=expiry_time).format(MainConfig.DATETIME_FORMAT)
|
||||
|
||||
@classmethod
|
||||
def get_json(cls, list_keys: List[Union[str, bytes]]) -> RedisResponse:
|
||||
"""Get JSON values from Redis using pattern matching."""
|
||||
try:
|
||||
redis_row = RedisRow()
|
||||
json_get = redis_cli.get(redis_row.regex(list_keys=list_keys))
|
||||
redis_row.key = json_get
|
||||
if not json_get:
|
||||
return RedisResponse(
|
||||
status=False,
|
||||
message="Value is not get successfully.",
|
||||
error="Value is not found in the redis.",
|
||||
)
|
||||
list_of_rows = []
|
||||
regex = RedisRow.regex(list_keys=list_keys)
|
||||
json_get = redis_cli.scan_iter(match=regex)
|
||||
|
||||
for row in list(json_get):
|
||||
redis_row = RedisRow()
|
||||
redis_row.set_key(key=row)
|
||||
redis_row.expires_at = cls.resolve_expires_at(redis_row=redis_row)
|
||||
redis_row.feed(redis_cli.get(redis_row.redis_key))
|
||||
list_of_rows.append(redis_row)
|
||||
return RedisResponse(
|
||||
status=True,
|
||||
message="Value is get successfully.",
|
||||
data=json.loads(json_get),
|
||||
data=list_of_rows,
|
||||
)
|
||||
except Exception as e:
|
||||
return RedisResponse(
|
||||
|
||||
Reference in New Issue
Block a user