tests-passed
This commit is contained in:
0
cache/__init__.py
vendored
Normal file
0
cache/__init__.py
vendored
Normal file
10
cache/cache.py
vendored
10
cache/cache.py
vendored
@@ -37,6 +37,7 @@ from sqlalchemy import and_, join, select
|
||||
from auth.orm import Author, AuthorFollower
|
||||
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
||||
from orm.topic import Topic, TopicFollower
|
||||
from resolvers.stat import get_with_stat
|
||||
from services.db import local_session
|
||||
from services.redis import redis
|
||||
from utils.encoders import fast_json_dumps
|
||||
@@ -246,7 +247,7 @@ async def get_cached_topic_followers(topic_id: int):
|
||||
f[0]
|
||||
for f in session.query(Author.id)
|
||||
.join(TopicFollower, TopicFollower.follower == Author.id)
|
||||
.filter(TopicFollower.topic == topic_id)
|
||||
.where(TopicFollower.topic == topic_id)
|
||||
.all()
|
||||
]
|
||||
|
||||
@@ -276,7 +277,7 @@ async def get_cached_author_followers(author_id: int):
|
||||
f[0]
|
||||
for f in session.query(Author.id)
|
||||
.join(AuthorFollower, AuthorFollower.follower == Author.id)
|
||||
.filter(AuthorFollower.author == author_id, Author.id != author_id)
|
||||
.where(AuthorFollower.author == author_id, Author.id != author_id)
|
||||
.all()
|
||||
]
|
||||
await redis.execute("SET", f"author:followers:{author_id}", fast_json_dumps(followers_ids))
|
||||
@@ -529,9 +530,8 @@ async def cache_by_id(entity, entity_id: int, cache_method):
|
||||
entity_id: ID сущности
|
||||
cache_method: функция кэширования
|
||||
"""
|
||||
from resolvers.stat import get_with_stat
|
||||
|
||||
caching_query = select(entity).filter(entity.id == entity_id)
|
||||
caching_query = select(entity).where(entity.id == entity_id)
|
||||
result = get_with_stat(caching_query)
|
||||
if not result or not result[0]:
|
||||
logger.warning(f"{entity.__name__} with id {entity_id} not found")
|
||||
@@ -875,7 +875,7 @@ async def invalidate_topic_followers_cache(topic_id: int) -> None:
|
||||
|
||||
# Получаем список всех подписчиков топика из БД
|
||||
with local_session() as session:
|
||||
followers_query = session.query(TopicFollower.follower).filter(TopicFollower.topic == topic_id)
|
||||
followers_query = session.query(TopicFollower.follower).where(TopicFollower.topic == topic_id)
|
||||
follower_ids = [row[0] for row in followers_query.all()]
|
||||
|
||||
logger.debug(f"Найдено {len(follower_ids)} подписчиков топика {topic_id}")
|
||||
|
5
cache/precache.py
vendored
5
cache/precache.py
vendored
@@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import traceback
|
||||
|
||||
from sqlalchemy import and_, join, select
|
||||
|
||||
@@ -51,7 +52,7 @@ async def precache_topics_authors(topic_id: int, session) -> None:
|
||||
select(ShoutAuthor.author)
|
||||
.select_from(join(ShoutTopic, Shout, ShoutTopic.shout == Shout.id))
|
||||
.join(ShoutAuthor, ShoutAuthor.shout == Shout.id)
|
||||
.filter(
|
||||
.where(
|
||||
and_(
|
||||
ShoutTopic.topic == topic_id,
|
||||
Shout.published_at.is_not(None),
|
||||
@@ -189,7 +190,5 @@ async def precache_data() -> None:
|
||||
logger.error(f"fail caching {author}")
|
||||
logger.info(f"{len(authors)} authors and their followings precached")
|
||||
except Exception as exc:
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
logger.error(f"Error in precache_data: {exc}")
|
||||
|
18
cache/revalidator.py
vendored
18
cache/revalidator.py
vendored
@@ -1,14 +1,6 @@
|
||||
import asyncio
|
||||
import contextlib
|
||||
|
||||
from cache.cache import (
|
||||
cache_author,
|
||||
cache_topic,
|
||||
get_cached_author,
|
||||
get_cached_topic,
|
||||
invalidate_cache_by_prefix,
|
||||
)
|
||||
from resolvers.stat import get_with_stat
|
||||
from services.redis import redis
|
||||
from utils.logger import root_logger as logger
|
||||
|
||||
@@ -55,6 +47,16 @@ class CacheRevalidationManager:
|
||||
|
||||
async def process_revalidation(self) -> None:
|
||||
"""Обновление кэша для всех сущностей, требующих ревалидации."""
|
||||
# Поздние импорты для избежания циклических зависимостей
|
||||
from cache.cache import (
|
||||
cache_author,
|
||||
cache_topic,
|
||||
get_cached_author,
|
||||
get_cached_topic,
|
||||
invalidate_cache_by_prefix,
|
||||
)
|
||||
from resolvers.stat import get_with_stat
|
||||
|
||||
# Проверяем соединение с Redis
|
||||
if not self._redis._client:
|
||||
return # Выходим из метода, если не удалось подключиться
|
||||
|
2
cache/triggers.py
vendored
2
cache/triggers.py
vendored
@@ -88,7 +88,7 @@ def after_reaction_handler(mapper, connection, target) -> None:
|
||||
with local_session() as session:
|
||||
shout = (
|
||||
session.query(Shout)
|
||||
.filter(
|
||||
.where(
|
||||
Shout.id == shout_id,
|
||||
Shout.published_at.is_not(None),
|
||||
Shout.deleted_at.is_(None),
|
||||
|
Reference in New Issue
Block a user