updated Mongo Postgres Redis Controllers

This commit is contained in:
2025-04-01 13:37:36 +03:00
parent 5d30bc2701
commit 6b9e9050a2
16 changed files with 1700 additions and 48 deletions

View File

@@ -12,7 +12,10 @@ import arrow
import json
from connection import redis_cli
from typing import Union, Dict, List, Optional, Any, ClassVar
from typing import Union, Dict, List, Optional, Any, TypeVar
T = TypeVar('T', Dict[str, Any], List[Any])
class RedisKeyError(Exception):
@@ -86,7 +89,7 @@ class RedisRow:
self.key = self.delimiter.join(merged).encode()
@classmethod
def regex(cls, list_keys: List[Union[str, bytes, None]]) -> str:
def regex(cls, list_keys: List[Union[Optional[str], Optional[bytes]]]) -> str:
"""
Generate Redis search pattern from list of keys.
@@ -250,7 +253,7 @@ class RedisRow:
try:
redis_cli.delete(self.redis_key)
except Exception as e:
print(f"Error deleting key: {str(e)}")
raise RedisKeyError(f"Failed to delete key: {str(e)}")
@property
def keys(self) -> str:
@@ -268,9 +271,24 @@ class RedisRow:
Args:
key: Key in string or bytes format
Raises:
RedisKeyError: If key is empty or invalid
"""
if not key:
raise RedisKeyError("Cannot set empty key")
# Convert to string for validation
key_str = key.decode() if isinstance(key, bytes) else str(key)
# Validate key length (Redis has a 512MB limit for keys)
if len(key_str) > 512 * 1024 * 1024:
raise RedisKeyError("Key exceeds maximum length of 512MB")
# Validate key format (basic check for invalid characters)
if any(c in key_str for c in ['\n', '\r', '\t', '\0']):
raise RedisKeyError("Key contains invalid characters")
self.key = key if isinstance(key, bytes) else str(key).encode()
@property