From 8826af02b50afd669fcd1270fb022c0652f385b2 Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 28 Mar 2024 19:05:27 +0300 Subject: [PATCH] refactored-get-author --- resolvers/author.py | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index 64135767..5ab23e4e 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -60,28 +60,27 @@ async def get_author(_, _info, slug='', author_id=None): if slug: author_id = local_session().query(Author.id).filter(Author.slug == slug) logger.debug(f'found @{slug} with id {author_id}') - if author_id: - cache_key = f'author:{author_id}' - cache = await redis.execute('GET', cache_key) - logger.debug(f'GET {cache_key} -> {cache}') + if 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) + else: q = select(Author).where(Author.id == author_id) - author_dict = None - if cache and isinstance(cache, str): - author_dict = json.loads(cache) + [author] = await get_authors_with_stat_cached(q) + if author: + author_dict = author.dict() else: - result = await get_authors_with_stat_cached(q) - if result: - [author] = result - author_dict = 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() - if author_dict: - await set_author_cache(author_dict) - logger.debug('author stored in cache') - return author_dict + 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: + await set_author_cache(author_dict) + logger.debug('author stored in cache') + return author_dict except Exception as exc: import traceback