debug-update-shout-2
This commit is contained in:
parent
576e1ea152
commit
4fffd1025f
25
cache/cache.py
vendored
25
cache/cache.py
vendored
|
@ -329,3 +329,28 @@ async def get_cached_topic_authors(topic_id: int):
|
||||||
return authors
|
return authors
|
||||||
|
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
async def invalidate_shouts_cache(cache_keys: List[str]):
|
||||||
|
"""
|
||||||
|
Инвалидирует кэш выборок публикаций по переданным ключам.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
cache_keys: Список ключей кэша для инвалидации
|
||||||
|
|
||||||
|
Example:
|
||||||
|
await invalidate_shouts_cache([
|
||||||
|
"feed", # общая лента
|
||||||
|
"author_123", # публикации автора
|
||||||
|
"topic_456" # публикации по теме
|
||||||
|
])
|
||||||
|
"""
|
||||||
|
from services.redis import redis_client
|
||||||
|
|
||||||
|
for key in cache_keys:
|
||||||
|
cache_key = f"shouts:{key}"
|
||||||
|
try:
|
||||||
|
await redis_client.delete(cache_key)
|
||||||
|
logger.debug(f"Invalidated cache key: {cache_key}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error invalidating cache key {cache_key}: {e}")
|
||||||
|
|
|
@ -4,7 +4,7 @@ from sqlalchemy import and_, desc, select
|
||||||
from sqlalchemy.orm import joinedload
|
from sqlalchemy.orm import joinedload
|
||||||
from sqlalchemy.sql.functions import coalesce
|
from sqlalchemy.sql.functions import coalesce
|
||||||
|
|
||||||
from cache.cache import cache_author, cache_topic
|
from cache.cache import cache_author, cache_topic, invalidate_shouts_cache
|
||||||
from orm.author import Author
|
from orm.author import Author
|
||||||
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
||||||
from orm.topic import Topic
|
from orm.topic import Topic
|
||||||
|
@ -330,6 +330,27 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
|
||||||
|
|
||||||
shout_dict = shout_by_id.dict()
|
shout_dict = shout_by_id.dict()
|
||||||
|
|
||||||
|
# Инвалидация кэша после обновления
|
||||||
|
try:
|
||||||
|
logger.info("Invalidating cache after shout update")
|
||||||
|
|
||||||
|
# Инвалидируем кэш для всех связанных выборок
|
||||||
|
await invalidate_shouts_cache([
|
||||||
|
"feed", # лента
|
||||||
|
f"author_{author_id}", # публикации автора
|
||||||
|
"random_top", # случайные топовые
|
||||||
|
"unrated", # неоцененные
|
||||||
|
])
|
||||||
|
|
||||||
|
# Инвалидируем кэш для каждой связанной темы
|
||||||
|
for topic in shout_by_id.topics:
|
||||||
|
await invalidate_shouts_cache([f"topic_{topic.id}"])
|
||||||
|
|
||||||
|
logger.info("Cache invalidated successfully")
|
||||||
|
except Exception as cache_error:
|
||||||
|
logger.warning(f"Cache invalidation error: {cache_error}", exc_info=True)
|
||||||
|
# Не возвращаем ошибку, так как это некритично
|
||||||
|
|
||||||
if not publish:
|
if not publish:
|
||||||
await notify_shout(shout_dict, "update")
|
await notify_shout(shout_dict, "update")
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user