caching-fixes

This commit is contained in:
Untone 2024-03-28 14:05:06 +03:00
parent 7907e5bc4f
commit 6f016f236d
2 changed files with 29 additions and 24 deletions

View File

@ -61,9 +61,10 @@ async def get_author(_, _info, slug='', author_id=None):
logger.debug(f'GET {cache_key} -> {cache}') logger.debug(f'GET {cache_key} -> {cache}')
q = select(Author).where(Author.id == author_id) q = select(Author).where(Author.id == author_id)
author_dict = None author_dict = None
if cache: if cache and isinstance(cache, str):
author_dict = json.loads(cache) author_dict = json.loads(cache)
else: else:
result = await get_authors_with_stat_cached(q) result = await get_authors_with_stat_cached(q)
if result: if result:
[author] = result [author] = result
@ -213,17 +214,19 @@ async def get_author_follows_topics(_, _info, slug='', user=None, author_id=None
.first() .first()
) )
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 not author_id:
raise ValueError('Author not found')
logger.debug(f'getting {author_id} follows topics') logger.debug(f'getting {author_id} follows topics')
rkey = f'author:{author_id}:follows-topics' rkey = f'author:{author_id}:follows-topics'
cached = await redis.execute('GET', rkey) cached = await redis.execute('GET', rkey)
topics = json.loads(cached) if cached else author_follows_topics(author_id) topics = []
if isinstance(cached, str):
topics = json.loads(cached)
if not cached: if not cached:
topics = author_follows_topics(author_id)
prepared = [topic.dict() for topic in topics] prepared = [topic.dict() for topic in topics]
await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder)) await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder))
return topics return topics
else:
raise ValueError('Author not found')
@query.field('get_author_follows_authors') @query.field('get_author_follows_authors')
@ -240,10 +243,11 @@ async def get_author_follows_authors(_, _info, slug='', user=None, author_id=Non
logger.debug(f'getting {author_id} follows authors') logger.debug(f'getting {author_id} follows authors')
rkey = f'author:{author_id}:follows-authors' rkey = f'author:{author_id}:follows-authors'
cached = await redis.execute('GET', rkey) cached = await redis.execute('GET', rkey)
authors = ( authors = []
json.loads(cached) if cached else author_follows_authors(author_id) if isinstance(cached, str):
) authors = json.loads(cached)
if not cached: if not authors:
authors = author_follows_authors(author_id)
prepared = [author.dict() for author in authors] prepared = [author.dict() for author in authors]
await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder)) await redis.execute('SET', rkey, json.dumps(prepared, cls=CustomJSONEncoder))
return authors return authors
@ -300,6 +304,7 @@ async def get_author_followers(_, _info, slug: str):
return results return results
else: else:
logger.debug(f'@{slug} got followers cached') logger.debug(f'@{slug} got followers cached')
if isinstance(cached, str):
return json.loads(cached) return json.loads(cached)
except Exception as exc: except Exception as exc:
import traceback import traceback

View File

@ -87,9 +87,9 @@ async def update_follows_for_author(
follows = [e for e in follows if e["id"] != entity_id] follows = [e for e in follows if e["id"] != entity_id]
logger.debug(f'{entity['slug']} removed from what @{follower.slug} follows') logger.debug(f'{entity['slug']} removed from what @{follower.slug} follows')
if entity_type == "topic": if entity_type == "topic":
await set_follows_topics_cache(follows, follower.id) await set_follows_topics_cache(follows, follower.id.scalar())
if entity_type == "author": if entity_type == "author":
await set_follows_authors_cache(follows, follower.id) await set_follows_authors_cache(follows, follower.id.scalar())
return follows return follows
@ -166,25 +166,25 @@ def after_author_update(_mapper, _connection, author: Author):
def after_topic_follower_insert(_mapper, _connection, target: TopicFollower): def after_topic_follower_insert(_mapper, _connection, target: TopicFollower):
asyncio.create_task( asyncio.create_task(
handle_topic_follower_change(target.topic, target.follower, True) handle_topic_follower_change(target.topic.scalar(), target.follower.scalar(), True)
) )
def after_topic_follower_delete(_mapper, _connection, target: TopicFollower): def after_topic_follower_delete(_mapper, _connection, target: TopicFollower):
asyncio.create_task( asyncio.create_task(
handle_topic_follower_change(target.topic, target.follower, False) handle_topic_follower_change(target.topic.scalar(), target.follower.scalar(), False)
) )
def after_author_follower_insert(_mapper, _connection, target: AuthorFollower): def after_author_follower_insert(_mapper, _connection, target: AuthorFollower):
asyncio.create_task( asyncio.create_task(
handle_author_follower_change(target.author, target.follower, True) handle_author_follower_change(target.author.scalar(), target.follower.scalar(), True)
) )
def after_author_follower_delete(_mapper, _connection, target: AuthorFollower): def after_author_follower_delete(_mapper, _connection, target: AuthorFollower):
asyncio.create_task( asyncio.create_task(
handle_author_follower_change(target.author, target.follower, False) handle_author_follower_change(target.author.scalar(), target.follower.scalar(), False)
) )
@ -200,7 +200,7 @@ async def handle_author_follower_change(
follows_authors = await redis.execute( follows_authors = await redis.execute(
"GET", f"author:{follower_id}:follows-authors" "GET", f"author:{follower_id}:follows-authors"
) )
if follows_authors: if isinstance(follows_authors, str):
follows_authors = json.loads(follows_authors) follows_authors = json.loads(follows_authors)
if not any(x.get("id") == author.id for x in follows_authors): if not any(x.get("id") == author.id for x in follows_authors):
follows_authors.append(author.dict()) follows_authors.append(author.dict())
@ -233,7 +233,7 @@ async def handle_topic_follower_change(
follows_topics = await redis.execute( follows_topics = await redis.execute(
"GET", f"author:{follower_id}:follows-topics" "GET", f"author:{follower_id}:follows-topics"
) )
if follows_topics: if isinstance(follows_topics, str):
follows_topics = json.loads(follows_topics) follows_topics = json.loads(follows_topics)
if not any(x.get("id") == topic.id for x in follows_topics): if not any(x.get("id") == topic.id for x in follows_topics):
follows_topics.append(topic) follows_topics.append(topic)