invcache-fix4

This commit is contained in:
Untone 2025-01-16 06:01:47 +03:00
parent c1c095a73c
commit 83ec475cc8
2 changed files with 4 additions and 19 deletions

21
cache/cache.py vendored
View File

@ -7,7 +7,6 @@ from sqlalchemy import and_, join, select
from orm.author import Author, AuthorFollower
from orm.shout import Shout, ShoutAuthor, ShoutTopic
from orm.topic import Topic, TopicFollower
from resolvers.editor import cache_by_id
from services.db import local_session
from services.redis import redis
from utils.encoders import CustomJSONEncoder
@ -348,25 +347,11 @@ async def invalidate_shouts_cache(cache_keys: List[str]):
Инвалидирует кэш выборок публикаций по переданным ключам.
"""
for key in cache_keys:
cache_key = f"shouts:{key}"
try:
await redis_operation('DEL', cache_key)
logger.debug(f"Invalidated cache key: {cache_key}")
await redis_operation('SETEX', f"{cache_key}:invalidated", value="1", ttl=CACHE_TTL)
if key.startswith("topic_"):
topic_id = key.split("_")[1]
related_keys = [
CACHE_KEYS['TOPIC_ID'].format(topic_id),
CACHE_KEYS['TOPIC_AUTHORS'].format(topic_id),
CACHE_KEYS['TOPIC_FOLLOWERS'].format(topic_id)
]
for related_key in related_keys:
await redis_operation('DEL', related_key)
await redis_operation('DEL', key)
await redis_operation('SETEX', f"{key}:invalidated", value="1", ttl=CACHE_TTL)
except Exception as e:
logger.error(f"Error invalidating cache key {cache_key}: {e}")
logger.error(f"Error invalidating cache key {key}: {e}")
async def cache_topic_shouts(topic_id: int, shouts: List[dict]):

View File

@ -1,6 +1,6 @@
import asyncio
from cache.cache import cache_author, cache_topic, get_cached_author, get_cached_topic
from cache.cache import cache_author, cache_topic, get_cached_author, get_cached_topic, get_cached_entity
from resolvers.stat import get_with_stat
from utils.logger import root_logger as logger