File size: 20,097 Bytes
2ed8996 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | """
Query Result Caching System for AegisLM SaaS Backend.
Production-ready intelligent query caching with Redis backend,
cache invalidation, and performance optimization.
"""
import asyncio
import json
import hashlib
import pickle
from datetime import datetime, timedelta
from typing import Any, Optional, Dict, List, Union, Callable
from functools import wraps
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession
import logging
from .database import get_redis
from .config import settings
logger = logging.getLogger(__name__)
class CacheKey:
"""Cache key generator for queries."""
@staticmethod
def generate(query: str, params: Optional[Dict[str, Any]] = None) -> str:
"""Generate cache key for query and parameters."""
# Normalize query
normalized_query = ' '.join(query.lower().split())
# Create hash of query and params
content = normalized_query
if params:
content += json.dumps(params, sort_keys=True)
hash_key = hashlib.sha256(content.encode()).hexdigest()
return f"query_cache:{hash_key}"
@staticmethod
def generate_table_dependency_key(table_name: str) -> str:
"""Generate table dependency key for cache invalidation."""
return f"table_deps:{table_name}"
class CacheConfig:
"""Cache configuration."""
def __init__(self, ttl_seconds: int = 300, max_size: int = 1000,
enabled: bool = True, smart_invalidation: bool = True):
self.ttl_seconds = ttl_seconds
self.max_size = max_size
self.enabled = enabled
self.smart_invalidation = smart_invalidation
class QueryCache:
"""Intelligent query result caching system."""
def __init__(self):
self.redis_client = None
self.default_config = CacheConfig(
ttl_seconds=getattr(settings, 'QUERY_CACHE_TTL', 300), # 5 minutes
max_size=getattr(settings, 'QUERY_CACHE_MAX_SIZE', 1000),
enabled=getattr(settings, 'QUERY_CACHE_ENABLED', True),
smart_invalidation=getattr(settings, 'QUERY_CACHE_SMART_INVALIDATION', True)
)
# Table-specific configurations
self.table_configs = {
'users': CacheConfig(ttl_seconds=600), # 10 minutes
'evaluations': CacheConfig(ttl_seconds=180), # 3 minutes
'api_keys': CacheConfig(ttl_seconds=900), # 15 minutes
}
# Query patterns that should not be cached
self.excluded_patterns = [
'INSERT', 'UPDATE', 'DELETE', 'CREATE', 'DROP', 'ALTER',
'TRUNCATE', 'COMMIT', 'ROLLBACK'
]
async def get_redis(self):
"""Get Redis client."""
if not self.redis_client:
self.redis_client = await get_redis()
return self.redis_client
def should_cache_query(self, query: str) -> bool:
"""Check if query should be cached."""
query_upper = query.upper()
# Exclude non-SELECT queries
if not query_upper.strip().startswith('SELECT'):
return False
# Exclude specific patterns
for pattern in self.excluded_patterns:
if pattern in query_upper:
return False
# Exclude queries with NOW(), CURRENT_TIMESTAMP, etc.
time_functions = ['NOW()', 'CURRENT_TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME']
for func in time_functions:
if func in query_upper:
return False
return True
def get_cache_config(self, query: str) -> CacheConfig:
"""Get cache configuration for query."""
# Extract table names from query
tables = self._extract_tables(query)
# Use table-specific config if available
for table in tables:
if table in self.table_configs:
return self.table_configs[table]
return self.default_config
def _extract_tables(self, query: str) -> List[str]:
"""Extract table names from query."""
import re
tables = []
# Find FROM clauses
from_matches = re.findall(r'FROM\s+(\w+)', query, re.IGNORECASE)
tables.extend(from_matches)
# Find JOIN clauses
join_matches = re.findall(r'JOIN\s+(\w+)', query, re.IGNORECASE)
tables.extend(join_matches)
return list(set(tables))
async def get(self, query: str, params: Optional[Dict[str, Any]] = None) -> Optional[Any]:
"""Get cached query result."""
try:
if not self.should_cache_query(query):
return None
config = self.get_cache_config(query)
if not config.enabled:
return None
redis_client = await self.get_redis()
cache_key = CacheKey.generate(query, params)
# Get cached result
cached_data = await redis_client.get(cache_key)
if cached_data:
# Deserialize result
result = pickle.loads(cached_data)
logger.debug(f"Cache hit for query: {cache_key[:16]}...")
return result
logger.debug(f"Cache miss for query: {cache_key[:16]}...")
return None
except Exception as e:
logger.error(f"Failed to get from cache: {e}")
return None
async def set(self, query: str, result: Any, params: Optional[Dict[str, Any]] = None) -> bool:
"""Cache query result."""
try:
if not self.should_cache_query(query):
return False
config = self.get_cache_config(query)
if not config.enabled:
return False
redis_client = await self.get_redis()
cache_key = CacheKey.generate(query, params)
# Serialize result
serialized_result = pickle.dumps(result)
# Check cache size limit
await self._enforce_size_limit(redis_client)
# Store in cache
await redis_client.setex(cache_key, config.ttl_seconds, serialized_result)
# Store table dependencies for smart invalidation
if config.smart_invalidation:
await self._store_table_dependencies(query, cache_key)
logger.debug(f"Cached query result: {cache_key[:16]}...")
return True
except Exception as e:
logger.error(f"Failed to cache result: {e}")
return False
async def invalidate_table(self, table_name: str) -> int:
"""Invalidate all cache entries dependent on a table."""
try:
redis_client = await self.get_redis()
dep_key = CacheKey.generate_table_dependency_key(table_name)
# Get dependent cache keys
cache_keys = await redis_client.smembers(dep_key)
if cache_keys:
# Delete all dependent cache entries
await redis_client.delete(*cache_keys)
# Remove dependency tracking
await redis_client.delete(dep_key)
logger.info(f"Invalidated {len(cache_keys)} cache entries for table: {table_name}")
return len(cache_keys)
return 0
except Exception as e:
logger.error(f"Failed to invalidate table cache: {e}")
return 0
async def invalidate_query(self, query: str, params: Optional[Dict[str, Any]] = None) -> bool:
"""Invalidate specific query cache."""
try:
redis_client = await self.get_redis()
cache_key = CacheKey.generate(query, params)
result = await redis_client.delete(cache_key)
return result > 0
except Exception as e:
logger.error(f"Failed to invalidate query cache: {e}")
return False
async def clear_all(self) -> bool:
"""Clear all query cache entries."""
try:
redis_client = await self.get_redis()
# Get all cache keys
cursor = 0
cache_keys = []
while True:
cursor, keys = await redis_client.scan(cursor, match="query_cache:*", count=100)
cache_keys.extend(keys)
if cursor == 0:
break
# Delete all cache keys and dependencies
if cache_keys:
await redis_client.delete(*cache_keys)
# Clear table dependencies
cursor = 0
dep_keys = []
while True:
cursor, keys = await redis_client.scan(cursor, match="table_deps:*", count=100)
dep_keys.extend(keys)
if cursor == 0:
break
if dep_keys:
await redis_client.delete(*dep_keys)
logger.info(f"Cleared {len(cache_keys)} cache entries")
return True
except Exception as e:
logger.error(f"Failed to clear cache: {e}")
return False
async def _store_table_dependencies(self, query: str, cache_key: str):
"""Store table dependencies for smart invalidation."""
try:
redis_client = await self.get_redis()
tables = self._extract_tables(query)
for table in tables:
dep_key = CacheKey.generate_table_dependency_key(table)
await redis_client.sadd(dep_key, cache_key)
# Set expiry for dependency tracking
await redis_client.expire(dep_key, 3600) # 1 hour
except Exception as e:
logger.error(f"Failed to store table dependencies: {e}")
async def _enforce_size_limit(self, redis_client):
"""Enforce cache size limit by removing oldest entries."""
try:
# Get current cache size
cursor = 0
cache_keys = []
while True:
cursor, keys = await redis_client.scan(cursor, match="query_cache:*", count=100)
cache_keys.extend(keys)
if cursor == 0:
break
current_size = len(cache_keys)
max_size = self.default_config.max_size
if current_size >= max_size:
# Remove oldest entries (simple LRU simulation)
# Get TTL for each key and remove those with shortest TTL
key_ttl_pairs = []
for key in cache_keys[:100]: # Check first 100 keys
ttl = await redis_client.ttl(key)
key_ttl_pairs.append((key, ttl))
# Sort by TTL (shortest first) and remove
key_ttl_pairs.sort(key=lambda x: x[1])
keys_to_remove = [k[0] for k in key_ttl_pairs[:10]] # Remove 10 oldest
if keys_to_remove:
await redis_client.delete(*keys_to_remove)
logger.debug(f"Removed {len(keys_to_remove)} old cache entries")
except Exception as e:
logger.error(f"Failed to enforce size limit: {e}")
async def get_cache_stats(self) -> Dict[str, Any]:
"""Get cache statistics."""
try:
redis_client = await self.get_redis()
# Count cache entries
cursor = 0
cache_keys = []
while True:
cursor, keys = await redis_client.scan(cursor, match="query_cache:*", count=100)
cache_keys.extend(keys)
if cursor == 0:
break
# Count table dependencies
cursor = 0
dep_keys = []
while True:
cursor, keys = await redis_client.scan(cursor, match="table_deps:*", count=100)
dep_keys.extend(keys)
if cursor == 0:
break
# Get memory usage
total_memory = 0
sample_size = min(50, len(cache_keys)) # Sample first 50 keys
for key in cache_keys[:sample_size]:
try:
size = await redis_client.memory_usage(key)
total_memory += size
except:
pass
# Estimate total memory usage
if sample_size > 0:
avg_size = total_memory / sample_size
estimated_total = avg_size * len(cache_keys)
else:
estimated_total = 0
return {
"cache_entries": len(cache_keys),
"table_dependencies": len(dep_keys),
"estimated_memory_mb": round(estimated_total / (1024 * 1024), 2),
"max_size": self.default_config.max_size,
"utilization_percent": round((len(cache_keys) / self.default_config.max_size) * 100, 2),
"default_ttl_seconds": self.default_config.ttl_seconds,
"enabled": self.default_config.enabled
}
except Exception as e:
logger.error(f"Failed to get cache stats: {e}")
return {"error": str(e)}
# Global query cache instance
query_cache = QueryCache()
# Decorator for automatic query caching
def cached_query(ttl_seconds: Optional[int] = None, enabled: bool = True):
"""Decorator for automatic query result caching."""
def decorator(func: Callable):
@wraps(func)
async def wrapper(*args, **kwargs):
if not enabled:
return await func(*args, **kwargs)
# Extract query from function arguments
# This assumes the first argument is the query string
query = args[0] if args else kwargs.get('query')
params = kwargs.get('params')
if not query:
return await func(*args, **kwargs)
# Try to get from cache
cached_result = await query_cache.get(query, params)
if cached_result is not None:
return cached_result
# Execute function and cache result
result = await func(*args, **kwargs)
await query_cache.set(query, result, params)
return result
return wrapper
return decorator
# Context manager for query caching
class CachedQueryContext:
"""Context manager for query caching with automatic invalidation."""
def __init__(self, ttl_seconds: Optional[int] = None, tables: Optional[List[str]] = None):
self.ttl_seconds = ttl_seconds
self.tables = tables or []
self.config = CacheConfig(ttl_seconds=ttl_seconds) if ttl_seconds else query_cache.default_config
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
# Invalidate cache for specified tables if an exception occurred
if exc_type is not None and self.tables:
for table in self.tables:
await query_cache.invalidate_table(table)
async def get(self, query: str, params: Optional[Dict[str, Any]] = None) -> Optional[Any]:
"""Get cached result."""
return await query_cache.get(query, params)
async def set(self, query: str, result: Any, params: Optional[Dict[str, Any]] = None) -> bool:
"""Cache result."""
return await query_cache.set(query, result, params)
# Cache warming function
async def warm_cache(common_queries: List[Dict[str, Any]]):
"""Warm cache with common queries."""
try:
from .database import async_engine
warmed_count = 0
for query_info in common_queries:
query = query_info["query"]
params = query_info.get("params")
ttl = query_info.get("ttl")
# Execute and cache query
async with async_engine.begin() as conn:
result = await conn.execute(text(query), params or {})
data = result.fetchall()
if ttl:
config = CacheConfig(ttl_seconds=ttl)
# Temporarily set custom config
original_config = query_cache.default_config
query_cache.default_config = config
await query_cache.set(query, data, params)
query_cache.default_config = original_config
else:
await query_cache.set(query, data, params)
warmed_count += 1
logger.info(f"Warmed cache with {warmed_count} queries")
return warmed_count
except Exception as e:
logger.error(f"Failed to warm cache: {e}")
return 0
# Scheduled cache maintenance
async def cache_maintenance_task():
"""Run scheduled cache maintenance."""
try:
# Get cache stats
stats = await query_cache.get_cache_stats()
# Log statistics
logger.info(f"Cache stats: {stats['cache_entries']} entries, {stats['estimated_memory_mb']} MB")
# Clean up expired dependencies
redis_client = await query_cache.get_redis()
cursor = 0
expired_deps = []
while True:
cursor, keys = await redis_client.scan(cursor, match="table_deps:*", count=100)
for key in keys:
ttl = await redis_client.ttl(key)
if ttl == -1: # No expiry set
await redis_client.expire(key, 3600) # Set 1 hour expiry
if cursor == 0:
break
logger.info("Cache maintenance completed")
except Exception as e:
logger.error(f"Cache maintenance failed: {e}")
if __name__ == "__main__":
import sys
async def main():
command = sys.argv[1] if len(sys.argv) > 1 else "help"
if command == "stats":
stats = await query_cache.get_cache_stats()
print(json.dumps(stats, indent=2))
elif command == "clear":
success = await query_cache.clear_all()
if success:
print("✅ Cache cleared successfully")
else:
print("❌ Failed to clear cache")
elif command == "invalidate":
table_name = sys.argv[2] if len(sys.argv) > 2 else None
if not table_name:
print("Error: invalidate requires table name")
sys.exit(1)
count = await query_cache.invalidate_table(table_name)
print(f"Invalidated {count} cache entries for table: {table_name}")
elif command == "warm":
# Example common queries
common_queries = [
{"query": "SELECT COUNT(*) FROM users WHERE is_active = true"},
{"query": "SELECT COUNT(*) FROM evaluations WHERE status = 'completed'"},
{"query": "SELECT COUNT(*) FROM api_keys WHERE is_active = true"}
]
warmed = await warm_cache(common_queries)
print(f"Warmed {warmed} queries in cache")
else:
print("Usage: python query_cache.py <command> [args]")
print("Commands: stats, clear, invalidate <table>, warm")
asyncio.run(main())
|