2
from typing import Any, Optional
6
from core.helpers.cache.base import BaseBackend
7
from core.helpers.redis import redis as redis_client
10
class RedisBackend(BaseBackend):
11
async def get(self, *, key: str) -> Any:
12
result = await redis_client.get(key)
16
return ujson.loads(result)
18
async def set(self, *, response: Any, key: str, ttl: int = 60) -> None:
19
if isinstance(response, dict):
20
response = ujson.dumps(response)
22
response = pickle.dumps(response)
24
await redis_client.set(name=key, value=response, ex=ttl)
29
key: Optional[str] = None,
30
value: Optional[str] = None,
31
mapping: Optional[dict] = None,
32
items: Optional[list] = None,
34
await redis_client.hset(name=name, key=key, value=value, mapping=mapping, items=items)
36
async def delete_startswith(self, *, key: str) -> None:
37
async for key in redis_client.scan_iter(f"{key}*"):
38
await redis_client.delete(key)
40
async def get_startswith(self, *, key: str) -> dict:
43
async for value in redis_client.scan_iter(f"{key}*"):
47
key.decode('utf-8'): await self.get(key=key)
51
async def delete(self, *, key: str) -> None:
52
await redis_client.delete(key)
55
redis_backend = RedisBackend()