From 53545605d0b0781c8d8c607a6927df51f122df33 Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 5 Jun 2024 21:40:32 +0300 Subject: [PATCH] fixed-redis-cache-4 --- services/precache.py | 83 +++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/services/precache.py b/services/precache.py index df40cd46..b6c9f27f 100644 --- a/services/precache.py +++ b/services/precache.py @@ -100,47 +100,50 @@ async def precache_topics_followers(topic_id: int, session): async def precache_data(): - # cache reset - await redis.execute("FLUSHDB") - logger.info("redis flushed") + try: + # cache reset + await redis.execute("FLUSHDB") + logger.info("redis flushed") - # authors - authors_by_id = {} - authors = get_with_stat(select(Author).where(Author.user.is_not(None))) - for author in authors: - profile = author.dict() if not isinstance(author, dict) else author - author_id = profile.get("id") - if author_id: - authors_by_id[author_id] = profile - user_id = profile["user"] - author_payload = json.dumps(profile, cls=CustomJSONEncoder) - await redis.execute("SET", f"author:id:{author_id}", author_payload) - await redis.execute("SET", f"author:user:{user_id}", author_payload) - logger.info(f"{len(authors)} authors precached") + # authors + authors_by_id = {} + authors = get_with_stat(select(Author).where(Author.user.is_not(None))) + for author in authors: + profile = author.dict() if not isinstance(author, dict) else author + author_id = profile.get("id") + if author_id: + authors_by_id[author_id] = profile + user_id = profile["user"] + author_payload = json.dumps(profile, cls=CustomJSONEncoder) + await redis.execute("SET", f"author:id:{author_id}", author_payload) + await redis.execute("SET", f"author:user:{user_id}", author_payload) + logger.info(f"{len(authors)} authors precached") - # followings for authors - with local_session() as session: - for author_id in authors_by_id.keys(): - await precache_authors_followers(author_id, session) - await precache_authors_follows(author_id, session) - logger.info("authors followings precached") + # followings for authors + with local_session() as session: + for author_id in authors_by_id.keys(): + await precache_authors_followers(author_id, session) + await precache_authors_follows(author_id, session) + logger.info("authors followings precached") - # topics - topics_by_id = {} - topics = get_with_stat(select(Topic)) - for topic in topics: - topic_profile = topic.dict() if not isinstance(topic, dict) else topic - topic_id = topic_profile.get("id") - topics_by_id[topic_id] = topic_profile - topic_slug = topic_profile["slug"] - 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:slug:{topic_slug}", topic_payload) - logger.info(f"{len(topics)} topics precached") + # topics + topics_by_id = {} + topics = get_with_stat(select(Topic)) + for topic in topics: + topic_profile = topic.dict() if not isinstance(topic, dict) else topic + topic_id = topic_profile.get("id") + topics_by_id[topic_id] = topic_profile + topic_slug = topic_profile["slug"] + 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:slug:{topic_slug}", topic_payload) + logger.info(f"{len(topics)} topics precached") - # followings for topics - with local_session() as session: - for topic_id in topics_by_id.keys(): - await precache_topics_followers(topic_id, session) - await precache_topics_authors(topic_id, session) - logger.info("topics followings precached") + # followings for topics + with local_session() as session: + for topic_id in topics_by_id.keys(): + await precache_topics_followers(topic_id, session) + await precache_topics_authors(topic_id, session) + logger.info("topics followings precached") + except Exception as exc: + logger.error(exc)