cached-author-fi
All checks were successful
Deploy on push / deploy (push) Successful in 1m9s

This commit is contained in:
Untone 2024-06-05 21:04:48 +03:00
parent 35ef4357fb
commit d93fa4cb4b
3 changed files with 14 additions and 15 deletions

View File

@ -91,7 +91,8 @@ async def get_author_by_user_id(user_id: str):
result = get_with_stat(author_query) result = get_with_stat(author_query)
if result: if result:
[author] = result [author] = result
await cache_author(author.dict()) if author:
await cache_author(author.dict())
except Exception as exc: except Exception as exc:
import traceback import traceback

View File

@ -21,17 +21,14 @@ from services.schema import mutation, query
from services.search import search_service from services.search import search_service
async def cache_by_id(entity, entity_id: int): async def cache_by_id(entity, entity_id: int, cache_method):
caching_query = select(entity).filter(entity.id == entity_id) caching_query = select(entity).filter(entity.id == entity_id)
[x] = get_with_stat(caching_query) [x] = get_with_stat(caching_query)
if not x: if not x:
return return
d = x.dict() # convert object to dictionary d = x.dict() # convert object to dictionary
if entity == Author: cache_method(d)
await cache_author(d)
else:
await cache_topic(d)
return d return d
@ -252,7 +249,7 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
patch_topics(session, shout_by_id, topics_input) patch_topics(session, shout_by_id, topics_input)
del shout_input["topics"] del shout_input["topics"]
for tpc in topics_input: for tpc in topics_input:
await cache_by_id(Topic, tpc["id"]) await cache_by_id(Topic, tpc["id"], cache_topic)
# main topic # main topic
main_topic = shout_input.get("main_topic") main_topic = shout_input.get("main_topic")
@ -274,7 +271,7 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
# search service indexing # search service indexing
search_service.index(shout_by_id) search_service.index(shout_by_id)
for a in shout_by_id.authors: for a in shout_by_id.authors:
await cache_by_id(Author, a.id) await cache_by_id(Author, a.id, cache_author)
return {"shout": shout_dict, "error": None} return {"shout": shout_dict, "error": None}
else: else:
@ -314,13 +311,13 @@ async def delete_shout(_, info, shout_id: int):
session.commit() session.commit()
for author in shout.authors: for author in shout.authors:
await cache_by_id(Author, author.id) await cache_by_id(Author, author.id, cache_author)
info.context["author"] = author.dict() info.context["author"] = author.dict()
info.context["user_id"] = author.user info.context["user_id"] = author.user
unfollow(None, info, "shout", shout.slug) unfollow(None, info, "shout", shout.slug)
for topic in shout.topics: for topic in shout.topics:
await cache_by_id(Topic, topic.id) await cache_by_id(Topic, topic.id, cache_topic)
await notify_shout(shout_dict, "delete") await notify_shout(shout_dict, "delete")
return {"error": None} return {"error": None}

View File

@ -71,7 +71,7 @@ async def get_cached_author(author_id: int, get_with_stat):
if result: if result:
[author] = result [author] = result
if author: if author:
await cache_author(author) await cache_author(author.dict())
return author return author
@ -79,10 +79,11 @@ async def get_cached_author_by_user_id(user_id: str, get_with_stat) -> dict:
author_str = await redis.execute("GET", f"author:user:{user_id}") author_str = await redis.execute("GET", f"author:user:{user_id}")
author_dict = None author_dict = None
if not author_str: if not author_str:
with local_session() as session: author_query = select(Author).filter(Author.user == user_id)
author = session.query(Author).filter(Author.user == user_id).first() [author_with_stat] = get_with_stat(author_query)
await cache_author(author.dict()) if author_with_stat:
author_dict = author.dict() await cache_author(author_with_stat.dict())
author_dict = author_with_stat.dict()
else: else:
author_dict = json.loads(author_str) author_dict = json.loads(author_str)
return author_dict return author_dict