From b04fc1ba6514c6c282dad9c9b17b535d66e5ada6 Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 5 Jun 2024 23:42:09 +0300 Subject: [PATCH] get-author-id-by-user-id --- services/cache.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/services/cache.py b/services/cache.py index 98e4198d..54fb5d77 100644 --- a/services/cache.py +++ b/services/cache.py @@ -30,7 +30,7 @@ async def cache_author(author: dict): author_id = author.get("id") user_id = author.get("user", "").strip() payload = json.dumps(author, cls=CustomJSONEncoder) - await redis.execute("SET", f"author:user:{user_id}", payload) + await redis.execute("SET", f"author:user:{user_id}", author_id) await redis.execute("SET", f"author:id:{author_id}", payload) @@ -76,9 +76,9 @@ async def get_cached_author(author_id: int, get_with_stat): async def get_cached_author_by_user_id(user_id: str, get_with_stat) -> dict: - author_str = await redis.execute("GET", f"author:user:{user_id.strip()}") + author_id = await redis.execute("GET", f"author:user:{user_id.strip()}") author_dict = None - if not author_str: + if not author_id: author_query = select(Author).filter(Author.user == user_id) result = get_with_stat(author_query) if result: @@ -87,7 +87,9 @@ async def get_cached_author_by_user_id(user_id: str, get_with_stat) -> dict: await cache_author(author_with_stat.dict()) author_dict = author_with_stat.dict() else: - author_dict = json.loads(author_str) + author_str = await redis.execute("GET", f"author:id:{author_id}") + if author_str: + author_dict = json.loads(author_str) return author_dict