faster-get-author
Some checks failed
Deploy on push / deploy (push) Failing after 8s

This commit is contained in:
Untone 2024-02-25 19:02:15 +03:00
parent 1e922e3161
commit b12db9af0e
2 changed files with 16 additions and 10 deletions

View File

@ -40,16 +40,22 @@ def get_authors_all(_, _info):
async def get_author(_, _info, slug='', author_id=None): async def get_author(_, _info, slug='', author_id=None):
q = None q = None
author = None author = None
cache = None
try: try:
if slug or author_id: if slug:
if bool(slug): with local_session() as session:
q = select(Author).where(Author.slug == slug) q = select(Author).filter(Author.slug == slug)
if author_id: [author] = session.execute(q)
q = select(Author).where(Author.id == author_id) author_id = author.id
if author_id:
[author] = get_with_stat(q) cache = await redis.execute('GET', f'id:{author_id}:author')
if author: if not cache:
await update_author_cache(author) q = select(Author).where(Author.id == author_id)
[author] = get_with_stat(q)
if author:
await update_author_cache(author)
else:
author = json.loads(cache)
except Exception as exc: except Exception as exc:
logger.error(exc) logger.error(exc)
return author return author

View File

@ -21,7 +21,7 @@ DEFAULT_FOLLOWS = {
async def update_author_cache(author: Author, ttl=25 * 60 * 60): async def update_author_cache(author: Author, ttl=25 * 60 * 60):
payload = json.dumps(author.dict()) payload = json.dumps(author.dict())
await redis.execute('SETEX', f'user:{author.user}:author', ttl, payload) await redis.execute('SETEX', f'user:{author.user}:author', ttl, payload)
await redis.execute('SETEX', f'id:{author.user}:author', ttl, payload) await redis.execute('SETEX', f'id:{author.id}:author', ttl, payload)
@event.listens_for(Shout, 'after_insert') @event.listens_for(Shout, 'after_insert')