author-refactored
All checks were successful
Deploy on push / deploy (push) Successful in 23s

This commit is contained in:
Untone 2024-03-28 19:36:27 +03:00
parent 736877d50e
commit 77440388d3

View File

@ -57,35 +57,30 @@ def get_authors_all(_, _info):
async def get_author(_, _info, slug='', author_id=None):
author_dict = None
try:
author_query = ''
author_dict = None
if slug:
author_id = local_session().query(Author.id).filter(Author.slug == slug).scalar()
author_query = select(Author.id).filter(Author.slug == slug)
author_id = local_session().execute(author_query).scalar()
logger.debug(f'found @{slug} with id {author_id}')
if author_id:
author_query = select(Author.id).filter(Author.id == author_id)
cache_key = f'author:{author_id}'
cache = await redis.execute('GET', cache_key)
author_dict = None
if cache and isinstance(cache, str):
logger.debug(f'got cached author {cache_key} -> {cache}')
author_dict = json.loads(cache)
stat_str = await redis.execute('GET', f'author:{author_id}')
stat = json.loads(stat_str).get('stat') if isinstance(stat_str, str) else {}
author_dict['stat'] = stat
logger.info(f'cached stat {stat}')
logger.debug(f'got cached author {cache_key} -> {author_dict}')
else:
q = select(Author).where(Author.id == author_id)
[author] = await get_authors_with_stat_cached(q)
[author] = await get_authors_with_stat_cached(author_query)
if not author or not author.stat:
[author] = get_with_stat(author_query)
if author:
author_dict = author.dict()
logger.debug(f'queried author from db {author_dict}')
else:
logger.warn('author was not cached!')
author_query = select(Author).filter(Author.id == author_id)
[author] = get_with_stat(author_query)
author_dict = author.dict()
author.stat = author_dict.get('stat')
if author_dict:
await set_author_cache(author_dict)
logger.debug('author stored in cache')
return author_dict
logger.debug('updated author stored in cache')
return author_dict
except Exception as exc:
import traceback