followers-cached
Some checks failed
Deploy on push / deploy (push) Failing after 5s

This commit is contained in:
Untone 2024-02-29 10:23:08 +03:00
parent caf45f3d42
commit f774c54cc2
2 changed files with 8 additions and 1 deletions

View File

@ -1,3 +1,4 @@
import asyncio
import json import json
import time import time
@ -9,7 +10,7 @@ from orm.author import Author, AuthorFollower
from orm.shout import ShoutAuthor, ShoutTopic from orm.shout import ShoutAuthor, ShoutTopic
from orm.topic import Topic from orm.topic import Topic
from resolvers.stat import get_with_stat, author_follows_authors, author_follows_topics from resolvers.stat import get_with_stat, author_follows_authors, author_follows_topics
from services.cache import update_author_cache from services.cache import update_author_cache, update_author_followers_cache
from services.auth import login_required from services.auth import login_required
from services.db import local_session from services.db import local_session
from services.rediscache import redis from services.rediscache import redis
@ -242,6 +243,7 @@ async def get_author_followers(_, _info, slug: str):
) )
) )
results = get_with_stat(q) results = get_with_stat(q)
_ = asyncio.create_task(update_author_followers_cache(author_id, results))
return json.loads(cached) if cached else results return json.loads(cached) if cached else results
except Exception as exc: except Exception as exc:
import traceback import traceback

View File

@ -25,6 +25,11 @@ async def update_author_cache(author: dict, ttl=25 * 60 * 60):
await redis.execute('SETEX', f'id:{author.get("id")}:author', ttl, payload) await redis.execute('SETEX', f'id:{author.get("id")}:author', ttl, payload)
async def update_author_followers_cache(author_id: int, followers, ttl=25 * 60 * 60):
payload = json.dumps(followers)
await redis.execute('SET', f'author:{author_id}:followers', payload)
async def update_follows_topics_cache(follows, author_id: int, ttl=25 * 60 * 60): async def update_follows_topics_cache(follows, author_id: int, ttl=25 * 60 * 60):
try: try:
payload = json.dumps(follows) payload = json.dumps(follows)