2023-03-13 19:00:39 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from typing import Optional
|
2023-08-21 11:31:29 +00:00
|
|
|
from enum import Enum
|
2023-03-13 19:00:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def store_model_as_hash(redis, redis_key, model):
|
|
|
|
for key, value in model.dict().items():
|
|
|
|
if isinstance(value, datetime):
|
|
|
|
value = value.isoformat()
|
2023-08-21 11:31:29 +00:00
|
|
|
if isinstance(value, Enum):
|
|
|
|
value = value.value
|
2023-03-13 19:00:39 +00:00
|
|
|
redis.hset(redis_key, key, str(value))
|
|
|
|
|
|
|
|
|
|
|
|
def hash_as_model(redis, redis_key: str, model_class):
|
|
|
|
token_dict = _model_dict_from_hash(redis, redis_key)
|
|
|
|
if token_dict is not None:
|
|
|
|
return model_class(**token_dict)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def _prepare_model_dict(d: dict):
|
|
|
|
for key in d.keys():
|
|
|
|
if d[key] == "None":
|
|
|
|
d[key] = None
|
|
|
|
|
|
|
|
|
|
|
|
def _model_dict_from_hash(redis, redis_key: str) -> Optional[dict]:
|
|
|
|
if redis.exists(redis_key):
|
|
|
|
token_dict = redis.hgetall(redis_key)
|
|
|
|
_prepare_model_dict(token_dict)
|
|
|
|
return token_dict
|
|
|
|
return None
|