From 9e6f81606b941e963b93077c3485818c71609e20 Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 9 Apr 2024 16:59:41 +0300 Subject: [PATCH] import-fix --- services/cache.py | 31 ------------------------------- services/triggers.py | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/services/cache.py b/services/cache.py index ec29ae1b..f26f970b 100644 --- a/services/cache.py +++ b/services/cache.py @@ -1,12 +1,9 @@ import json -from sqlalchemy import select from orm.author import Author from orm.topic import Topic -from resolvers.stat import get_with_stat from services.encoders import CustomJSONEncoder -from services.logger import root_logger as logger from services.rediscache import redis DEFAULT_FOLLOWS = { @@ -114,31 +111,3 @@ async def cache_follower(follower: Author, author: Author, is_insert=True): author['stat']['followers'] = len(updated_followers) await cache_author(author) return followers - - -async def handle_author_follower_change(author_id: int, follower_id: int, is_insert: bool): - logger.info(author_id) - author_query = select(Author).select_from(Author).filter(Author.id == author_id) - [author] = get_with_stat(author_query) - follower_query = select(Author).select_from(Author).filter(Author.id == follower_id) - [follower] = get_with_stat(follower_query) - if follower and author: - await cache_author(author.dict()) - await cache_author(follower.dict()) - await cache_follows(follower, 'author', author.dict(), is_insert) - await cache_follower(follower, author, is_insert) - - -async def handle_topic_follower_change(topic_id: int, follower_id: int, is_insert: bool): - logger.info(topic_id) - topic_query = select(Topic).filter(Topic.id == topic_id) - [topic] = get_with_stat(topic_query) - follower_query = select(Author).filter(Author.id == follower_id) - [follower] = get_with_stat(follower_query) - if follower and topic: - await cache_author(follower.dict()) - await redis.execute('SET', f'topic:{topic.id}', json.dumps(topic.dict(), cls=CustomJSONEncoder)) - await cache_follows(follower, 'topic', topic.dict(), is_insert) - - -# handle_author_follow and handle_topic_follow -> cache_author, cache_follows, cache_followers diff --git a/services/triggers.py b/services/triggers.py index 2788c029..e8b22402 100644 --- a/services/triggers.py +++ b/services/triggers.py @@ -1,14 +1,17 @@ import asyncio +import json from sqlalchemy import event, select from orm.author import Author, AuthorFollower from orm.reaction import Reaction from orm.shout import Shout, ShoutAuthor -from orm.topic import TopicFollower +from orm.topic import TopicFollower, Topic from resolvers.stat import get_with_stat +from services.encoders import CustomJSONEncoder +from services.rediscache import redis from services.logger import root_logger as logger -from services.cache import cache_author, handle_topic_follower_change, handle_author_follower_change +from services.cache import cache_author, cache_follows, cache_follower DEFAULT_FOLLOWS = { 'topics': [], @@ -17,6 +20,34 @@ DEFAULT_FOLLOWS = { } +async def handle_author_follower_change(author_id: int, follower_id: int, is_insert: bool): + logger.info(author_id) + author_query = select(Author).select_from(Author).filter(Author.id == author_id) + [author] = get_with_stat(author_query) + follower_query = select(Author).select_from(Author).filter(Author.id == follower_id) + [follower] = get_with_stat(follower_query) + if follower and author: + await cache_author(author.dict()) + await cache_author(follower.dict()) + await cache_follows(follower, 'author', author.dict(), is_insert) + await cache_follower(follower, author, is_insert) + + +async def handle_topic_follower_change(topic_id: int, follower_id: int, is_insert: bool): + logger.info(topic_id) + topic_query = select(Topic).filter(Topic.id == topic_id) + [topic] = get_with_stat(topic_query) + follower_query = select(Author).filter(Author.id == follower_id) + [follower] = get_with_stat(follower_query) + if follower and topic: + await cache_author(follower.dict()) + await redis.execute('SET', f'topic:{topic.id}', json.dumps(topic.dict(), cls=CustomJSONEncoder)) + await cache_follows(follower, 'topic', topic.dict(), is_insert) + + +# handle_author_follow and handle_topic_follow -> cache_author, cache_follows, cache_followers + + def after_shout_update(_mapper, _connection, shout: Shout): logger.info('after shout update') # Main query to get authors associated with the shout through ShoutAuthor