Improve the frontend 5
This commit is contained in:
112
services/external/app/cache/redis_wrapper.py
vendored
112
services/external/app/cache/redis_wrapper.py
vendored
@@ -184,3 +184,115 @@ class ExternalDataCache:
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error invalidating cache", error=str(e))
|
||||
|
||||
# ===== Calendar Caching Methods =====
|
||||
|
||||
def _calendar_cache_key(self, calendar_id: str) -> str:
|
||||
"""Generate cache key for school calendar"""
|
||||
return f"calendar:{calendar_id}"
|
||||
|
||||
def _tenant_context_cache_key(self, tenant_id: str) -> str:
|
||||
"""Generate cache key for tenant location context"""
|
||||
return f"tenant_context:{tenant_id}"
|
||||
|
||||
async def get_cached_calendar(
|
||||
self,
|
||||
calendar_id: str
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
"""Get cached school calendar by ID"""
|
||||
try:
|
||||
key = self._calendar_cache_key(calendar_id)
|
||||
client = await self._get_client()
|
||||
cached = await client.get(key)
|
||||
|
||||
if cached:
|
||||
logger.debug("Calendar cache hit", calendar_id=calendar_id)
|
||||
return json.loads(cached)
|
||||
|
||||
logger.debug("Calendar cache miss", calendar_id=calendar_id)
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error reading calendar cache", error=str(e))
|
||||
return None
|
||||
|
||||
async def set_cached_calendar(
|
||||
self,
|
||||
calendar_id: str,
|
||||
calendar_data: Dict[str, Any]
|
||||
):
|
||||
"""Cache school calendar data (7 days TTL)"""
|
||||
try:
|
||||
key = self._calendar_cache_key(calendar_id)
|
||||
client = await self._get_client()
|
||||
|
||||
# Calendars change rarely, use 7-day TTL
|
||||
ttl = 86400 * 7
|
||||
|
||||
await client.setex(
|
||||
key,
|
||||
ttl,
|
||||
json.dumps(calendar_data)
|
||||
)
|
||||
|
||||
logger.debug("Calendar cached", calendar_id=calendar_id)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error caching calendar", error=str(e))
|
||||
|
||||
async def get_cached_tenant_context(
|
||||
self,
|
||||
tenant_id: str
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
"""Get cached tenant location context"""
|
||||
try:
|
||||
key = self._tenant_context_cache_key(tenant_id)
|
||||
client = await self._get_client()
|
||||
cached = await client.get(key)
|
||||
|
||||
if cached:
|
||||
logger.debug("Tenant context cache hit", tenant_id=tenant_id)
|
||||
return json.loads(cached)
|
||||
|
||||
logger.debug("Tenant context cache miss", tenant_id=tenant_id)
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error reading tenant context cache", error=str(e))
|
||||
return None
|
||||
|
||||
async def set_cached_tenant_context(
|
||||
self,
|
||||
tenant_id: str,
|
||||
context_data: Dict[str, Any]
|
||||
):
|
||||
"""Cache tenant location context (24 hours TTL)"""
|
||||
try:
|
||||
key = self._tenant_context_cache_key(tenant_id)
|
||||
client = await self._get_client()
|
||||
|
||||
# Tenant context changes less frequently, 24-hour TTL
|
||||
ttl = 86400
|
||||
|
||||
await client.setex(
|
||||
key,
|
||||
ttl,
|
||||
json.dumps(context_data)
|
||||
)
|
||||
|
||||
logger.debug("Tenant context cached", tenant_id=tenant_id)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error caching tenant context", error=str(e))
|
||||
|
||||
async def invalidate_tenant_context(self, tenant_id: str):
|
||||
"""Invalidate tenant context cache (called when context is updated)"""
|
||||
try:
|
||||
key = self._tenant_context_cache_key(tenant_id)
|
||||
client = await self._get_client()
|
||||
await client.delete(key)
|
||||
|
||||
logger.info("Tenant context cache invalidated", tenant_id=tenant_id)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error invalidating tenant context cache", error=str(e))
|
||||
|
||||
Reference in New Issue
Block a user