Add role-based filtering and imporve code

This commit is contained in:
Urtzi Alfaro
2025-10-15 16:12:49 +02:00
parent 96ad5c6692
commit 8f9e9a7edc
158 changed files with 11033 additions and 1544 deletions

View File

@@ -1,15 +1,13 @@
# services/external/app/cache/redis_cache.py
# services/external/app/cache/redis_wrapper.py
"""
Redis cache layer for fast training data access
Redis cache layer for fast training data access using shared Redis implementation
"""
from typing import List, Dict, Any, Optional
import json
from datetime import datetime, timedelta
import structlog
import redis.asyncio as redis
from app.core.config import settings
from shared.redis_utils import get_redis_client
logger = structlog.get_logger()
@@ -18,12 +16,11 @@ class ExternalDataCache:
"""Redis cache for external data service"""
def __init__(self):
self.redis_client = redis.from_url(
settings.REDIS_URL,
encoding="utf-8",
decode_responses=True
)
self.ttl = 86400 * 7
self.ttl = 86400 * 7 # 7 days
async def _get_client(self):
"""Get the shared Redis client"""
return await get_redis_client()
def _weather_cache_key(
self,
@@ -43,7 +40,8 @@ class ExternalDataCache:
"""Get cached weather data"""
try:
key = self._weather_cache_key(city_id, start_date, end_date)
cached = await self.redis_client.get(key)
client = await self._get_client()
cached = await client.get(key)
if cached:
logger.debug("Weather cache hit", city_id=city_id, key=key)
@@ -84,7 +82,8 @@ class ExternalDataCache:
serializable_data.append(record_dict)
await self.redis_client.setex(
client = await self._get_client()
await client.setex(
key,
self.ttl,
json.dumps(serializable_data)
@@ -113,7 +112,8 @@ class ExternalDataCache:
"""Get cached traffic data"""
try:
key = self._traffic_cache_key(city_id, start_date, end_date)
cached = await self.redis_client.get(key)
client = await self._get_client()
cached = await client.get(key)
if cached:
logger.debug("Traffic cache hit", city_id=city_id, key=key)
@@ -154,7 +154,8 @@ class ExternalDataCache:
serializable_data.append(record_dict)
await self.redis_client.setex(
client = await self._get_client()
await client.setex(
key,
self.ttl,
json.dumps(serializable_data)
@@ -168,11 +169,18 @@ class ExternalDataCache:
async def invalidate_city_cache(self, city_id: str):
"""Invalidate all cache entries for a city"""
try:
client = await self._get_client()
pattern = f"*:{city_id}:*"
async for key in self.redis_client.scan_iter(match=pattern):
await self.redis_client.delete(key)
logger.info("City cache invalidated", city_id=city_id)
# Use scan_iter for safer key pattern matching
keys_to_delete = []
async for key in client.scan_iter(match=pattern):
keys_to_delete.append(key)
if keys_to_delete:
await client.delete(*keys_to_delete)
logger.info("City cache invalidated", city_id=city_id, keys_deleted=len(keys_to_delete))
except Exception as e:
logger.error("Error invalidating cache", error=str(e))