acupedia-api/app/cache/invalidation.py

37 lines
2.1 KiB
Python

from __future__ import annotations
from dataclasses import dataclass
from uuid import UUID
from .service import CacheService
@dataclass(slots=True)
class ContentCacheInvalidator:
cache: CacheService
async def invalidate_active_modules(self) -> int:
return await self.cache.delete(self.cache.keys.active_modules())
async def invalidate_content(self, *, content_id: UUID) -> int:
return await self.cache.delete_pattern(self.cache.keys.content_pattern(content_id))
async def invalidate_module(self, *, module_id: UUID) -> int:
return await self.cache.delete_pattern(self.cache.keys.module_pattern(module_id))
async def invalidate_template(self, *, template_code: str) -> int:
n = await self.cache.delete(self.cache.keys.template_definitions(template_code))
return n + await self.cache.delete_pattern(self.cache.keys.template_pattern(template_code))
async def invalidate_content_change(self, *, content_id: UUID, module_ids: set[UUID], template_code: str) -> int:
n = await self.invalidate_content(content_id=content_id)
for module_id in module_ids:
n += await self.cache.delete(
self.cache.keys.module_contents(module_id),
self.cache.keys.module_template_contents(module_id=module_id, template_code=template_code),
self.cache.keys.module_template_details(module_id=module_id, template_code=template_code),
)
return n
async def invalidate_definition_change(self, *, template_code: str, module_ids: set[UUID]) -> int:
n = await self.invalidate_template(template_code=template_code)
for module_id in module_ids:
n += await self.cache.delete(
self.cache.keys.module_template_contents(module_id=module_id, template_code=template_code),
self.cache.keys.module_template_details(module_id=module_id, template_code=template_code),
)
return n
async def invalidate_module_structure_change(self, *, module_id: UUID) -> int:
return await self.invalidate_active_modules() + await self.invalidate_module(module_id=module_id)