Spaces:
Paused

yaz / src /services /cache.py
anaselamr's picture
fix(collaboration): production-ready collaboration fixes — all 19 engines
0743ddc
Raw History Blame Contribute Delete
2.26 kB
"""YAZ Care Platform - Cache Service Fallback"""
import time
from typing import Any, Dict, Optional
import structlog
logger = structlog.get_logger(__name__)
class SimpleCache:
"""Simple in-memory cache fallback"""
def __init__(self):
self._cache: dict[str, dict[str, Any]] = {}
self.hits = 0
self.misses = 0
def get(self, key: str) -> Any | None:
"""Get value from cache"""
try:
if key in self._cache:
entry = self._cache[key]
if entry.get("expires", 0) > time.time():
self.hits += 1
return entry["value"]
else:
# Expired, remove it
del self._cache[key]
self.misses += 1
return None
except Exception as e:
logger.warning(f"Cache get error: {e}")
return None
def set(self, key: str, value: Any, ttl: int = 300):
"""Set value in cache with TTL"""
try:
self._cache[key] = {
"value": value,
"expires": time.time() + ttl,
"created": time.time(),
}
except Exception as e:
logger.warning(f"Cache set error: {e}")
def delete(self, key: str):
"""Delete key from cache"""
self._cache.pop(key, None)
def get_stats(self) -> dict[str, Any]:
"""Get cache statistics"""
total = self.hits + self.misses
hit_ratio = (self.hits / total * 100) if total > 0 else 0
return {
"hits": self.hits,
"misses": self.misses,
"hit_ratio": hit_ratio,
"size": len(self._cache),
"status": "available",
}
# Global instance
cache = SimpleCache()
# Alias for backward compatibility (factory.py imports cache_manager)
cache_manager = cache
class HealthcareCacheNamespaces:
"""Cache namespace constants for healthcare modules"""
PATIENT = "patient"
CLINICAL = "clinical"
SURGICAL = "surgical"
LABORATORY = "laboratory"
PHARMACY = "pharmacy"
SCHEDULING = "scheduling"
BILLING = "billing"
INVENTORY = "inventory"
TENANT = "tenant"
CONFIG = "config"