feat: add optional Redis caching layer for API endpoints

Add Redis-backed response caching for read-heavy API endpoints (nodes,
advertisements, messages, channels, dashboard, profiles) with configurable
TTL, key prefix isolation, and graceful fallback when Redis is unavailable.

New files:
- common/redis.py: CacheBackend, NullCache, RedisCacheBackend
- api/cache.py: @cached decorator, sorted_query_string helper
- tests/test_api/test_cache.py: 23 unit tests

Changes:
- pyproject.toml: add redis[hiredis] dependency
- common/config.py: 8 Redis settings on APISettings
- api/cli.py: Redis Click options + startup banner
- api/app.py: Redis lifespan init/cleanup, X-Cache middleware, health check
- 6 route files: apply @cached decorator to list endpoints
- docker-compose.yml: Redis service (cache profile), env vars
- docker-compose.dev.yml: Redis port exposure
- .env.example, README.md, AGENTS.md, docs/upgrading.md: documentation

Redis is disabled by default (REDIS_ENABLED=false). Enable with
--profile cache and REDIS_ENABLED=true.
This commit is contained in:
Louis King
2026-06-09 23:08:49 +01:00
parent a4419a8987
commit 385d1ab141
21 changed files with 1574 additions and 84 deletions
+31
View File
@@ -126,6 +126,27 @@ services:
volumes:
- observer_data:/app/data
# ==========================================================================
# Redis Cache - Optional shared cache for API response caching
# Use --profile cache to start with the bundled Redis, or point
# REDIS_HOST at an external Redis instance for multi-instance setups.
# ==========================================================================
redis:
image: redis:7-alpine
container_name: ${COMPOSE_PROJECT_NAME:-hub}-redis
profiles:
- all
- cache
restart: unless-stopped
command: redis-server --appendonly yes --maxmemory 128mb --maxmemory-policy allkeys-lru
volumes:
- redis_data:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 3
# ==========================================================================
# Collector - MQTT subscriber and database storage
# ==========================================================================
@@ -221,6 +242,14 @@ services:
- API_ADMIN_KEY=${API_ADMIN_KEY:-}
- METRICS_ENABLED=${METRICS_ENABLED:-true}
- METRICS_CACHE_TTL=${METRICS_CACHE_TTL:-60}
# Redis cache (optional — API works without Redis)
- REDIS_ENABLED=${REDIS_ENABLED:-false}
- REDIS_HOST=redis
- REDIS_PORT=6379
- REDIS_PASSWORD=${REDIS_PASSWORD:-}
- REDIS_KEY_PREFIX=${REDIS_KEY_PREFIX:-hub}
- REDIS_CACHE_TTL=${REDIS_CACHE_TTL:-30}
- REDIS_CACHE_TTL_DASHBOARD=${REDIS_CACHE_TTL_DASHBOARD:-30}
command: ["api"]
healthcheck:
test:
@@ -378,3 +407,5 @@ volumes:
name: ${COMPOSE_PROJECT_NAME:-hub}_mqtt_data
observer_data:
name: ${COMPOSE_PROJECT_NAME:-hub}_observer_data
redis_data:
name: ${COMPOSE_PROJECT_NAME:-hub}_redis_data