From 968935869e99193336b97f4f55b788c83a06ddd4 Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 30 May 2024 19:42:38 +0300 Subject: [PATCH] cache-refactored-4 --- resolvers/author.py | 16 ++++++++-------- resolvers/follower.py | 28 ++++++++++++++++++---------- schema/type.graphql | 6 +++--- services/cache.py | 8 ++++---- 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index 44c62487..ab55e3a3 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -12,8 +12,8 @@ from services.cache import ( get_cached_author, get_cached_author_by_user_id, get_cached_author_followers, - get_cached_author_follows_authors, - get_cached_author_follows_topics, + get_cached_follower_authors, + get_cached_follower_topics, ) from services.db import local_session from services.logger import root_logger as logger @@ -176,8 +176,8 @@ async def get_author_follows(_, _info, slug="", user=None, author_id=0): if bool(author_id): logger.debug(f"getting {author_id} follows authors") - authors = await get_cached_author_follows_authors(author_id) - topics = await get_cached_author_follows_topics(author_id) + authors = await get_cached_follower_authors(author_id) + topics = await get_cached_follower_topics(author_id) return { "topics": topics, "authors": authors, @@ -193,8 +193,8 @@ async def get_author_follows(_, _info, slug="", user=None, author_id=0): @query.field("get_author_follows_topics") async def get_author_follows_topics(_, _info, slug="", user=None, author_id=None): try: - author_id = get_author_id_from(slug, user, author_id) - topics = await get_cached_author_follows_topics(author_id) + follower_id = get_author_id_from(slug, user, author_id) + topics = await get_cached_follower_topics(follower_id) return topics except Exception: import traceback @@ -205,8 +205,8 @@ async def get_author_follows_topics(_, _info, slug="", user=None, author_id=None @query.field("get_author_follows_authors") async def get_author_follows_authors(_, _info, slug="", user=None, author_id=None): try: - author_id = get_author_id_from(slug, user, author_id) - return await get_cached_author_follows_authors(author_id) + follower_id = get_author_id_from(slug, user, author_id) + return await get_cached_follower_authors(follower_id) except Exception: import traceback diff --git a/resolvers/follower.py b/resolvers/follower.py index c8fcb2f5..b4c53a80 100644 --- a/resolvers/follower.py +++ b/resolvers/follower.py @@ -14,8 +14,8 @@ from services.cache import ( cache_author, cache_topic, get_cached_author_by_user_id, - get_cached_author_follows_authors, - get_cached_author_follows_topics, + get_cached_follower_authors, + get_cached_follower_topics, ) from services.db import local_session from services.logger import root_logger as logger @@ -48,14 +48,13 @@ async def follow(_, info, what, slug): if not user_id or not follower_dict: return {"error": "unauthorized"} follower_id = follower_dict.get("id") - entity = what.lower() if what == "AUTHOR": - follows = await get_cached_author_follows_authors(follower_id) follower_id = int(follower_id) error = author_follow(follower_id, slug) if not error: + follows = await get_cached_follower_authors(follower_id) [author_id] = local_session().query(Author.id).filter(Author.slug == slug).first() if author_id and author_id not in follows: follows.append(author_id) @@ -68,8 +67,10 @@ async def follow(_, info, what, slug): elif what == "TOPIC": error = topic_follow(follower_id, slug) - topic_dict = await cache_by_slug(what, slug) - await cache_topic(topic_dict) + if not error: + follows = await get_cached_follower_topics(follower_id) + topic_dict = await cache_by_slug(what, slug) + await cache_topic(topic_dict) elif what == "COMMUNITY": with local_session() as session: @@ -77,6 +78,11 @@ async def follow(_, info, what, slug): elif what == "SHOUT": error = reactions_follow(follower_id, slug) + if not error: + # TODO: follows = await get_cached_follower_reactions(follower_id) + # shout_dict = await cache_shout_by_slug(what, slug) + # await cache_topic(topic_dict) + pass return {f"{entity}s": follows, "error": error} @@ -96,7 +102,7 @@ async def unfollow(_, info, what, slug): follows = [] if what == "AUTHOR": - follows = await get_cached_author_follows_authors(follower_id) + follows = await get_cached_follower_authors(follower_id) follower_id = int(follower_id) error = author_unfollow(follower_id, slug) # NOTE: after triggers should update cached stats @@ -115,7 +121,7 @@ async def unfollow(_, info, what, slug): elif what == "TOPIC": error = topic_unfollow(follower_id, slug) if not error: - follows = await get_cached_author_follows_topics(follower_id) + follows = await get_cached_follower_topics(follower_id) topic_dict = await cache_by_slug(what, slug) await cache_topic(topic_dict) @@ -125,6 +131,8 @@ async def unfollow(_, info, what, slug): elif what == "SHOUT": error = reactions_unfollow(follower_id, slug) + if not error: + pass return {"error": error, f"{entity}s": follows} @@ -142,8 +150,8 @@ async def get_follows_by_user_id(user_id: str): author_id = author.get("id") if author_id: - topics = await get_cached_author_follows_topics(author_id) - authors = await get_cached_author_follows_authors(author_id) + topics = await get_cached_follower_topics(author_id) + authors = await get_cached_follower_authors(author_id) return { "topics": topics or [], "authors": authors or [], diff --git a/schema/type.graphql b/schema/type.graphql index 1e1a3be5..e0277ce5 100644 --- a/schema/type.graphql +++ b/schema/type.graphql @@ -174,9 +174,9 @@ type Invite { } type AuthorFollowsResult { - topics: [Int] - authors: [Int] - communities: [Int] + topics: [Topic] + authors: [Author] + communities: [Community] error: String } diff --git a/services/cache.py b/services/cache.py index 0f2b2240..6408e615 100644 --- a/services/cache.py +++ b/services/cache.py @@ -1,6 +1,6 @@ import json - from typing import List + from sqlalchemy import and_, join, select from orm.author import Author, AuthorFollower @@ -78,7 +78,7 @@ async def get_cached_author_by_user_id(user_id: str, get_with_stat): return await get_cached_author(int(author_id), get_with_stat) -async def get_cached_authors_by_ids(authors_ids: List[int]): +async def get_cached_authors_by_ids(authors_ids: List[int]) -> List[Author | dict]: authors = [] for author_id in authors_ids: if author_id: @@ -159,7 +159,7 @@ async def get_cached_author_followers(author_id: int): return followers -async def get_cached_author_follows_authors(author_id: int): +async def get_cached_follower_authors(author_id: int): rkey = f"author:follows-authors:{author_id}" authors_ids = [] cached = await redis.execute("GET", rkey) @@ -188,7 +188,7 @@ async def get_cached_topics_by_ids(topics_ids: List[int]): return topics_objects -async def get_cached_author_follows_topics(author_id: int): +async def get_cached_follower_topics(author_id: int): rkey = f"author:follows-topics:{author_id}" topics_ids = [] cached = await redis.execute("GET", rkey)