This commit is contained in:
parent
4c126fd859
commit
5095b0b4c0
30
cache/cache.py
vendored
30
cache/cache.py
vendored
|
@ -115,9 +115,9 @@ async def get_cached_topic_by_slug(slug: str, get_with_stat):
|
||||||
return json.loads(result)
|
return json.loads(result)
|
||||||
# Load from database if not found in cache
|
# Load from database if not found in cache
|
||||||
topic_query = select(Topic).where(Topic.slug == slug)
|
topic_query = select(Topic).where(Topic.slug == slug)
|
||||||
topic = get_with_stat(topic_query)
|
topics = get_with_stat(topic_query)
|
||||||
if topic:
|
if topics:
|
||||||
topic_dict = topic.dict()
|
topic_dict = topics[0].dict()
|
||||||
await cache_topic(topic_dict)
|
await cache_topic(topic_dict)
|
||||||
return topic_dict
|
return topic_dict
|
||||||
return None
|
return None
|
||||||
|
@ -258,7 +258,7 @@ async def get_cached_follower_topics(author_id: int):
|
||||||
|
|
||||||
|
|
||||||
# Get author by user ID from cache
|
# 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.
|
Retrieve author information by user_id, checking the cache first, then the database.
|
||||||
|
|
||||||
|
@ -277,17 +277,17 @@ async def get_cached_author_by_user_id(user_id: str):
|
||||||
return json.loads(author_data)
|
return json.loads(author_data)
|
||||||
|
|
||||||
# If data is not found in cache, query the database
|
# If data is not found in cache, query the database
|
||||||
with local_session() as session:
|
author_query = select(Author).where(Author.user == user_id)
|
||||||
author = session.execute(select(Author).where(Author.user == user_id)).scalar_one_or_none()
|
authors = get_with_stat(author_query)
|
||||||
|
if authors:
|
||||||
if author:
|
# Cache the retrieved author data
|
||||||
# Cache the retrieved author data
|
author = authors[0]
|
||||||
author_dict = author.dict()
|
author_dict = author.dict()
|
||||||
await asyncio.gather(
|
await asyncio.gather(
|
||||||
redis.execute("SET", f"author:user:{user_id.strip()}", str(author.id)),
|
redis.execute("SET", f"author:user:{user_id.strip()}", str(author.id)),
|
||||||
redis.execute("SET", f"author:id:{author.id}", json.dumps(author_dict)),
|
redis.execute("SET", f"author:id:{author.id}", json.dumps(author_dict)),
|
||||||
)
|
)
|
||||||
return author_dict
|
return author_dict
|
||||||
|
|
||||||
# Return None if author is not found
|
# Return None if author is not found
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Reference in New Issue
Block a user