get-author-fix
All checks were successful
Deploy on push / deploy (push) Successful in 25s

This commit is contained in:
Untone 2024-02-25 22:45:36 +03:00
parent 8be96daae4
commit b02b8276a6

View File

@ -42,22 +42,24 @@ async def get_author(_, _info, slug='', author_id=None):
author = None author = None
cache = None cache = None
try: try:
if slug: if slug:
with local_session() as session: with local_session() as session:
aliased_author = aliased(Author) aliased_author = aliased(Author)
q = select(aliased_author).filter(aliased_author.slug == slug) q = select(aliased_author).filter(aliased_author.slug == slug)
[author] = session.execute(q) [author] = session.execute(q)
author_id = aliased_author.id author_id = aliased_author.id
if author_id: if author_id:
cache = await redis.execute('GET', f'id:{author_id}:author') cache = await redis.execute('GET', f'id:{author_id}:author')
if not cache: author = json.loads(cache)
if not author:
aliased_author = aliased(Author) aliased_author = aliased(Author)
q = select(aliased_author).where(aliased_author.id == author_id) q = select(aliased_author).where(aliased_author.id == author_id)
[author] = get_with_stat(q) [author] = get_with_stat(q)
if author: if author:
await update_author_cache(author.dict()) await update_author_cache(author.dict())
else:
author = json.loads(cache)
except Exception as exc: except Exception as exc:
logger.error(exc) logger.error(exc)
return author return author