This commit is contained in:
parent
5bd9c9750d
commit
e4266b0bab
11
cache/cache.py
vendored
11
cache/cache.py
vendored
|
@ -105,17 +105,18 @@ async def get_cached_topic(topic_id: int):
|
||||||
|
|
||||||
|
|
||||||
# Get topic by slug from cache
|
# 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}"
|
topic_key = f"topic:slug:{slug}"
|
||||||
result = await redis.get(topic_key)
|
result = await redis.get(topic_key)
|
||||||
if result:
|
if result:
|
||||||
return json.loads(result)
|
return json.loads(result)
|
||||||
# Load from database if not found in cache
|
# Load from database if not found in cache
|
||||||
with local_session() as session:
|
topic_query = select(Topic).where(Topic.slug == slug)
|
||||||
topic = session.execute(select(Topic).where(Topic.slug == slug)).scalar_one_or_none()
|
topic = get_with_stat(topic_query)
|
||||||
if topic:
|
if topic:
|
||||||
await cache_topic(topic.dict())
|
topic_dict = topic.dict()
|
||||||
return topic.dict()
|
await cache_topic(topic_dict)
|
||||||
|
return topic_dict
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user