cache-reimplement-2
All checks were successful
Deploy on push / deploy (push) Successful in 1m4s

This commit is contained in:
2024-05-21 01:40:57 +03:00
parent 3742528e3a
commit 4c1fbf64a2
13 changed files with 292 additions and 326 deletions

View File

@@ -11,7 +11,7 @@ from resolvers.author import ( # search_authors,
)
from resolvers.community import get_communities_all, get_community
from resolvers.editor import create_shout, delete_shout, update_shout
from resolvers.follower import follow, get_shout_followers, get_topic_followers, unfollow
from resolvers.follower import follow, get_shout_followers, unfollow
from resolvers.notifier import (
load_notifications,
notification_mark_seen,
@@ -35,7 +35,14 @@ from resolvers.reader import (
load_shouts_search,
load_shouts_unrated,
)
from resolvers.topic import get_topic, get_topics_all, get_topics_by_author, get_topics_by_community
from resolvers.topic import (
get_topic,
get_topic_authors,
get_topic_followers,
get_topics_all,
get_topics_by_author,
get_topics_by_community,
)
from services.triggers import events_register
events_register()
@@ -44,6 +51,7 @@ __all__ = [
# author
"get_author",
"get_author_id",
"get_author_followers",
"get_author_follows",
"get_author_follows_topics",
"get_author_follows_authors",
@@ -60,6 +68,8 @@ __all__ = [
"get_topics_all",
"get_topics_by_community",
"get_topics_by_author",
"get_topic_followers",
"get_topic_authors",
# reader
"get_shout",
"load_shouts_by",
@@ -72,9 +82,7 @@ __all__ = [
# follower
"follow",
"unfollow",
"get_topic_followers",
"get_shout_followers",
"get_author_followers",
# editor
"create_shout",
"update_shout",

View File

@@ -1,19 +1,22 @@
import json
import time
from sqlalchemy import and_, desc, or_, select, text
from sqlalchemy.orm import aliased
from sqlalchemy import desc, or_, select, text
from orm.author import Author, AuthorFollower
from orm.author import Author
from orm.shout import ShoutAuthor, ShoutTopic
from orm.topic import Topic
from resolvers.stat import author_follows_authors, author_follows_topics, get_with_stat
from resolvers.stat import get_with_stat
from services.auth import login_required
from services.cache import cache_author, cache_follow_author_change
from services.cache import (
cache_author,
get_cached_author,
get_cached_author_by_user_id,
get_cached_author_followers,
get_cached_author_follows_authors,
get_cached_author_follows_topics,
)
from services.db import local_session
from services.encoders import CustomJSONEncoder
from services.logger import root_logger as logger
from services.rediscache import redis
from services.schema import mutation, query
@@ -70,10 +73,7 @@ async def get_author(_, _info, slug="", author_id=0):
if found_author:
logger.debug(f"found author id: {found_author.id}")
author_id = found_author.id if found_author.id else author_id
if author_id:
cached_result = await redis.execute("GET", f"author:{author_id}")
if isinstance(cached_result, str):
author_dict = json.loads(cached_result)
author_dict = await get_cached_author(author_id)
# update stat from db
if not author_dict or not author_dict.get("stat"):
@@ -97,17 +97,11 @@ async def get_author(_, _info, slug="", author_id=0):
async def get_author_by_user_id(user_id: str):
logger.info(f"getting author id for {user_id}")
redis_key = f"user:{user_id}"
author = None
try:
res = await redis.execute("GET", redis_key)
if isinstance(res, str):
author = json.loads(res)
author_id = author.get("id")
author_slug = author.get("slug")
if author_id:
logger.debug(f"got author @{author_slug} #{author_id} cached")
return author
author = await get_cached_author_by_user_id(user_id)
if author:
return author
author_query = select(Author).filter(Author.user == user_id)
result = get_with_stat(author_query)
@@ -156,11 +150,7 @@ async def load_authors_by(_, _info, by, limit, offset):
for [a] in authors_nostat:
author_dict = None
if isinstance(a, Author):
author_id = a.id
if bool(author_id):
cached_result = await redis.execute("GET", f"author:{author_id}")
if isinstance(cached_result, str):
author_dict = json.loads(cached_result)
author_dict = await get_cached_author(a.id)
if not author_dict or not isinstance(author_dict.get("shouts"), int):
break
@@ -196,28 +186,9 @@ async def get_author_follows(_, _info, slug="", user=None, author_id=0):
topics = []
authors = []
if bool(author_id):
rkey = f"author:{author_id}:follows-authors"
logger.debug(f"getting {author_id} follows authors")
cached = await redis.execute("GET", rkey)
if not cached:
authors = author_follows_authors(author_id) # type: ignore
prepared = [author.dict() for author in authors]
await redis.execute(
"SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder)
)
elif isinstance(cached, str):
authors = json.loads(cached)
rkey = f"author:{author_id}:follows-topics"
cached = await redis.execute("GET", rkey)
if cached and isinstance(cached, str):
topics = json.loads(cached)
if not cached:
topics = author_follows_topics(author_id) # type: ignore
prepared = [topic.dict() for topic in topics]
await redis.execute(
"SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder)
)
authors = await get_cached_author_follows_authors(author_id)
topics = await get_cached_author_follows_topics(author_id)
return {
"topics": topics,
"authors": authors,
@@ -244,19 +215,7 @@ async def get_author_follows_topics(_, _info, slug="", user=None, author_id=None
author_id = author_id_result[0] if author_id_result else None
if not author_id:
raise ValueError("Author not found")
logger.debug(f"getting {author_id} follows topics")
rkey = f"author:{author_id}:follows-topics"
cached = await redis.execute("GET", rkey)
topics = []
if isinstance(cached, str):
topics = json.loads(cached)
if not cached:
topics = author_follows_topics(author_id)
prepared = [topic.dict() for topic in topics]
await redis.execute(
"SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder)
)
return topics
return get_author_follows_topics(author_id)
@query.field("get_author_follows_authors")
@@ -269,22 +228,9 @@ async def get_author_follows_authors(_, _info, slug="", user=None, author_id=Non
.first()
)
author_id = author_id_result[0] if author_id_result else None
if author_id:
logger.debug(f"getting {author_id} follows authors")
rkey = f"author:{author_id}:follows-authors"
cached = await redis.execute("GET", rkey)
authors = []
if isinstance(cached, str):
authors = json.loads(cached)
if not authors:
authors = author_follows_authors(author_id)
prepared = [author.dict() for author in authors]
await redis.execute(
"SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder)
)
return authors
else:
if not author_id:
raise ValueError("Author not found")
return await get_cached_author_follows_authors(author_id)
def create_author(user_id: str, slug: str, name: str = ""):
@@ -307,51 +253,8 @@ def create_author(user_id: str, slug: str, name: str = ""):
@query.field("get_author_followers")
async def get_author_followers(_, _info, slug: str):
logger.debug(f"getting followers for @{slug}")
try:
author_alias = aliased(Author)
author_query = select(author_alias).filter(author_alias.slug == slug)
result = local_session().execute(author_query).first()
followers = []
if result:
[author] = result
author_id = author.id
cached = await redis.execute("GET", f"author:{author_id}:followers")
if cached:
followers_ids = []
followers = []
if isinstance(cached, str):
followers_cached = json.loads(cached)
if isinstance(followers_cached, list):
logger.debug(
f"@{slug} got {len(followers_cached)} followers cached"
)
for fc in followers_cached:
if fc["id"] not in followers_ids and fc["id"] != author_id:
followers.append(fc)
followers_ids.append(fc["id"])
return followers
author_follower_alias = aliased(AuthorFollower, name="af")
followers_query = select(Author).join(
author_follower_alias,
and_(
author_follower_alias.author == author_id,
author_follower_alias.follower == Author.id,
Author.id != author_id, # exclude the author from the followers
),
)
followers = get_with_stat(followers_query)
if isinstance(followers, list):
followers_ids = [r.id for r in followers]
for follower in followers:
if follower.id not in followers_ids:
await cache_follow_author_change(follower.dict(), author.dict())
followers_ids.append(follower.id)
logger.debug(f"@{slug} cache updated with {len(followers)} followers")
return followers
except Exception as exc:
import traceback
logger.error(exc)
logger.error(traceback.format_exc())
return []
author_query = select(Author.id).filter(Author.slug == slug).first()
author_id_result = local_session().execute(author_query)
author_id = author_id_result[0] if author_id_result else None
followers = await get_cached_author_followers(author_id)
return followers

View File

@@ -1,5 +1,3 @@
import json
import time
from typing import List
from sqlalchemy import select
@@ -10,13 +8,18 @@ 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 author_follows_authors, author_follows_topics, get_with_stat
from resolvers.stat import get_with_stat
from services.auth import login_required
from services.cache import DEFAULT_FOLLOWS, cache_author, cache_topic
from services.cache import (
cache_author,
cache_topic,
get_cached_author_by_user_id,
get_cached_author_follows_authors,
get_cached_author_follows_topics,
)
from services.db import local_session
from services.logger import root_logger as logger
from services.notify import notify_follower
from services.rediscache import redis
from services.schema import mutation, query
@@ -47,30 +50,28 @@ async def follow(_, info, what, slug):
follower_id = follower_dict.get("id")
entity = what.lower()
follows = []
follows_str = await redis.execute("GET", f"author:{follower_id}:follows-{entity}s")
if isinstance(follows_str, str):
follows = json.loads(follows_str) or []
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:
author_query = select(Author).filter(Author.slug == slug)
[author] = get_with_stat(author_query)
if author:
author_dict = author.dict()
author_id = int(author_dict.get("id", 0))
follows_ids = set(int(a.get("id")) for a in follows)
if author_id not in follows_ids:
[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)
await cache_author(follower_dict)
await notify_follower(follower_dict, author_id, "follow")
[author] = get_with_stat(select(Author).filter(Author.id == author_id))
if author:
author_dict = author.dict()
await cache_author(author_dict)
await cache_author(follower_dict)
await notify_follower(follower_dict, author_id, "follow")
follows.append(author_dict)
elif what == "TOPIC":
error = topic_follow(follower_id, slug)
_topic_dict = await cache_by_slug(what, slug)
topic_dict = await cache_by_slug(what, slug)
await cache_topic(topic_dict)
elif what == "COMMUNITY":
# FIXME: when more communities
@@ -95,31 +96,32 @@ async def unfollow(_, info, what, slug):
entity = what.lower()
follows = []
follows_str = await redis.execute("GET", f"author:{follower_id}:follows-{entity}s")
if isinstance(follows_str, str):
follows = json.loads(follows_str) or []
if what == "AUTHOR":
follows = await get_cached_author_follows_authors(follower_id)
follower_id = int(follower_id)
error = author_unfollow(follower_id, slug)
# NOTE: after triggers should update cached stats
if not error:
logger.info(f"@{follower_dict.get('slug')} unfollowed @{slug}")
author_query = select(Author).filter(Author.slug == slug)
[author] = get_with_stat(author_query)
if author:
author_dict = author.dict()
author_id = author.id
await cache_author(author_dict)
for idx, item in enumerate(follows):
if item["id"] == author_id:
await cache_author(follower_dict)
await notify_follower(follower_dict, author_id, "unfollow")
follows.pop(idx)
break
[author_id] = (
local_session().query(Author.id).filter(Author.slug == slug).first()
)
if author_id and author_id in follows:
follows.remove(author_id)
await cache_author(follower_dict)
await notify_follower(follower_dict, author_id, "follow")
[author] = get_with_stat(select(Author).filter(Author.id == author_id))
if author:
author_dict = author.dict()
await cache_author(author_dict)
elif what == "TOPIC":
error = topic_unfollow(follower_id, slug)
_topic_dict = await cache_by_slug(what, slug)
if not error:
follows = await get_cached_author_follows_topics(follower_id)
topic_dict = await cache_by_slug(what, slug)
await cache_topic(topic_dict)
elif what == "COMMUNITY":
follows = local_session().execute(select(Community))
@@ -133,36 +135,23 @@ async def unfollow(_, info, what, slug):
async def get_follows_by_user_id(user_id: str):
if not user_id:
return {"error": "unauthorized"}
author = await redis.execute("GET", f"user:{user_id}")
if isinstance(author, str):
author = json.loads(author)
author = await get_cached_author_by_user_id(user_id)
if not author:
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
if not author:
return {"error": "cant find author"}
author = author.dict()
last_seen = author.get("last_seen", 0) if isinstance(author, dict) else 0
follows = DEFAULT_FOLLOWS
day_old = int(time.time()) - last_seen > 24 * 60 * 60
if day_old:
author_id = json.loads(str(author)).get("id")
if author_id:
topics = author_follows_topics(author_id)
authors = author_follows_authors(author_id)
follows = {
"topics": topics,
"authors": authors,
"communities": [
{"id": 1, "name": "Дискурс", "slug": "discours", "pic": ""}
],
}
else:
logger.debug(f"getting follows for {user_id} from redis")
res = await redis.execute("GET", f"user:{user_id}:follows")
if isinstance(res, str):
follows = json.loads(res)
return follows
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)
return {
"topics": topics or [],
"authors": authors or [],
"communities": [{"id": 1, "name": "Дискурс", "slug": "discours", "pic": ""}],
}
def topic_follow(follower_id, slug):
@@ -282,17 +271,6 @@ def author_unfollow(follower_id, slug):
return "cant unfollow"
@query.field("get_topic_followers")
async def get_topic_followers(_, _info, slug: str) -> List[Author]:
topic_followers_query = select(Author)
topic_followers_query = (
topic_followers_query.join(TopicFollower, TopicFollower.follower == Author.id)
.join(Topic, Topic.id == TopicFollower.topic)
.filter(Topic.slug == slug)
)
return get_with_stat(topic_followers_query)
@query.field("get_shout_followers")
def get_shout_followers(
_, _info, slug: str = "", shout_id: int | None = None

View File

@@ -56,10 +56,9 @@ def get_topic_shouts_stat(topic_id: int):
return result[0] if result else 0
def get_topic_authors_stat(topic_id: int):
# authors
q = (
select(func.count(distinct(ShoutAuthor.author)))
def get_topic_authors_query(topic_id):
return (
select(ShoutAuthor.author)
.select_from(join(ShoutTopic, Shout, ShoutTopic.shout == Shout.id))
.join(ShoutAuthor, ShoutAuthor.shout == Shout.id)
.filter(
@@ -70,8 +69,18 @@ def get_topic_authors_stat(topic_id: int):
)
)
)
result = local_session().execute(q).first()
return result[0] if result else 0
def get_topic_authors_stat(topic_id: int):
# authors query
topic_authors_query = get_topic_authors_query(topic_id)
# Оборачиваем запрос в другой запрос, чтобы посчитать уникальных авторов
count_query = select(func.count(distinct(topic_authors_query.subquery().c.author)))
# Выполняем запрос и получаем результат
result = local_session().execute(count_query).scalar()
return result if result is not None else 0
def get_topic_followers_stat(topic_id: int):

View File

@@ -5,7 +5,9 @@ from orm.shout import ShoutTopic
from orm.topic import Topic
from resolvers.stat import get_with_stat
from services.auth import login_required
from services.cache import get_cached_topic_authors, get_cached_topic_followers
from services.db import local_session
from services.logger import root_logger as logger
from services.memorycache import cache_region
from services.schema import mutation, query
@@ -124,3 +126,23 @@ def get_topics_random(_, _info, amount=12):
topics.append(topic)
return topics
@query.field("get_topic_followers")
async def get_topic_followers(_, _info, slug: str):
logger.debug(f"getting followers for @{slug}")
topic_query = select(Topic.id).filter(Topic.slug == slug).first()
topic_id_result = local_session().execute(topic_query)
topic_id = topic_id_result[0] if topic_id_result else None
followers = await get_cached_topic_followers(topic_id)
return followers
@query.field("get_topic_authors")
async def get_topic_authors(_, _info, slug: str):
logger.debug(f"getting authors for @{slug}")
topic_query = select(Topic.id).filter(Topic.slug == slug).first()
topic_id_result = local_session().execute(topic_query)
topic_id = topic_id_result[0] if topic_id_result else None
authors = await get_cached_topic_authors(topic_id)
return authors