following-cache-anyway-found

This commit is contained in:
Untone 2024-05-05 20:16:45 +03:00
parent cfe9ac1005
commit 3ab42ecb72
4 changed files with 14 additions and 16 deletions

View File

@ -156,7 +156,7 @@ async def load_authors_by(_, _info, by, limit, offset):
for [a] in authors_nostat: for [a] in authors_nostat:
if isinstance(a, Author): if isinstance(a, Author):
author_id = a.id author_id = a.id
if author_id: if bool(author_id):
cached_result = await redis.execute("GET", f"author:{author_id}") cached_result = await redis.execute("GET", f"author:{author_id}")
if isinstance(cached_result, str): if isinstance(cached_result, str):
author_dict = json.loads(cached_result) author_dict = json.loads(cached_result)
@ -187,12 +187,12 @@ async def get_author_follows(_, _info, slug="", user=None, author_id=0):
author_id = author.id if not author_id else author_id author_id = author.id if not author_id else author_id
topics = [] topics = []
authors = [] authors = []
if author_id: if bool(author_id):
rkey = f"author:{author_id}:follows-authors" rkey = f"author:{author_id}:follows-authors"
logger.debug(f"getting {author_id} follows authors") logger.debug(f"getting {author_id} follows authors")
cached = await redis.execute("GET", rkey) cached = await redis.execute("GET", rkey)
if not cached: if not cached:
authors = author_follows_authors(author_id) authors = author_follows_authors(author_id) # type: ignore
prepared = [author.dict() for author in authors] prepared = [author.dict() for author in authors]
await redis.execute( await redis.execute(
"SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder) "SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder)
@ -205,7 +205,7 @@ async def get_author_follows(_, _info, slug="", user=None, author_id=0):
if cached and isinstance(cached, str): if cached and isinstance(cached, str):
topics = json.loads(cached) topics = json.loads(cached)
if not cached: if not cached:
topics = author_follows_topics(author_id) topics = author_follows_topics(author_id) # type: ignore
prepared = [topic.dict() for topic in topics] prepared = [topic.dict() for topic in topics]
await redis.execute( await redis.execute(
"SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder) "SET", rkey, json.dumps(prepared, cls=CustomJSONEncoder)

View File

@ -51,10 +51,7 @@ async def follow(_, info, what, slug):
follows = [] follows = []
follows_str = await redis.execute("GET", f"author:{follower_id}:follows-{entity}s") follows_str = await redis.execute("GET", f"author:{follower_id}:follows-{entity}s")
if isinstance(follows_str, str): if isinstance(follows_str, str):
follows = json.loads(follows_str) follows = json.loads(follows_str) or []
if not follows:
return {"error": "cant find following cache"}
if what == "AUTHOR": if what == "AUTHOR":
follower_id = int(follower_id) follower_id = int(follower_id)
@ -105,7 +102,7 @@ async def unfollow(_, info, what, slug):
follows = [] follows = []
follows_str = await redis.execute("GET", f"author:{follower_id}:follows-{entity}s") follows_str = await redis.execute("GET", f"author:{follower_id}:follows-{entity}s")
if isinstance(follows_str, str): if isinstance(follows_str, str):
follows = json.loads(follows_str) follows = json.loads(follows_str) or []
if what == "AUTHOR": if what == "AUTHOR":
error = author_unfollow(follower_id, slug) error = author_unfollow(follower_id, slug)

View File

@ -137,7 +137,7 @@ async def cache_topic(topic_dict: dict):
follower_follows_topics_str = await redis.execute( follower_follows_topics_str = await redis.execute(
"GET", f"author:{follower_id}:follows-topics" "GET", f"author:{follower_id}:follows-topics"
) )
if isinstance(follower_follows_topics, str): if isinstance(follower_follows_topics_str, str):
follower_follows_topics = json.loads(follower_follows_topics_str) follower_follows_topics = json.loads(follower_follows_topics_str)
c = 0 c = 0
for old_topic in follower_follows_topics: for old_topic in follower_follows_topics:
@ -149,7 +149,7 @@ async def cache_topic(topic_dict: dict):
# topic not found in the list, so add the new topic with the updated stat field # topic not found in the list, so add the new topic with the updated stat field
follower_follows_topics.append(topic_dict) follower_follows_topics.append(topic_dict)
await redis.set( await redis.execute('SET',
"SET", "SET",
f"author:{follower_id}:follows-topics", f"author:{follower_id}:follows-topics",
json.dumps(follower_follows_topics), json.dumps(follower_follows_topics),

View File

@ -109,34 +109,35 @@ def after_author_update(_mapper, _connection, author: Author):
result = get_with_stat(q) result = get_with_stat(q)
if result: if result:
[author_with_stat] = result [author_with_stat] = result
asyncio.create_task(cache_author(author_with_stat.dict())) if author_with_stat:
_task = asyncio.create_task(cache_author(author_with_stat.dict()))
def after_topic_follower_insert(_mapper, _connection, target: TopicFollower): def after_topic_follower_insert(_mapper, _connection, target: TopicFollower):
logger.info(target) logger.info(target)
asyncio.create_task( asyncio.create_task(
handle_topic_follower_change(target.topic, target.follower, True) handle_topic_follower_change(target.topic, target.follower, True) # type: ignore
) )
def after_topic_follower_delete(_mapper, _connection, target: TopicFollower): def after_topic_follower_delete(_mapper, _connection, target: TopicFollower):
logger.info(target) logger.info(target)
asyncio.create_task( asyncio.create_task(
handle_topic_follower_change(target.topic, target.follower, False) handle_topic_follower_change(target.topic, target.follower, False) # type: ignore
) )
def after_author_follower_insert(_mapper, _connection, target: AuthorFollower): def after_author_follower_insert(_mapper, _connection, target: AuthorFollower):
logger.info(target) logger.info(target)
asyncio.create_task( asyncio.create_task(
handle_author_follower_change(target.author, target.follower, True) handle_author_follower_change(target.author, target.follower, True) # type: ignore
) )
def after_author_follower_delete(_mapper, _connection, target: AuthorFollower): def after_author_follower_delete(_mapper, _connection, target: AuthorFollower):
logger.info(target) logger.info(target)
asyncio.create_task( asyncio.create_task(
handle_author_follower_change(target.author, target.follower, False) handle_author_follower_change(target.author, target.follower, False) # type: ignore
) )