flask-demo-api/flask_demo_api/repository/redis.py

29 lines
786 B
Python
Raw Normal View History

2024-04-16 01:03:02 +03:00
import pickle
2024-04-15 04:02:47 +03:00
import redis # type: ignore
2024-04-16 01:03:02 +03:00
from flask_demo_api.protocols.models import KeyDTO
from flask_demo_api.protocols.repository import Repository
2024-04-15 02:16:07 +03:00
class RedisRepository(Repository):
def __init__(self, redis_pool: redis.Redis) -> None:
self.pool = redis_pool
def get_key(self, obj: KeyDTO) -> KeyDTO | None:
2024-04-16 01:03:02 +03:00
data = self.pool.get(obj.key)
if not data:
return None
return KeyDTO(key=str(obj.key), val=data)
2024-04-15 02:16:07 +03:00
def add_key(self, obj: KeyDTO) -> KeyDTO:
2024-04-16 01:03:02 +03:00
self.pool.set(str(obj.key), pickle.dumps(obj.val))
return obj
2024-04-15 02:16:07 +03:00
def put_key(self, obj: KeyDTO) -> KeyDTO:
return KeyDTO(key="5", val="6")
2024-04-16 01:03:02 +03:00
def delete_key(self, obj: KeyDTO) -> None:
self.pool.delete(str(obj.key))
return None