use-cached-authors

This commit is contained in:
2024-03-12 15:26:36 +03:00
parent d5a9a18c04
commit 26a527473f
2 changed files with 24 additions and 7 deletions

View File

@@ -1,3 +1,5 @@
import json
from sqlalchemy import func, distinct, select, join, and_, case, true, cast, Integer
from sqlalchemy.orm import aliased
@@ -7,6 +9,7 @@ from services.db import local_session
from orm.author import AuthorFollower, Author, AuthorRating
from orm.shout import ShoutTopic, ShoutAuthor, Shout
from services.logger import root_logger as logger
from services.rediscache import redis
def add_topic_stat_columns(q):
@@ -196,6 +199,20 @@ def get_with_stat(q):
return records
async def get_authors_with_stat_cached(q):
try:
records = []
with local_session() as session:
for x in session.execute(q):
stat_str = await redis.execute('GET', f'id:{x.id}:{'author'}')
if isinstance(stat_str, str):
x.stat = json.loads(stat_str).get('stat')
records.append(x)
except Exception as exc:
raise Exception(exc)
return records
def author_follows_authors(author_id: int):
af = aliased(AuthorFollower, name="af")
q = (