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

View File

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

View File

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

View File

@ -1,6 +1,6 @@
import json import json
from typing import List from typing import List
from sqlalchemy import and_, join, select from sqlalchemy import and_, join, select
from orm.author import Author, AuthorFollower 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) 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 = [] authors = []
for author_id in authors_ids: for author_id in authors_ids:
if author_id: if author_id:
@ -159,7 +159,7 @@ async def get_cached_author_followers(author_id: int):
return followers 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}" rkey = f"author:follows-authors:{author_id}"
authors_ids = [] authors_ids = []
cached = await redis.execute("GET", rkey) cached = await redis.execute("GET", rkey)
@ -188,7 +188,7 @@ async def get_cached_topics_by_ids(topics_ids: List[int]):
return topics_objects 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}" rkey = f"author:follows-topics:{author_id}"
topics_ids = [] topics_ids = []
cached = await redis.execute("GET", rkey) cached = await redis.execute("GET", rkey)