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

This commit is contained in:
Untone 2024-05-30 19:42:38 +03:00
parent 74e000c96b
commit 968935869e
4 changed files with 33 additions and 25 deletions

View File

@ -12,8 +12,8 @@ from services.cache import (
get_cached_author,
get_cached_author_by_user_id,
get_cached_author_followers,
get_cached_author_follows_authors,
get_cached_author_follows_topics,
get_cached_follower_authors,
get_cached_follower_topics,
)
from services.db import local_session
from services.logger import root_logger as logger
@ -176,8 +176,8 @@ async def get_author_follows(_, _info, slug="", user=None, author_id=0):
if bool(author_id):
logger.debug(f"getting {author_id} follows authors")
authors = await get_cached_author_follows_authors(author_id)
topics = await get_cached_author_follows_topics(author_id)
authors = await get_cached_follower_authors(author_id)
topics = await get_cached_follower_topics(author_id)
return {
"topics": topics,
"authors": authors,
@ -193,8 +193,8 @@ async def get_author_follows(_, _info, slug="", user=None, author_id=0):
@query.field("get_author_follows_topics")
async def get_author_follows_topics(_, _info, slug="", user=None, author_id=None):
try:
author_id = get_author_id_from(slug, user, author_id)
topics = await get_cached_author_follows_topics(author_id)
follower_id = get_author_id_from(slug, user, author_id)
topics = await get_cached_follower_topics(follower_id)
return topics
except Exception:
import traceback
@ -205,8 +205,8 @@ async def get_author_follows_topics(_, _info, slug="", user=None, author_id=None
@query.field("get_author_follows_authors")
async def get_author_follows_authors(_, _info, slug="", user=None, author_id=None):
try:
author_id = get_author_id_from(slug, user, author_id)
return await get_cached_author_follows_authors(author_id)
follower_id = get_author_id_from(slug, user, author_id)
return await get_cached_follower_authors(follower_id)
except Exception:
import traceback

View File

@ -14,8 +14,8 @@ from services.cache import (
cache_author,
cache_topic,
get_cached_author_by_user_id,
get_cached_author_follows_authors,
get_cached_author_follows_topics,
get_cached_follower_authors,
get_cached_follower_topics,
)
from services.db import local_session
from services.logger import root_logger as logger
@ -48,14 +48,13 @@ async def follow(_, info, what, slug):
if not user_id or not follower_dict:
return {"error": "unauthorized"}
follower_id = follower_dict.get("id")
entity = what.lower()
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:
follows = await get_cached_follower_authors(follower_id)
[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)
@ -68,8 +67,10 @@ async def follow(_, info, what, slug):
elif what == "TOPIC":
error = topic_follow(follower_id, slug)
topic_dict = await cache_by_slug(what, slug)
await cache_topic(topic_dict)
if not error:
follows = await get_cached_follower_topics(follower_id)
topic_dict = await cache_by_slug(what, slug)
await cache_topic(topic_dict)
elif what == "COMMUNITY":
with local_session() as session:
@ -77,6 +78,11 @@ async def follow(_, info, what, slug):
elif what == "SHOUT":
error = reactions_follow(follower_id, slug)
if not error:
# TODO: follows = await get_cached_follower_reactions(follower_id)
# shout_dict = await cache_shout_by_slug(what, slug)
# await cache_topic(topic_dict)
pass
return {f"{entity}s": follows, "error": error}
@ -96,7 +102,7 @@ async def unfollow(_, info, what, slug):
follows = []
if what == "AUTHOR":
follows = await get_cached_author_follows_authors(follower_id)
follows = await get_cached_follower_authors(follower_id)
follower_id = int(follower_id)
error = author_unfollow(follower_id, slug)
# NOTE: after triggers should update cached stats
@ -115,7 +121,7 @@ async def unfollow(_, info, what, slug):
elif what == "TOPIC":
error = topic_unfollow(follower_id, slug)
if not error:
follows = await get_cached_author_follows_topics(follower_id)
follows = await get_cached_follower_topics(follower_id)
topic_dict = await cache_by_slug(what, slug)
await cache_topic(topic_dict)
@ -125,6 +131,8 @@ async def unfollow(_, info, what, slug):
elif what == "SHOUT":
error = reactions_unfollow(follower_id, slug)
if not error:
pass
return {"error": error, f"{entity}s": follows}
@ -142,8 +150,8 @@ async def get_follows_by_user_id(user_id: str):
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)
topics = await get_cached_follower_topics(author_id)
authors = await get_cached_follower_authors(author_id)
return {
"topics": topics or [],
"authors": authors or [],

View File

@ -174,9 +174,9 @@ type Invite {
}
type AuthorFollowsResult {
topics: [Int]
authors: [Int]
communities: [Int]
topics: [Topic]
authors: [Author]
communities: [Community]
error: String
}

View File

@ -1,6 +1,6 @@
import json
from typing import List
from sqlalchemy import and_, join, select
from orm.author import Author, AuthorFollower
@ -78,7 +78,7 @@ async def get_cached_author_by_user_id(user_id: str, get_with_stat):
return await get_cached_author(int(author_id), get_with_stat)
async def get_cached_authors_by_ids(authors_ids: List[int]):
async def get_cached_authors_by_ids(authors_ids: List[int]) -> List[Author | dict]:
authors = []
for author_id in authors_ids:
if author_id:
@ -159,7 +159,7 @@ async def get_cached_author_followers(author_id: int):
return followers
async def get_cached_author_follows_authors(author_id: int):
async def get_cached_follower_authors(author_id: int):
rkey = f"author:follows-authors:{author_id}"
authors_ids = []
cached = await redis.execute("GET", rkey)
@ -188,7 +188,7 @@ async def get_cached_topics_by_ids(topics_ids: List[int]):
return topics_objects
async def get_cached_author_follows_topics(author_id: int):
async def get_cached_follower_topics(author_id: int):
rkey = f"author:follows-topics:{author_id}"
topics_ids = []
cached = await redis.execute("GET", rkey)