This commit is contained in:
parent
ceeeb23c26
commit
fac25ab4f4
|
@ -9,7 +9,7 @@ from orm.shout import ShoutAuthor, ShoutTopic
|
||||||
from orm.topic import Topic
|
from orm.topic import Topic
|
||||||
from resolvers.stat import author_follows_authors, author_follows_topics, get_with_stat
|
from resolvers.stat import author_follows_authors, author_follows_topics, get_with_stat
|
||||||
from services.auth import login_required
|
from services.auth import login_required
|
||||||
from services.cache import cache_author, cache_follower
|
from services.cache import cache_author, cache_follow_author_change
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from services.encoders import CustomJSONEncoder
|
from services.encoders import CustomJSONEncoder
|
||||||
from services.logger import root_logger as logger
|
from services.logger import root_logger as logger
|
||||||
|
@ -332,7 +332,7 @@ async def get_author_followers(_, _info, slug: str):
|
||||||
results = get_with_stat(q)
|
results = get_with_stat(q)
|
||||||
if isinstance(results, list):
|
if isinstance(results, list):
|
||||||
for follower in results:
|
for follower in results:
|
||||||
await cache_follower(follower.dict(), author.dict())
|
await cache_follow_author_change(follower.dict(), author.dict())
|
||||||
logger.debug(f"@{slug} cache updated with {len(results)} followers")
|
logger.debug(f"@{slug} cache updated with {len(results)} followers")
|
||||||
return results
|
return results
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
|
|
|
@ -94,15 +94,15 @@ async def cache_follows(follower: dict, entity_type: str, entity: dict, is_inser
|
||||||
await redis.execute("SET", redis_key, payload)
|
await redis.execute("SET", redis_key, payload)
|
||||||
|
|
||||||
# update follower's stats everywhere
|
# update follower's stats everywhere
|
||||||
author_str = await redis.execute("GET", f"author:{follower_id}")
|
follower_str = await redis.execute("GET", f"author:{follower_id}")
|
||||||
if isinstance(author_str, str):
|
if isinstance(follower_str, str):
|
||||||
author = json.loads(author_str)
|
follower = json.loads(follower_str)
|
||||||
author["stat"][f"{entity_type}s"] = len(follows)
|
follower["stat"][f"{entity_type}s"] = len(follows)
|
||||||
await cache_author(author)
|
await cache_author(follower)
|
||||||
return follows
|
return follows
|
||||||
|
|
||||||
|
|
||||||
async def cache_follower(follower: dict, author: dict, is_insert=True):
|
async def cache_follow_author_change(follower: dict, author: dict, is_insert=True):
|
||||||
author_id = author.get("id")
|
author_id = author.get("id")
|
||||||
follower_id = follower.get("id")
|
follower_id = follower.get("id")
|
||||||
followers = []
|
followers = []
|
||||||
|
@ -110,26 +110,35 @@ async def cache_follower(follower: dict, author: dict, is_insert=True):
|
||||||
redis_key = f"author:{author_id}:followers"
|
redis_key = f"author:{author_id}:followers"
|
||||||
followers_str = await redis.execute("GET", redis_key)
|
followers_str = await redis.execute("GET", redis_key)
|
||||||
followers = json.loads(followers_str) if isinstance(followers_str, str) else []
|
followers = json.loads(followers_str) if isinstance(followers_str, str) else []
|
||||||
if is_insert and not any([int(f["id"]) == author_id for f in followers]):
|
|
||||||
|
# Remove the author from the list of followers, if present
|
||||||
|
followers = [f for f in followers if f["id"] != author_id]
|
||||||
|
|
||||||
|
# If inserting, add the new follower to the list if not already present
|
||||||
|
if is_insert and not any(f["id"] == follower_id for f in followers):
|
||||||
followers.append(follower)
|
followers.append(follower)
|
||||||
|
|
||||||
|
# Remove the follower from the list if not inserting and present
|
||||||
else:
|
else:
|
||||||
followers = [e for e in followers if int(e["id"]) != author_id]
|
followers = [f for f in followers if f["id"] != follower_id]
|
||||||
|
|
||||||
followers = [
|
# Ensure followers are unique based on their 'id' field
|
||||||
dict(d)
|
followers = list({f["id"]: f for f in followers}.values())
|
||||||
for d in set(tuple(tuple(k, v) for k, v in d.items()) for d in followers)
|
|
||||||
]
|
# Update follower's stats everywhere
|
||||||
|
follower_str = await redis.execute("GET", f"author:{follower_id}")
|
||||||
|
if isinstance(follower_str, str):
|
||||||
|
follower = json.loads(follower_str)
|
||||||
|
follower["stat"]["followers"] = len(followers)
|
||||||
|
await cache_author(follower)
|
||||||
|
|
||||||
author_str = await redis.execute("GET", f"author:{follower_id}")
|
|
||||||
if isinstance(author_str, str):
|
|
||||||
author = json.loads(author_str)
|
|
||||||
author["stat"]["followers"] = len(followers)
|
|
||||||
await cache_author(author)
|
|
||||||
payload = json.dumps(followers, cls=CustomJSONEncoder)
|
payload = json.dumps(followers, cls=CustomJSONEncoder)
|
||||||
await redis.execute("SET", redis_key, payload)
|
await redis.execute("SET", redis_key, payload)
|
||||||
|
|
||||||
return followers
|
return followers
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def cache_topic(topic_dict: dict):
|
async def cache_topic(topic_dict: dict):
|
||||||
# update stat all field for followers' caches in <topics> list
|
# update stat all field for followers' caches in <topics> list
|
||||||
followers = (
|
followers = (
|
||||||
|
|
|
@ -8,7 +8,7 @@ from orm.reaction import Reaction
|
||||||
from orm.shout import Shout, ShoutAuthor
|
from orm.shout import Shout, ShoutAuthor
|
||||||
from orm.topic import Topic, TopicFollower
|
from orm.topic import Topic, TopicFollower
|
||||||
from resolvers.stat import get_with_stat
|
from resolvers.stat import get_with_stat
|
||||||
from services.cache import cache_author, cache_follower, cache_follows
|
from services.cache import cache_author, cache_follow_author_change, cache_follows
|
||||||
from services.encoders import CustomJSONEncoder
|
from services.encoders import CustomJSONEncoder
|
||||||
from services.logger import root_logger as logger
|
from services.logger import root_logger as logger
|
||||||
from services.rediscache import redis
|
from services.rediscache import redis
|
||||||
|
@ -30,9 +30,8 @@ async def handle_author_follower_change(
|
||||||
[follower] = get_with_stat(follower_query)
|
[follower] = get_with_stat(follower_query)
|
||||||
if follower and author:
|
if follower and author:
|
||||||
await cache_author(author.dict())
|
await cache_author(author.dict())
|
||||||
await cache_author(follower.dict())
|
await cache_follows(follower.dict(), "author", author.dict(), is_insert) # cache_author(follower_dict) inside
|
||||||
await cache_follows(follower.dict(), "author", author.dict(), is_insert)
|
await cache_follow_author_change(follower.dict(), author.dict(), is_insert) # cache_author(follower_dict) inside
|
||||||
await cache_follower(follower.dict(), author.dict(), is_insert)
|
|
||||||
|
|
||||||
|
|
||||||
async def handle_topic_follower_change(
|
async def handle_topic_follower_change(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user