From e99acd591a485055f789891f931b87240d3264d2 Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 12 Mar 2024 17:26:52 +0300 Subject: [PATCH] cached-all --- resolvers/author.py | 6 ++---- resolvers/follower.py | 6 +++--- resolvers/topic.py | 30 ++++++++++++++++++++++-------- services/memorycache.py | 4 ++-- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index 94e7141d..cf7f3aa9 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -14,7 +14,7 @@ from services.cache import set_author_cache, update_author_followers_cache from services.auth import login_required from services.db import local_session from services.encoders import CustomJSONEncoder -from services.memorycache import authors_cache_region +from services.memorycache import cache_region from services.rediscache import redis from services.schema import mutation, query from services.logger import root_logger as logger @@ -113,7 +113,7 @@ async def get_author_id(_, _info, user: str): def load_authors_by(_, _info, by, limit, offset): cache_key = f"{json.dumps(by)}_{limit}_{offset}" - @authors_cache_region.cache_on_arguments(cache_key) + @cache_region.cache_on_arguments(cache_key) def _load_authors_by(): logger.debug(f'loading authors by {by}') q = select(Author) @@ -150,8 +150,6 @@ def load_authors_by(_, _info, by, limit, offset): return _load_authors_by() - - @query.field('get_author_follows') async def get_author_follows(_, _info, slug='', user=None, author_id=None): with (local_session() as session): diff --git a/resolvers/follower.py b/resolvers/follower.py index 2fbe3953..abcbd7a5 100644 --- a/resolvers/follower.py +++ b/resolvers/follower.py @@ -12,7 +12,7 @@ from orm.community import Community from orm.reaction import Reaction from orm.shout import Shout, ShoutReactionsFollower from orm.topic import Topic, TopicFollower -from resolvers.stat import get_authors_with_stat_cached, author_follows_topics, author_follows_authors, get_with_stat, \ +from resolvers.stat import get_authors_with_stat_cached, author_follows_topics, author_follows_authors, \ get_topics_with_stat_cached from services.auth import login_required from services.db import local_session @@ -274,14 +274,14 @@ def author_unfollow(follower_id, slug): @query.field('get_topic_followers') -def get_topic_followers(_, _info, slug: str, topic_id: int) -> List[Author]: +async def get_topic_followers(_, _info, slug: str, topic_id: int) -> List[Author]: q = select(Author) q = ( q.join(TopicFollower, TopicFollower.follower == Author.id) .join(Topic, Topic.id == TopicFollower.topic) .filter(or_(Topic.slug == slug, Topic.id == topic_id)) ) - return get_with_stat(q) + return await get_authors_with_stat_cached(q) @query.field('get_shout_followers') diff --git a/resolvers/topic.py b/resolvers/topic.py index 9ce025a6..af44e2c7 100644 --- a/resolvers/topic.py +++ b/resolvers/topic.py @@ -3,21 +3,35 @@ from sqlalchemy import distinct, func, select from orm.author import Author from orm.shout import ShoutTopic from orm.topic import Topic -from resolvers.stat import get_topics_with_stat_cached +from resolvers.stat import get_topics_with_stat_cached, get_with_stat from services.auth import login_required from services.db import local_session from services.schema import mutation, query +from services.memorycache import cache_region @query.field('get_topics_all') -async def get_topics_all(_, _info): - return await get_topics_with_stat_cached(select(Topic)) +def get_topics_all(_, _info): + cache_key = "get_topics_all" + + @cache_region.cache_on_arguments(cache_key) + def _get_topics_all(): + return get_with_stat(select(Topic)) + + return _get_topics_all() @query.field('get_topics_by_community') -async def get_topics_by_community(_, _info, community_id: int): - q = select(Topic).where(Topic.community == community_id) - return await get_topics_with_stat_cached(q) +def get_topics_by_community(_, _info, community_id: int): + cache_key = f"get_topics_by_community_{community_id}" + + @cache_region.cache_on_arguments(cache_key) + def _get_topics_by_community(): + q = select(Topic).where(Topic.community == community_id) + return get_with_stat(q) + + return _get_topics_by_community() + @query.field('get_topics_by_author') @@ -34,9 +48,9 @@ async def get_topics_by_author(_, _info, author_id=0, slug='', user=''): @query.field('get_topic') -async def get_topic(_, _info, slug: str): +def get_topic(_, _info, slug: str): q = select(Topic).filter(Topic.slug == slug) - topics = await get_topics_with_stat_cached(q) + topics = get_with_stat(q) if topics: return topics[0] diff --git a/services/memorycache.py b/services/memorycache.py index 65aaa111..d079d19e 100644 --- a/services/memorycache.py +++ b/services/memorycache.py @@ -3,8 +3,8 @@ from dogpile.cache import make_region from settings import REDIS_URL # Создание региона кэша с TTL -authors_cache_region = make_region() -authors_cache_region.configure( +cache_region = make_region() +cache_region.configure( 'dogpile.cache.redis', arguments={'url': f'{REDIS_URL}/1'}, expiration_time=3600, # Cache expiration time in seconds