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):
q = None
author = None
cache = None
try:
if slug or author_id:
if bool(slug):
q = select(Author).where(Author.slug == slug)
if author_id:
q = select(Author).where(Author.id == author_id)
[author] = get_with_stat(q)
if author:
await update_author_cache(author)
if slug:
with local_session() as session:
q = select(Author).filter(Author.slug == slug)
[author] = session.execute(q)
author_id = author.id
if author_id:
cache = await redis.execute('GET', f'id:{author_id}:author')
if not cache:
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:
logger.error(exc)
return author

View File

@ -21,7 +21,7 @@ DEFAULT_FOLLOWS = {
async def update_author_cache(author: Author, ttl=25 * 60 * 60):
payload = json.dumps(author.dict())
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')