mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-30 12:46:39 +00:00
fix: Make calls to redis synchronous
This commit is contained in:
parent
b609bbc39d
commit
5ca8f43a8e
|
@ -8,21 +8,21 @@ from selfprivacy_api.utils.redis_pool import RedisPool
|
|||
CACHE_PREFIX = "exec_cache:"
|
||||
|
||||
|
||||
async def get_redis_object(key: str) -> Optional[Any]:
|
||||
redis = RedisPool().get_connection_async()
|
||||
binary_obj = await redis.get(key)
|
||||
def get_redis_object(key: str) -> Optional[Any]:
|
||||
redis = RedisPool().get_connection()
|
||||
binary_obj = redis.get(key)
|
||||
if binary_obj is None:
|
||||
return None
|
||||
return pickle.loads(binary_obj)
|
||||
|
||||
|
||||
async def save_redis_object(key: str, obj: Any, expire: Optional[int] = 60) -> None:
|
||||
redis = RedisPool().get_connection_async()
|
||||
def save_redis_object(key: str, obj: Any, expire: Optional[int] = 60) -> None:
|
||||
redis = RedisPool().get_connection()
|
||||
binary_obj = pickle.dumps(obj)
|
||||
if expire:
|
||||
await redis.setex(key, expire, binary_obj)
|
||||
redis.setex(key, expire, binary_obj)
|
||||
else:
|
||||
await redis.set(key, binary_obj)
|
||||
redis.set(key, binary_obj)
|
||||
|
||||
|
||||
def redis_cached_call(ttl: Optional[int] = 60) -> Callable[..., Callable]:
|
||||
|
@ -30,16 +30,13 @@ def redis_cached_call(ttl: Optional[int] = 60) -> Callable[..., Callable]:
|
|||
@wraps(func)
|
||||
async def wrapper(*args, **kwargs) -> Any:
|
||||
key = f"{CACHE_PREFIX}{func.__name__}:{args}:{kwargs}"
|
||||
cached_value = await get_redis_object(key)
|
||||
cached_value = get_redis_object(key)
|
||||
if cached_value is not None:
|
||||
return cached_value
|
||||
|
||||
if asyncio.iscoroutinefunction(func):
|
||||
result = await func(*args, **kwargs)
|
||||
else:
|
||||
result = func(*args, **kwargs)
|
||||
|
||||
await save_redis_object(key, result, ttl)
|
||||
save_redis_object(key, result, ttl)
|
||||
|
||||
return result
|
||||
|
||||
|
|
Loading…
Reference in a new issue