WAGERBABE DOCS
All Stories
3-3-tiered-caching-implementationDoneEpic 3.3

Story 3.3: Tiered Caching Implementation

Status: done

Tasks

  • Task 1: Create TieredCacheService Class (AC: 1, 2, 3, 4, 5, 6, 7)
    • 1.1 Created `server/app/services/tiered_cache_service.py` extending redis_cache.py
    • 1.2 Implemented `get_with_dynamic_ttl()` with status-aware key lookup
    • 1.3 Implemented `set_with_dynamic_ttl()` with automatic TTL selection
    • 1.4 Implemented `invalidate_game_cache()` for cache invalidation
    • 1.5 Implemented `get_ttl_for_status(computed_status)` method
    • 1.6 Defined TTL constants: `TTL_LIVE=30`, `TTL_UPCOMING=300`, `TTL_SCHEDULED=1800`, `TTL_COMPLETED=86400`
  • Task 2: Cache Integration with Odds Endpoint (AC: 1, 2, 7, 11)
    • 2.1 Leveraged existing X-Cache header support in redis_cache.py
    • 2.2 Build cache key using `computed_status`: `odds:{status}:{game_id}`
    • 2.3 Cache hit returns data with `X-Cache: HIT` header
    • 2.4 Cache miss fetches from API, caches with dynamic TTL, returns `X-Cache: MISS`
    • 2.5 Used singleton pattern consistent with existing services
  • Task 3: Graceful Degradation (AC: 10) - Already handled in Story 3.1
    • 3.1 All Redis operations wrapped in try/except in redis_cache.py
    • 3.2 On RedisError, logs warning and returns None (fallback to API)
    • 3.3 Redis availability tracked via `get_cache_stats()["connected"]`
    • 3.4 Connection pool with automatic reconnection
  • Task 4: Cache Hit Rate Monitoring (AC: 8)
    • 4.1 Extended TieredCacheService with per-status metrics
    • 4.2 Track hits/misses/sets per status tier
    • 4.3 Hit ratio exposed via `/api/v1/shared/cache/stats` and health endpoints
    • 4.4 Per-status hit ratio calculation in `get_extended_stats()`
  • Task 5: Cache Warming (AC: 9)
    • 5.1 Created `server/app/tasks/cache_warmer.py` background task
    • 5.2 Query games within next 6 hours (upcoming/live)
    • 5.3 Pre-fetch and cache odds for top games
    • 5.4 Scheduled to run every 2 minutes via APScheduler
    • 5.5 Registered in GameMonitoringScheduler.start()
  • Task 6: Cache Statistics Endpoint (AC: 12)
    • 6.1 Created `/api/v1/shared/cache/stats` endpoint in cache.py
    • 6.2 Returns: hit_ratio, total_keys, memory_usage, errors, uptime
    • 6.3 Includes per-status distribution (live, upcoming, scheduled, completed)
    • 6.4 Added reset endpoint at POST `/api/v1/shared/cache/stats/reset`
  • Task 7: Testing & Validation (AC: 1-12)
    • 7.1 Unit tests for TieredCacheService: 27 tests in test_tiered_cache_service.py
    • 7.2 Tests for cache hit/miss behavior
    • 7.3 Tests for correct TTL selection per status
    • 7.4 Tests for graceful degradation (mocked Redis failure)
    • 7.5 Tests for metrics tracking
    • 7.6 Tests for cache key generation

Progress

Tasks7/7
Acceptance Criteria0
Total Tasks7