From 732bd2b098c8e325a002f17aedee401499661cff Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 26 Feb 2024 04:58:27 +0300 Subject: [PATCH] caching-follows --- resolvers/author.py | 30 ++++++++++++++++++++---------- services/event_listeners.py | 12 ++++++------ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index e80dd253..6df8296c 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -122,7 +122,7 @@ def load_authors_by(_, _info, by, limit, offset): @query.field('get_author_follows') -def get_author_follows(_, _info, slug='', user=None, author_id=None): +async def get_author_follows(_, _info, slug='', user=None, author_id=None): with local_session() as session: if user or slug: author_id_result = ( @@ -132,9 +132,11 @@ def get_author_follows(_, _info, slug='', user=None, author_id=None): ) author_id = author_id_result[0] if author_id_result else None if author_id: - logger.debug(f'getting {author_id} follows') - topics = author_follows_topics(author_id) - authors = author_follows_authors(author_id) + logger.debug(f'getting {author_id} follows authors') + cached = await redis.execute(f'id:{author_id}:follows-authors') + topics = json.loads(cached) if cached else author_follows_topics(author_id) + cached = await redis.execute(f'id:{author_id}:follows-topics') + authors = json.loads(cached) if cached else author_follows_authors(author_id) return { 'topics': topics, 'authors': authors, @@ -147,7 +149,7 @@ def get_author_follows(_, _info, slug='', user=None, author_id=None): @query.field('get_author_follows_topics') -def get_author_follows_topics(_, _info, slug='', user=None, author_id=None): +async def get_author_follows_topics(_, _info, slug='', user=None, author_id=None): with local_session() as session: if user or slug: author_id_result = ( @@ -158,14 +160,18 @@ def get_author_follows_topics(_, _info, slug='', user=None, author_id=None): author_id = author_id_result[0] if author_id_result else None if author_id: logger.debug(f'getting {author_id} follows topics') - follows = author_follows_topics(author_id) - return follows + rkey = f'id:{author_id}:follows-topics' + cached = await redis.execute(rkey) + topics = json.loads(cached) if cached else author_follows_topics(author_id) + if not cached: + await redis.execute('SETEX', rkey, json.dumps(topics), 24*60*60) + return topics else: raise ValueError('Author not found') @query.field('get_author_follows_authors') -def get_author_follows_authors(_, _info, slug='', user=None, author_id=None): +async def get_author_follows_authors(_, _info, slug='', user=None, author_id=None): with local_session() as session: if user or slug: author_id_result = ( @@ -176,8 +182,12 @@ def get_author_follows_authors(_, _info, slug='', user=None, author_id=None): author_id = author_id_result[0] if author_id_result else None if author_id: logger.debug(f'getting {author_id} follows authors') - follows = author_follows_authors(author_id) - return follows + rkey = f'id:{author_id}:follows-authors' + cached = await redis.execute(rkey) + authors = json.loads(cached) if cached else author_follows_authors(author_id) + if not cached: + await redis.execute('SETEX', rkey, json.dumps(authors), 24*60*60) + return authors else: raise ValueError('Author not found') diff --git a/services/event_listeners.py b/services/event_listeners.py index bfc7a51f..fa474d10 100644 --- a/services/event_listeners.py +++ b/services/event_listeners.py @@ -29,22 +29,22 @@ async def update_follows_topics_cache(follows, author_id: int, ttl=25 * 60 * 60) try: payload = json.dumps(follows) await redis.execute('SETEX', f'author:{author_id}:follows-topics', ttl, payload) - except Exception as exc: - logger.error(exc) + except Exception: import traceback - traceback.print_exc() + exc = traceback.format_exc() + logger.error(exc) async def update_follows_authors_cache(follows, author_id: int, ttl=25 * 60 * 60): try: payload = json.dumps(follows) await redis.execute('SETEX', f'author:{author_id}:follows-authors', ttl, payload) - except Exception as exc: - logger.error(exc) + except Exception: import traceback - traceback.print_exc() + exc = traceback.format_exc() + logger.error(exc) @event.listens_for(Shout, 'after_insert')