From f774c54cc258f96c4e38bf0b1db6160a593aaae4 Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 29 Feb 2024 10:23:08 +0300 Subject: [PATCH] followers-cached --- resolvers/author.py | 4 +++- services/cache.py | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/resolvers/author.py b/resolvers/author.py index f23aba1b..e2b25dac 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -1,3 +1,4 @@ +import asyncio import json import time @@ -9,7 +10,7 @@ from orm.author import Author, AuthorFollower from orm.shout import ShoutAuthor, ShoutTopic from orm.topic import Topic from resolvers.stat import get_with_stat, author_follows_authors, author_follows_topics -from services.cache import update_author_cache +from services.cache import update_author_cache, update_author_followers_cache from services.auth import login_required from services.db import local_session from services.rediscache import redis @@ -242,6 +243,7 @@ async def get_author_followers(_, _info, slug: str): ) ) results = get_with_stat(q) + _ = asyncio.create_task(update_author_followers_cache(author_id, results)) return json.loads(cached) if cached else results except Exception as exc: import traceback diff --git a/services/cache.py b/services/cache.py index 86c74c2f..472cfdec 100644 --- a/services/cache.py +++ b/services/cache.py @@ -25,6 +25,11 @@ async def update_author_cache(author: dict, ttl=25 * 60 * 60): await redis.execute('SETEX', f'id:{author.get("id")}:author', ttl, payload) +async def update_author_followers_cache(author_id: int, followers, ttl=25 * 60 * 60): + payload = json.dumps(followers) + await redis.execute('SET', f'author:{author_id}:followers', payload) + + async def update_follows_topics_cache(follows, author_id: int, ttl=25 * 60 * 60): try: payload = json.dumps(follows)