From e4266b0bab49b9e5a824b1128dffe8c80dfa2fe8 Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 8 Aug 2024 17:46:25 +0300 Subject: [PATCH] get_cached_topic_by_slug-fix --- cache/cache.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cache/cache.py b/cache/cache.py index 6a03da49..6c8900b9 100644 --- a/cache/cache.py +++ b/cache/cache.py @@ -105,17 +105,18 @@ async def get_cached_topic(topic_id: int): # Get topic by slug from cache -async def get_cached_topic_by_slug(slug: str): +async def get_cached_topic_by_slug(slug: str, get_with_stat): topic_key = f"topic:slug:{slug}" result = await redis.get(topic_key) if result: return json.loads(result) # Load from database if not found in cache - with local_session() as session: - topic = session.execute(select(Topic).where(Topic.slug == slug)).scalar_one_or_none() - if topic: - await cache_topic(topic.dict()) - return topic.dict() + topic_query = select(Topic).where(Topic.slug == slug) + topic = get_with_stat(topic_query) + if topic: + topic_dict = topic.dict() + await cache_topic(topic_dict) + return topic_dict return None