This commit is contained in:
parent
d93fa4cb4b
commit
53545605d0
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user