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