postgres added

This commit is contained in:
2025-01-13 20:55:05 +03:00
parent 8b263a3a5c
commit 3bc0146767
15 changed files with 1120 additions and 37 deletions

View File

@@ -69,7 +69,9 @@ class RedisActions:
return arrow.now().shift(seconds=expiry_time).format(MainConfig.DATETIME_FORMAT)
@classmethod
def get_json(cls, list_keys: List[Union[str, bytes]]) -> RedisResponse:
def get_json(
cls, list_keys: List[Union[Optional[str], Optional[bytes]]]
) -> RedisResponse:
"""Get JSON values from Redis using pattern matching."""
try:
list_of_rows = []
@@ -80,8 +82,10 @@ class RedisActions:
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))
redis_value = redis_cli.get(redis_row.redis_key)
redis_row.feed(redis_value)
list_of_rows.append(redis_row)
return RedisResponse(
status=True,
message="Value is get successfully.",

View File

@@ -27,22 +27,38 @@ class RedisRow:
@classmethod
def regex(cls, list_keys: List[Union[str, bytes]]) -> str:
"""Generate Redis search pattern from list of keys."""
search_regex = ""
for key, list_key in enumerate(list_keys):
if not list_key:
continue
"""Generate Redis search pattern from list of keys.
list_key = (
list_key.decode() if isinstance(list_key, bytes) else str(list_key)
)
if key == 0:
search_regex += f"{list_key}{cls.delimiter}*"
elif key == len(list_keys) - 1:
search_regex += f"*{cls.delimiter}{list_key}"
else:
search_regex += f"*{cls.delimiter}{list_key}{cls.delimiter}*"
return search_regex
Example:
list_keys = [None, "example1", "example2"]
Result: "*:example1:example2"
"""
# First create string with dash separators
temp_str = "-"
for list_key in list_keys:
if list_key:
list_key = (
list_key.decode() if isinstance(list_key, bytes) else str(list_key)
)
temp_str += f"{list_key}-"
# Remove redundant dashes
temp_str = temp_str.strip("-")
# If no valid keys, return empty string
if not temp_str:
return ""
# Replace dashes with delimiter
result = temp_str.replace("-", cls.delimiter)
# Add wildcard at start if first item was None
if list_keys and list_keys[0] is None:
result = f"*{cls.delimiter}{result}"
else:
result = f"{result}"
return result
@classmethod
def parse(cls) -> List[str]:

View File

@@ -25,20 +25,26 @@ class RedisResponse:
self.error = error
def as_dict(self) -> Dict:
return {
data = self.all
main_dict = {
"status": self.status,
"message": self.message,
"data": self.data,
"count": self.count,
"dataType": self.data_type,
"error": self.error,
}
if isinstance(data, RedisRow):
return {"data": {data.keys: data.data}, **main_dict}
elif isinstance(data, list):
return {"data": {row.keys: row.data for row in data}, **main_dict}
@property
def all(self) -> Union[Optional[List[RedisRow]]]:
return self.data
return self.data or []
@property
def count(self) -> int:
return len(self.all)
@property
def first(self) -> Union[RedisRow, None]:
if self.data:
return self.data[0]
return None
return self.data[0] if self.data else None

View File

@@ -1,4 +1,5 @@
from redis import Redis
from AllConfigs.Redis.configs import WagRedis

View File

@@ -1,10 +1,7 @@
import secrets
import uuid
from Services.Redis import (
RedisActions,
AccessToken
)
from Services.Redis import RedisActions, AccessToken
first_user = AccessToken(
accessToken=secrets.token_urlsafe(90),
@@ -39,4 +36,4 @@ set_response_second = RedisActions.set_json(
search_keys = [None, set_response_first_json["uu_id"]]
get_response = RedisActions.get_json(list_keys=search_keys)
print("get_response", [data.expires_at for data in get_response.all])
# print("get_response", [data.expires_at for data in get_response.all])