unique-follows-debug
All checks were successful
Deploy on push / deploy (push) Successful in 35s

This commit is contained in:
Untone 2024-05-05 21:38:59 +03:00
parent 2b5fb704ba
commit 0d87d3d889
2 changed files with 13 additions and 18 deletions

View File

@ -59,8 +59,8 @@ async def follow(_, info, what, slug):
[author] = get_with_stat(select(Author).filter(Author.slug == slug)) [author] = get_with_stat(select(Author).filter(Author.slug == slug))
if author: if author:
author_dict = author.dict() author_dict = author.dict()
author_id = author.id author_id = int(author_dict.get('id', 0))
follows_ids = [a.get("id") for a in follows] follows_ids = set(int(a.get('id')) for a in follows)
if author_id not in follows_ids: if author_id not in follows_ids:
await cache_author(author_dict) await cache_author(author_dict)
await cache_author(follower_dict) await cache_author(follower_dict)

View File

@ -1,6 +1,5 @@
import json import json
from orm.topic import TopicFollower from orm.topic import TopicFollower
from services.encoders import CustomJSONEncoder from services.encoders import CustomJSONEncoder
from services.rediscache import redis from services.rediscache import redis
@ -14,21 +13,20 @@ DEFAULT_FOLLOWS = {
async def cache_author(author: dict): async def cache_author(author: dict):
author_id = author.get("id")
payload = json.dumps(author, cls=CustomJSONEncoder) payload = json.dumps(author, cls=CustomJSONEncoder)
await redis.execute("SET", f'user:{author.get("user")}', payload) await redis.execute("SET", f'user:{author.get("user")}', payload)
await redis.execute("SET", f'author:{author.get("id")}', payload) await redis.execute("SET", f'author:{author_id}', payload)
# update stat all field for followers' caches in <authors> list # update stat all field for followers' caches in <authors> list
followers_str = await redis.execute("GET", f'author:{author.get("id")}:followers') followers_str = await redis.execute("GET", f'author:{author_id}:followers')
followers = [] followers = []
if isinstance(followers_str, str): if isinstance(followers_str, str):
followers = json.loads(followers_str) followers = json.loads(followers_str)
if isinstance(followers, list): if isinstance(followers, list):
for follower in followers: for follower in followers:
follower_follows_authors = [] follower_follows_authors = []
follower_follows_authors_str = await redis.execute( follower_follows_authors_str = await redis.execute("GET", f'author:{author_id}:follows-authors')
"GET", f'author:{author.get("id")}:follows-authors'
)
if isinstance(follower_follows_authors_str, str): if isinstance(follower_follows_authors_str, str):
follower_follows_authors = json.loads(follower_follows_authors_str) follower_follows_authors = json.loads(follower_follows_authors_str)
c = 0 c = 0
@ -42,29 +40,26 @@ async def cache_author(author: dict):
follower_follows_authors.append(author) follower_follows_authors.append(author)
# update stat field for all authors' caches in <followers> list # update stat field for all authors' caches in <followers> list
follows_str = await redis.execute( follows_str = await redis.execute("GET", f'author:{author_id}:follows-authors')
"GET", f'author:{author.get("id")}:follows-authors'
)
follows_authors = [] follows_authors = []
if isinstance(follows_str, str): if isinstance(follows_str, str):
follows_authors = json.loads(follows_str) follows_authors = json.loads(follows_str)
if isinstance(follows_authors, list): if isinstance(follows_authors, list):
for followed_author in follows_authors: for followed_author in follows_authors:
followed_author_followers = [] followed_author_followers = []
followed_author_followers_str = await redis.execute( followed_author_followers_str = await redis.execute("GET", f'author:{author_id}:followers')
"GET", f'author:{author.get("id")}:followers'
)
if isinstance(followed_author_followers_str, str): if isinstance(followed_author_followers_str, str):
followed_author_followers = json.loads(followed_author_followers_str) followed_author_followers = json.loads(followed_author_followers_str)
c = 0 c = 0
for old_follower in followed_author_followers: for old_follower in followed_author_followers:
if int(old_follower.get("id")) == int(author.get("id", 0)): old_follower_id = int(old_follower.get("id"))
if old_follower_id == author_id:
followed_author_followers[c] = author followed_author_followers[c] = author
break # exit the loop since we found and updated the author break # exit the loop since we found and updated the author
c += 1 c += 1
else: # author not found in the list, so add the new author with the updated stat field
# author not found in the list, so add the new author with the updated stat field followed_author_followers.append(author)
followed_author_followers.append(author) await redis.execute("SET", f'author:{author_id}:followers', followed_author_followers)
async def cache_follows(follower: dict, entity_type: str, entity: dict, is_insert=True): async def cache_follows(follower: dict, entity_type: str, entity: dict, is_insert=True):