get_cached_topic_by_slug-fix
Some checks failed
Deploy on push / deploy (push) Failing after 11s

This commit is contained in:
Untone 2024-08-08 17:46:25 +03:00
parent 5bd9c9750d
commit e4266b0bab

11
cache/cache.py vendored
View File

@ -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()
topic_query = select(Topic).where(Topic.slug == slug)
topic = get_with_stat(topic_query)
if topic:
await cache_topic(topic.dict())
return topic.dict()
topic_dict = topic.dict()
await cache_topic(topic_dict)
return topic_dict
return None