Cache Manager Agent
The Cache Manager Agent provides distributed caching with Redis backend for improved performance across the UIP platform.
π Overviewβ
| Property | Value |
|---|---|
| Module | src.agents.cache.cache_manager_agent |
| Class | CacheManagerAgent |
| Author | Nguyen Viet Hoang |
| Version | 1.0.0 |
π― Purposeβ
The Cache Manager Agent manages distributed caching to:
- Reduce database load by caching frequently accessed data
- Improve response times for API requests
- Enable horizontal scaling with shared cache state
- Implement cache invalidation strategies
π§ Cache Strategiesβ
Write-Throughβ
Updates cache immediately when data is written to the database.
# Data written to DB and cache simultaneously
cache_manager.set("camera:CAM_001", camera_data, ttl=3600)
Write-Behind (Async)β
Asynchronous cache updates for better write performance.
# Queue cache update for async processing
cache_manager.set_async("observations:latest", observations)
Cache-Aside (Lazy Loading)β
Load data into cache only on cache miss.
# Check cache first, load from DB on miss
data = cache_manager.get_or_load("pattern:P001", loader_func)
π Architectureβ
βββββββββββββββββββ βββββββββββββββββββ
β API Request βββββΆβ Cache Manager β
βββββββββββββββββββ ββββββββββ¬βββββββββ
β
βββββββββββββΌββββββββββββ
βΌ βΌ βΌ
ββββββββββββ ββββββββββββ ββββββββββββ
β Redis β β Local β β Database β
β Cache β β Cache β β Fallback β
ββββββββββββ ββββββββββββ ββββββββββββ
π Usageβ
Basic Usageβ
from src.agents.cache.cache_manager_agent import CacheManagerAgent
# Initialize with configuration
config = {
"enabled": True,
"redis_host": "localhost",
"redis_port": 6379,
"default_ttl": 3600
}
cache_manager = CacheManagerAgent(config)
# Set value
cache_manager.set("key", {"data": "value"}, ttl=300)
# Get value
value = cache_manager.get("key")
# Delete value
cache_manager.delete("key")
Cache Key Patternsβ
# Entity caching
cache_manager.set("camera:CAM_001", camera_entity)
cache_manager.set("observation:OBS_001", observation)
# Query result caching
cache_manager.set("query:accidents:severe", results, ttl=60)
# Aggregation caching
cache_manager.set("stats:hourly:2025-11-29T10", hourly_stats)
βοΈ Configurationβ
Environment Variablesβ
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=your_password
REDIS_DB=1
YAML Configurationβ
# config/cache_config.yaml
cache:
enabled: true
redis_host: localhost
redis_port: 6379
redis_db: 1
default_ttl: 3600
# TTL by category
ttl_settings:
cameras: 300
observations: 60
patterns: 600
statistics: 3600
π Performance Metricsβ
The Cache Manager tracks:
- Hit Rate: Percentage of successful cache hits
- Miss Rate: Percentage of cache misses
- Latency: Average cache operation latency
- Memory Usage: Redis memory consumption
π Cache Invalidationβ
Manual Invalidationβ
# Invalidate single key
cache_manager.delete("camera:CAM_001")
# Invalidate by pattern
cache_manager.delete_pattern("camera:*")
# Clear all cache
cache_manager.flush()
Automatic Invalidationβ
TTL-based expiration ensures stale data is automatically removed.
π‘οΈ Fallback Behaviorβ
When Redis is unavailable, the agent falls back to:
- In-memory cache for local operations
- Direct database queries for critical data
- Graceful degradation without errors
π Related Documentationβ
- Cache Invalidator Agent - Cache invalidation strategies
- Performance Monitor - Cache performance tracking
- Redis Configuration - Redis setup
See the complete agents reference for all available agents.