fixed-redis-cache-4
All checks were successful
Deploy on push / deploy (push) Successful in 1m9s

This commit is contained in:
Untone 2024-06-05 21:40:32 +03:00
parent d93fa4cb4b
commit 53545605d0

View File

@ -100,47 +100,50 @@ async def precache_topics_followers(topic_id: int, session):
async def precache_data(): async def precache_data():
# cache reset try:
await redis.execute("FLUSHDB") # cache reset
logger.info("redis flushed") await redis.execute("FLUSHDB")
logger.info("redis flushed")
# authors # authors
authors_by_id = {} authors_by_id = {}
authors = get_with_stat(select(Author).where(Author.user.is_not(None))) authors = get_with_stat(select(Author).where(Author.user.is_not(None)))
for author in authors: for author in authors:
profile = author.dict() if not isinstance(author, dict) else author profile = author.dict() if not isinstance(author, dict) else author
author_id = profile.get("id") author_id = profile.get("id")
if author_id: if author_id:
authors_by_id[author_id] = profile authors_by_id[author_id] = profile
user_id = profile["user"] user_id = profile["user"]
author_payload = json.dumps(profile, cls=CustomJSONEncoder) author_payload = json.dumps(profile, cls=CustomJSONEncoder)
await redis.execute("SET", f"author:id:{author_id}", author_payload) await redis.execute("SET", f"author:id:{author_id}", author_payload)
await redis.execute("SET", f"author:user:{user_id}", author_payload) await redis.execute("SET", f"author:user:{user_id}", author_payload)
logger.info(f"{len(authors)} authors precached") logger.info(f"{len(authors)} authors precached")
# followings for authors # followings for authors
with local_session() as session: with local_session() as session:
for author_id in authors_by_id.keys(): for author_id in authors_by_id.keys():
await precache_authors_followers(author_id, session) await precache_authors_followers(author_id, session)
await precache_authors_follows(author_id, session) await precache_authors_follows(author_id, session)
logger.info("authors followings precached") logger.info("authors followings precached")
# topics # topics
topics_by_id = {} topics_by_id = {}
topics = get_with_stat(select(Topic)) topics = get_with_stat(select(Topic))
for topic in topics: for topic in topics:
topic_profile = topic.dict() if not isinstance(topic, dict) else topic topic_profile = topic.dict() if not isinstance(topic, dict) else topic
topic_id = topic_profile.get("id") topic_id = topic_profile.get("id")
topics_by_id[topic_id] = topic_profile topics_by_id[topic_id] = topic_profile
topic_slug = topic_profile["slug"] topic_slug = topic_profile["slug"]
topic_payload = json.dumps(topic_profile, cls=CustomJSONEncoder) topic_payload = json.dumps(topic_profile, cls=CustomJSONEncoder)
await redis.execute("SET", f"topic:id:{topic_id}", topic_payload) await redis.execute("SET", f"topic:id:{topic_id}", topic_payload)
await redis.execute("SET", f"topic:slug:{topic_slug}", topic_payload) await redis.execute("SET", f"topic:slug:{topic_slug}", topic_payload)
logger.info(f"{len(topics)} topics precached") logger.info(f"{len(topics)} topics precached")
# followings for topics # followings for topics
with local_session() as session: with local_session() as session:
for topic_id in topics_by_id.keys(): for topic_id in topics_by_id.keys():
await precache_topics_followers(topic_id, session) await precache_topics_followers(topic_id, session)
await precache_topics_authors(topic_id, session) await precache_topics_authors(topic_id, session)
logger.info("topics followings precached") logger.info("topics followings precached")
except Exception as exc:
logger.error(exc)