cached-all
This commit is contained in:
parent
a3303837d5
commit
e99acd591a
|
@ -14,7 +14,7 @@ from services.cache import set_author_cache, update_author_followers_cache
|
||||||
from services.auth import login_required
|
from services.auth import login_required
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from services.encoders import CustomJSONEncoder
|
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.rediscache import redis
|
||||||
from services.schema import mutation, query
|
from services.schema import mutation, query
|
||||||
from services.logger import root_logger as logger
|
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):
|
def load_authors_by(_, _info, by, limit, offset):
|
||||||
cache_key = f"{json.dumps(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():
|
def _load_authors_by():
|
||||||
logger.debug(f'loading authors by {by}')
|
logger.debug(f'loading authors by {by}')
|
||||||
q = select(Author)
|
q = select(Author)
|
||||||
|
@ -150,8 +150,6 @@ def load_authors_by(_, _info, by, limit, offset):
|
||||||
return _load_authors_by()
|
return _load_authors_by()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@query.field('get_author_follows')
|
@query.field('get_author_follows')
|
||||||
async 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):
|
with (local_session() as session):
|
||||||
|
|
|
@ -12,7 +12,7 @@ from orm.community import Community
|
||||||
from orm.reaction import Reaction
|
from orm.reaction import Reaction
|
||||||
from orm.shout import Shout, ShoutReactionsFollower
|
from orm.shout import Shout, ShoutReactionsFollower
|
||||||
from orm.topic import Topic, TopicFollower
|
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
|
get_topics_with_stat_cached
|
||||||
from services.auth import login_required
|
from services.auth import login_required
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
|
@ -274,14 +274,14 @@ def author_unfollow(follower_id, slug):
|
||||||
|
|
||||||
|
|
||||||
@query.field('get_topic_followers')
|
@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 = select(Author)
|
||||||
q = (
|
q = (
|
||||||
q.join(TopicFollower, TopicFollower.follower == Author.id)
|
q.join(TopicFollower, TopicFollower.follower == Author.id)
|
||||||
.join(Topic, Topic.id == TopicFollower.topic)
|
.join(Topic, Topic.id == TopicFollower.topic)
|
||||||
.filter(or_(Topic.slug == slug, Topic.id == topic_id))
|
.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')
|
@query.field('get_shout_followers')
|
||||||
|
|
|
@ -3,21 +3,35 @@ from sqlalchemy import distinct, func, select
|
||||||
from orm.author import Author
|
from orm.author import Author
|
||||||
from orm.shout import ShoutTopic
|
from orm.shout import ShoutTopic
|
||||||
from orm.topic import Topic
|
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.auth import login_required
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from services.schema import mutation, query
|
from services.schema import mutation, query
|
||||||
|
from services.memorycache import cache_region
|
||||||
|
|
||||||
|
|
||||||
@query.field('get_topics_all')
|
@query.field('get_topics_all')
|
||||||
async def get_topics_all(_, _info):
|
def get_topics_all(_, _info):
|
||||||
return await get_topics_with_stat_cached(select(Topic))
|
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')
|
@query.field('get_topics_by_community')
|
||||||
async def get_topics_by_community(_, _info, community_id: int):
|
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)
|
q = select(Topic).where(Topic.community == community_id)
|
||||||
return await get_topics_with_stat_cached(q)
|
return get_with_stat(q)
|
||||||
|
|
||||||
|
return _get_topics_by_community()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@query.field('get_topics_by_author')
|
@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')
|
@query.field('get_topic')
|
||||||
async def get_topic(_, _info, slug: str):
|
def get_topic(_, _info, slug: str):
|
||||||
q = select(Topic).filter(Topic.slug == slug)
|
q = select(Topic).filter(Topic.slug == slug)
|
||||||
topics = await get_topics_with_stat_cached(q)
|
topics = get_with_stat(q)
|
||||||
if topics:
|
if topics:
|
||||||
return topics[0]
|
return topics[0]
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,8 @@ from dogpile.cache import make_region
|
||||||
from settings import REDIS_URL
|
from settings import REDIS_URL
|
||||||
|
|
||||||
# Создание региона кэша с TTL
|
# Создание региона кэша с TTL
|
||||||
authors_cache_region = make_region()
|
cache_region = make_region()
|
||||||
authors_cache_region.configure(
|
cache_region.configure(
|
||||||
'dogpile.cache.redis',
|
'dogpile.cache.redis',
|
||||||
arguments={'url': f'{REDIS_URL}/1'},
|
arguments={'url': f'{REDIS_URL}/1'},
|
||||||
expiration_time=3600, # Cache expiration time in seconds
|
expiration_time=3600, # Cache expiration time in seconds
|
||||||
|
|
Loading…
Reference in New Issue
Block a user