import time from typing import Any, Dict, Optional from app.core.config import settings class CacheService: """Simple in-memory cache service.""" def __init__(self): self._cache: Dict[str, Dict[str, Any]] = {} def get(self, key: str) -> Optional[Any]: """ Get value from cache. Args: key: Cache key Returns: Cached value or None if not found or expired """ if key not in self._cache: return None cache_item = self._cache[key] # Check if cache item has expired if time.time() > cache_item["expires_at"]: # Remove expired item del self._cache[key] return None return cache_item["value"] def set(self, key: str, value: Any, expires_in: Optional[int] = None) -> None: """ Set value in cache. Args: key: Cache key value: Value to cache expires_in: Expiration time in seconds (default from settings) """ if expires_in is None: expires_in = settings.CACHE_EXPIRATION_SECONDS self._cache[key] = { "value": value, "expires_at": time.time() + expires_in } def delete(self, key: str) -> None: """ Delete value from cache. Args: key: Cache key """ if key in self._cache: del self._cache[key] def clear(self) -> None: """Clear all cached values.""" self._cache.clear() # Create a singleton instance of the cache cache = CacheService()