caching-follows
All checks were successful
Deploy on push / deploy (push) Successful in 23s

This commit is contained in:
Untone 2024-02-26 04:58:27 +03:00
parent f40eff2822
commit 732bd2b098
2 changed files with 26 additions and 16 deletions

View File

@ -122,7 +122,7 @@ def load_authors_by(_, _info, by, limit, offset):
@query.field('get_author_follows') @query.field('get_author_follows')
def get_author_follows(_, _info, slug='', user=None, author_id=None): async def get_author_follows(_, _info, slug='', user=None, author_id=None):
with local_session() as session: with local_session() as session:
if user or slug: if user or slug:
author_id_result = ( author_id_result = (
@ -132,9 +132,11 @@ def get_author_follows(_, _info, slug='', user=None, author_id=None):
) )
author_id = author_id_result[0] if author_id_result else None author_id = author_id_result[0] if author_id_result else None
if author_id: if author_id:
logger.debug(f'getting {author_id} follows') logger.debug(f'getting {author_id} follows authors')
topics = author_follows_topics(author_id) cached = await redis.execute(f'id:{author_id}:follows-authors')
authors = author_follows_authors(author_id) topics = json.loads(cached) if cached else author_follows_topics(author_id)
cached = await redis.execute(f'id:{author_id}:follows-topics')
authors = json.loads(cached) if cached else author_follows_authors(author_id)
return { return {
'topics': topics, 'topics': topics,
'authors': authors, 'authors': authors,
@ -147,7 +149,7 @@ def get_author_follows(_, _info, slug='', user=None, author_id=None):
@query.field('get_author_follows_topics') @query.field('get_author_follows_topics')
def get_author_follows_topics(_, _info, slug='', user=None, author_id=None): async def get_author_follows_topics(_, _info, slug='', user=None, author_id=None):
with local_session() as session: with local_session() as session:
if user or slug: if user or slug:
author_id_result = ( author_id_result = (
@ -158,14 +160,18 @@ def get_author_follows_topics(_, _info, slug='', user=None, author_id=None):
author_id = author_id_result[0] if author_id_result else None author_id = author_id_result[0] if author_id_result else None
if author_id: if author_id:
logger.debug(f'getting {author_id} follows topics') logger.debug(f'getting {author_id} follows topics')
follows = author_follows_topics(author_id) rkey = f'id:{author_id}:follows-topics'
return follows cached = await redis.execute(rkey)
topics = json.loads(cached) if cached else author_follows_topics(author_id)
if not cached:
await redis.execute('SETEX', rkey, json.dumps(topics), 24*60*60)
return topics
else: else:
raise ValueError('Author not found') raise ValueError('Author not found')
@query.field('get_author_follows_authors') @query.field('get_author_follows_authors')
def get_author_follows_authors(_, _info, slug='', user=None, author_id=None): async def get_author_follows_authors(_, _info, slug='', user=None, author_id=None):
with local_session() as session: with local_session() as session:
if user or slug: if user or slug:
author_id_result = ( author_id_result = (
@ -176,8 +182,12 @@ def get_author_follows_authors(_, _info, slug='', user=None, author_id=None):
author_id = author_id_result[0] if author_id_result else None author_id = author_id_result[0] if author_id_result else None
if author_id: if author_id:
logger.debug(f'getting {author_id} follows authors') logger.debug(f'getting {author_id} follows authors')
follows = author_follows_authors(author_id) rkey = f'id:{author_id}:follows-authors'
return follows cached = await redis.execute(rkey)
authors = json.loads(cached) if cached else author_follows_authors(author_id)
if not cached:
await redis.execute('SETEX', rkey, json.dumps(authors), 24*60*60)
return authors
else: else:
raise ValueError('Author not found') raise ValueError('Author not found')

View File

@ -29,22 +29,22 @@ async def update_follows_topics_cache(follows, author_id: int, ttl=25 * 60 * 60)
try: try:
payload = json.dumps(follows) payload = json.dumps(follows)
await redis.execute('SETEX', f'author:{author_id}:follows-topics', ttl, payload) await redis.execute('SETEX', f'author:{author_id}:follows-topics', ttl, payload)
except Exception as exc: except Exception:
logger.error(exc)
import traceback import traceback
traceback.print_exc() exc = traceback.format_exc()
logger.error(exc)
async def update_follows_authors_cache(follows, author_id: int, ttl=25 * 60 * 60): async def update_follows_authors_cache(follows, author_id: int, ttl=25 * 60 * 60):
try: try:
payload = json.dumps(follows) payload = json.dumps(follows)
await redis.execute('SETEX', f'author:{author_id}:follows-authors', ttl, payload) await redis.execute('SETEX', f'author:{author_id}:follows-authors', ttl, payload)
except Exception as exc: except Exception:
logger.error(exc)
import traceback import traceback
traceback.print_exc() exc = traceback.format_exc()
logger.error(exc)
@event.listens_for(Shout, 'after_insert') @event.listens_for(Shout, 'after_insert')