get-with-stat-as-arg
All checks were successful
Deploy on push / deploy (push) Successful in 1m11s

This commit is contained in:
Untone 2024-08-14 18:33:11 +03:00
parent 4c126fd859
commit 5095b0b4c0

16
cache/cache.py vendored
View File

@ -115,9 +115,9 @@ async def get_cached_topic_by_slug(slug: str, get_with_stat):
return json.loads(result)
# Load from database if not found in cache
topic_query = select(Topic).where(Topic.slug == slug)
topic = get_with_stat(topic_query)
if topic:
topic_dict = topic.dict()
topics = get_with_stat(topic_query)
if topics:
topic_dict = topics[0].dict()
await cache_topic(topic_dict)
return topic_dict
return None
@ -258,7 +258,7 @@ async def get_cached_follower_topics(author_id: int):
# Get author by user ID from cache
async def get_cached_author_by_user_id(user_id: str):
async def get_cached_author_by_user_id(user_id: str, get_with_stat):
"""
Retrieve author information by user_id, checking the cache first, then the database.
@ -277,11 +277,11 @@ async def get_cached_author_by_user_id(user_id: str):
return json.loads(author_data)
# If data is not found in cache, query the database
with local_session() as session:
author = session.execute(select(Author).where(Author.user == user_id)).scalar_one_or_none()
if author:
author_query = select(Author).where(Author.user == user_id)
authors = get_with_stat(author_query)
if authors:
# Cache the retrieved author data
author = authors[0]
author_dict = author.dict()
await asyncio.gather(
redis.execute("SET", f"author:user:{user_id.strip()}", str(author.id)),