cache-refactored
All checks were successful
Deploy on push / deploy (push) Successful in 24s

This commit is contained in:
2024-02-27 15:40:53 +03:00
parent 564a8c10b7
commit 2e68128dfc
5 changed files with 299 additions and 283 deletions

View File

@@ -1,7 +1,7 @@
import json
import time
from sqlalchemy import select, or_, and_, text, desc
from sqlalchemy import select, or_, and_, text, desc, cast, Integer
from sqlalchemy.orm import aliased
from sqlalchemy_searchable import search
@@ -9,7 +9,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.event_listeners import update_author_cache
from services.cache import update_author_cache
from services.auth import login_required
from services.db import local_session
from services.rediscache import redis
@@ -214,26 +214,27 @@ def create_author(user_id: str, slug: str, name: str = ''):
@query.field('get_author_followers')
def get_author_followers(_, _info, slug: str):
async def get_author_followers(_, _info, slug: str):
logger.debug(f'getting followers for @{slug}')
try:
with local_session() as session:
author_alias = aliased(Author)
author_id_result = (
session.query(author_alias.id).filter(author_alias.slug == slug).first()
session.query(author_alias).filter(author_alias.slug == slug).first()
)
author_id = author_id_result[0] if author_id_result else None
author_follower_alias = aliased(AuthorFollower, name='af')
q = select(Author).join(
author_follower_alias,
and_(
author_follower_alias.author == author_id,
author_follower_alias.follower == Author.id,
),
)
return get_with_stat(q)
author = author_id_result[0] if author_id_result else None
author_id = cast(author.id, Integer)
cached = await redis.execute('GET', f'id:{author_id}:followers')
if not cached:
author_follower_alias = aliased(AuthorFollower, name='af')
q = select(Author).join(
author_follower_alias,
and_(
author_follower_alias.author == author_id,
author_follower_alias.follower == Author.id,
),
)
return json.loads(cached) if cached else get_with_stat(q)
except Exception as exc:
logger.error(exc)
return []

View File

@@ -16,7 +16,7 @@ from resolvers.topic import topic_unfollow
from resolvers.stat import get_with_stat, author_follows_topics, author_follows_authors
from services.auth import login_required
from services.db import local_session
from services.event_listeners import DEFAULT_FOLLOWS, update_follows_for_author
from services.cache import DEFAULT_FOLLOWS, update_follows_for_author, update_followers_for_author
from services.notify import notify_follower
from services.schema import mutation, query
from services.logger import root_logger as logger
@@ -28,24 +28,27 @@ from services.rediscache import redis
async def follow(_, info, what, slug):
try:
user_id = info.context['user_id']
with local_session() as session:
follower = session.query(Author).filter(Author.user == user_id).first()
if follower:
if what == 'AUTHOR':
if author_unfollow(follower.id, slug):
author = session.query(Author).where(Author.slug == slug).first()
if author:
await update_follows_for_author(session, follower, 'author', author, True)
await notify_follower(follower.dict(), author.id, 'unfollow')
elif what == 'TOPIC':
topic = session.query(Topic).where(Topic.slug == slug).first()
if topic:
await update_follows_for_author(session, follower, 'topic', topic, True)
topic_unfollow(follower.id, slug)
elif what == 'COMMUNITY':
community_follow(follower.id, slug)
elif what == 'REACTIONS':
reactions_follow(follower.id, slug)
follower_query = select(Author).select_from(Author).filter(Author.user == user_id)
[follower] = get_with_stat(follower_query)
if follower:
if what == 'AUTHOR':
if author_unfollow(follower.id, slug):
author_query = select(Author).select_from(Author).where(Author.slug == slug)
[author] = get_with_stat(author_query)
if author:
await update_follows_for_author(follower, 'author', author, True)
await update_followers_for_author(follower, author, True)
await notify_follower(follower.dict(), author.id, 'unfollow')
elif what == 'TOPIC':
topic_query = select(Topic).where(Topic.slug == slug)
[topic] = get_with_stat(topic_query)
if topic:
await update_follows_for_author(follower, 'topic', topic, True)
topic_unfollow(follower.id, slug)
elif what == 'COMMUNITY':
community_follow(follower.id, slug)
elif what == 'REACTIONS':
reactions_follow(follower.id, slug)
except Exception as e:
logger.debug(info, what, slug)
logger.error(e)
@@ -59,24 +62,27 @@ async def follow(_, info, what, slug):
async def unfollow(_, info, what, slug):
user_id = info.context['user_id']
try:
with local_session() as session:
follower = session.query(Author).filter(Author.user == user_id).first()
if follower:
if what == 'AUTHOR':
if author_unfollow(follower.id, slug):
author = session.query(Author).where(Author.slug == slug).first()
if author:
await update_follows_for_author(session, follower, 'author', author, False)
await notify_follower(follower.dict(), author.id, 'unfollow')
elif what == 'TOPIC':
topic = session.query(Topic).where(Topic.slug == slug).first()
if topic:
await update_follows_for_author(session, follower, 'topic', topic, False)
topic_unfollow(follower.id, slug)
elif what == 'COMMUNITY':
community_unfollow(follower.id, slug)
elif what == 'REACTIONS':
reactions_unfollow(follower.id, slug)
follower_query = select(Author).filter(Author.user == user_id)
[follower] = get_with_stat(follower_query)
if follower:
if what == 'AUTHOR':
if author_unfollow(follower.id, slug):
author_query = select(Author).where(Author.slug == slug)
[author] = get_with_stat(author_query)
if author:
await update_follows_for_author(follower, 'author', author, False)
await update_followers_for_author(follower, author, False)
await notify_follower(follower.dict(), author.id, 'unfollow')
elif what == 'TOPIC':
topic_query = select(Topic).where(Topic.slug == slug)
[topic] = get_with_stat(topic_query)
if topic:
await update_follows_for_author(follower, 'topic', topic, False)
topic_unfollow(follower.id, slug)
elif what == 'COMMUNITY':
community_unfollow(follower.id, slug)
elif what == 'REACTIONS':
reactions_unfollow(follower.id, slug)
except Exception as e:
return {'error': str(e)}