loop-fix-4
This commit is contained in:
48
services/revalidator.py
Normal file
48
services/revalidator.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import asyncio
|
||||
from services.logger import root_logger as logger
|
||||
from services.cache import get_cached_author, cache_author, cache_topic, get_cached_topic
|
||||
|
||||
|
||||
class CacheRevalidationManager:
|
||||
"""Управление периодической ревалидацией кэша."""
|
||||
|
||||
def __init__(self):
|
||||
self.items_to_revalidate = {"authors": set(), "topics": set()}
|
||||
self.revalidation_interval = 60 # Интервал ревалидации в секундах
|
||||
self.loop = None
|
||||
|
||||
def start(self):
|
||||
self.loop = asyncio.get_event_loop()
|
||||
self.loop.run_until_complete(self.revalidate_cache())
|
||||
self.loop.run_forever()
|
||||
logger.info("[services.revalidator] started infinite loop")
|
||||
|
||||
async def revalidate_cache(self):
|
||||
"""Периодическая ревалидация кэша."""
|
||||
while True:
|
||||
await asyncio.sleep(self.revalidation_interval)
|
||||
await self.process_revalidation()
|
||||
|
||||
async def process_revalidation(self):
|
||||
"""Ревалидация кэша для отмеченных сущностей."""
|
||||
for entity_type, ids in self.items_to_revalidate.items():
|
||||
for entity_id in ids:
|
||||
if entity_type == "authors":
|
||||
# Ревалидация кэша автора
|
||||
author = await get_cached_author(entity_id)
|
||||
if author:
|
||||
await cache_author(author)
|
||||
elif entity_type == "topics":
|
||||
# Ревалидация кэша темы
|
||||
topic = await get_cached_topic(entity_id)
|
||||
if topic:
|
||||
await cache_topic(topic)
|
||||
ids.clear()
|
||||
|
||||
def mark_for_revalidation(self, entity_id, entity_type):
|
||||
"""Отметить сущность для ревалидации."""
|
||||
self.items_to_revalidate[entity_type].add(entity_id)
|
||||
|
||||
|
||||
# Инициализация менеджера ревалидации
|
||||
revalidation_manager = CacheRevalidationManager()
|
@@ -1,56 +1,12 @@
|
||||
import asyncio
|
||||
from sqlalchemy import event
|
||||
from orm.author import Author, AuthorFollower
|
||||
from orm.reaction import Reaction
|
||||
from orm.shout import Shout, ShoutAuthor
|
||||
from orm.topic import Topic, TopicFollower
|
||||
from services.cache import cache_author, get_cached_author, cache_topic, get_cached_topic
|
||||
from services.revalidator import revalidation_manager
|
||||
from services.logger import root_logger as logger
|
||||
|
||||
|
||||
class CacheRevalidationManager:
|
||||
"""Управление периодической ревалидацией кэша."""
|
||||
|
||||
def __init__(self):
|
||||
self.items_to_revalidate = {"authors": set(), "topics": set()}
|
||||
self.revalidation_interval = 60 # Интервал ревалидации в секундах
|
||||
|
||||
def start(self):
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(self.revalidate_cache())
|
||||
loop.run_forever()
|
||||
|
||||
async def revalidate_cache(self):
|
||||
"""Периодическая ревалидация кэша."""
|
||||
while True:
|
||||
await asyncio.sleep(self.revalidation_interval)
|
||||
await self.process_revalidation()
|
||||
|
||||
async def process_revalidation(self):
|
||||
"""Ревалидация кэша для отмеченных сущностей."""
|
||||
for entity_type, ids in self.items_to_revalidate.items():
|
||||
for entity_id in ids:
|
||||
if entity_type == "authors":
|
||||
# Ревалидация кэша автора
|
||||
author = await get_cached_author(entity_id)
|
||||
if author:
|
||||
await cache_author(author)
|
||||
elif entity_type == "topics":
|
||||
# Ревалидация кэша темы
|
||||
topic = await get_cached_topic(entity_id)
|
||||
if topic:
|
||||
await cache_topic(topic)
|
||||
ids.clear()
|
||||
|
||||
def mark_for_revalidation(self, entity_id, entity_type):
|
||||
"""Отметить сущность для ревалидации."""
|
||||
self.items_to_revalidate[entity_type].add(entity_id)
|
||||
|
||||
# Инициализация и запуск менеджера ревалидации
|
||||
revalidation_manager = CacheRevalidationManager()
|
||||
revalidation_manager.start()
|
||||
|
||||
|
||||
def after_update_handler(mapper, connection, target):
|
||||
"""Обработчик обновления сущности."""
|
||||
entity_type = "authors" if isinstance(target, Author) else "topics" if isinstance(target, Topic) else "shouts"
|
||||
|
Reference in New Issue
Block a user