From 31cf6b696186c2c491fb2264e968ee7822aca02f Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 1 Oct 2025 23:59:09 +0300 Subject: [PATCH] invalidation-fix4 --- resolvers/follower.py | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/resolvers/follower.py b/resolvers/follower.py index 48ff6fd2..c0918866 100644 --- a/resolvers/follower.py +++ b/resolvers/follower.py @@ -40,9 +40,20 @@ async def follow( ) -> dict[str, Any]: logger.debug("Начало выполнения функции 'follow'") viewer_id = info.context.get("author", {}).get("id") + follower_dict = info.context.get("author") or {} + + # ✅ КРИТИЧНО: Инвалидируем кеш В САМОМ НАЧАЛЕ, если пользователь авторизован + # чтобы предотвратить чтение старых данных при последующей перезагрузке + if viewer_id: + entity_type = what.lower() + cache_key_pattern = f"author:follows-{entity_type}s:{viewer_id}" + await redis.execute("DEL", cache_key_pattern) + await redis.execute("DEL", f"author:id:{viewer_id}") + logger.debug(f"Инвалидирован кеш подписок follower'а: {cache_key_pattern}") + if not viewer_id: return {"error": "Access denied"} - follower_dict = info.context.get("author") or {} + logger.debug(f"follower: {follower_dict}") if not viewer_id or not follower_dict: @@ -52,14 +63,6 @@ async def follow( follower_id = follower_dict.get("id") logger.debug(f"follower_id: {follower_id}") - # ✅ КРИТИЧНО: Инвалидируем кеш В САМОМ НАЧАЛЕ, ДО любых операций - # чтобы предотвратить чтение старых данных при последующей перезагрузке - entity_type = what.lower() - cache_key_pattern = f"author:follows-{entity_type}s:{follower_id}" - await redis.execute("DEL", cache_key_pattern) - await redis.execute("DEL", f"author:id:{follower_id}") - logger.debug(f"Инвалидирован кеш подписок follower'а: {cache_key_pattern}") - entity_classes = { "AUTHOR": (Author, AuthorFollower, get_cached_follower_authors, cache_author), "TOPIC": (Topic, TopicFollower, get_cached_follower_topics, cache_topic), @@ -206,9 +209,19 @@ async def unfollow( ) -> dict[str, Any]: logger.debug("Начало выполнения функции 'unfollow'") viewer_id = info.context.get("author", {}).get("id") + follower_dict = info.context.get("author") or {} + + # ✅ КРИТИЧНО: Инвалидируем кеш В САМОМ НАЧАЛЕ, если пользователь авторизован + if viewer_id: + entity_type = what.lower() + cache_key_pattern = f"author:follows-{entity_type}s:{viewer_id}" + await redis.execute("DEL", cache_key_pattern) + await redis.execute("DEL", f"author:id:{viewer_id}") + logger.debug(f"Инвалидирован кеш подписок В НАЧАЛЕ операции unfollow: {cache_key_pattern}") + if not viewer_id: return {"error": "Access denied"} - follower_dict = info.context.get("author") or {} + logger.debug(f"follower: {follower_dict}") if not viewer_id or not follower_dict: @@ -218,13 +231,6 @@ async def unfollow( follower_id = follower_dict.get("id") logger.debug(f"follower_id: {follower_id}") - # ✅ КРИТИЧНО: Инвалидируем кеш В САМОМ НАЧАЛЕ, ДО любых операций - entity_type = what.lower() - cache_key_pattern = f"author:follows-{entity_type}s:{follower_id}" - await redis.execute("DEL", cache_key_pattern) - await redis.execute("DEL", f"author:id:{follower_id}") - logger.debug(f"Инвалидирован кеш подписок В НАЧАЛЕ операции unfollow: {cache_key_pattern}") - entity_classes = { "AUTHOR": (Author, AuthorFollower, get_cached_follower_authors, cache_author), "TOPIC": (Topic, TopicFollower, get_cached_follower_topics, cache_topic),