Skip to content

feat(cache): smart caching strategy for dashboard & analytics#438

Open
lalalic wants to merge 1 commit intorohitdash08:mainfrom
lalalic:feat/smart-caching
Open

feat(cache): smart caching strategy for dashboard & analytics#438
lalalic wants to merge 1 commit intorohitdash08:mainfrom
lalalic:feat/smart-caching

Conversation

@lalalic
Copy link

@lalalic lalalic commented Mar 15, 2026

Summary

Intelligent multi-layer caching system with smart invalidation for dashboard and analytics performance. Closes #127.

Architecture

Request → L1 Cache (Memory, ~1μs) → L2 Cache (Redis, ~1ms) → Database

L1 Cache (In-Memory)

  • Python dict-based with configurable max size (1000 entries)
  • LRU-like eviction with automatic expired entry cleanup
  • Hit/miss tracking with real-time statistics

L2 Cache (Redis)

  • Persistent across process restarts
  • Shared across application instances
  • Pattern-based key scanning for targeted invalidation

Features

Configurable TTL Policies (8 types)

Data Type TTL Use Case
dashboard 5 min Dashboard widgets
analytics 10 min Spending trends, charts
insights 15 min AI-generated insights
categories 1 hour Category lists
user_prefs 2 hours User preferences
summary 30 min Financial summaries
report 1 hour Generated reports
default 5 min Everything else

@cached Decorator

@cached(data_type="dashboard")
def get_dashboard(user_id):
    return expensive_computation()

Smart Invalidation

  • Per-user invalidation across specific data types
  • Global cache flush capability
  • Automatic L1/L2 coordination

Cache Warming

Pre-populate caches for users via configurable warmer functions.

API Endpoints (8 total)

Method Endpoint Description
GET /cache/stats L1 + L2 statistics
POST /cache/stats/reset Reset counters
GET /cache/policies TTL policy listing
POST /cache/invalidate/user Per-user invalidation
POST /cache/invalidate/all Full cache flush
POST /cache/warm Pre-populate cache
POST /cache/test Test set/get/delete
GET /cache/health Health check

Testing

46 tests covering:

  • L1 cache operations (9 tests): set/get/delete, eviction, stats, expiry
  • Cache key generation (5 tests): determinism, isolation
  • Cache operations (6 tests): multi-layer get/set, invalidation
  • @cached decorator (4 tests): caching, isolation, params
  • Cache warming (4 tests): success, empty, error handling
  • Statistics (2 tests): retrieval and reset
  • Route endpoints (16 tests): all 8 endpoints with edge cases
  • Auth requirements (2 tests): unauthorized access

Files Changed

File Change
app/services/smart_cache.py Multi-layer cache engine with L1/L2
app/routes/smart_cache.py 8 API endpoints
app/routes/__init__.py Register cache blueprint
tests/conftest.py Add fakeredis + smart_cache patching
tests/test_smart_cache.py 46 comprehensive tests
docs/smart-caching.md Full documentation

Closes rohitdash08#127

Add intelligent multi-layer caching system with:
- L1 in-memory cache with LRU eviction and TTL expiry
- L2 Redis cache with automatic promotion
- 8 configurable TTL policies per data type
- @cached decorator for easy function-level caching
- Smart invalidation: per-user, per-type, and global
- Cache warming/preloading support
- Cache health monitoring and statistics
- cache_get/set/delete operations across both layers
- 8 REST endpoints for management and monitoring
- 46 tests, all passing
- Documentation
@lalalic lalalic requested a review from rohitdash08 as a code owner March 15, 2026 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Smart caching strategy for dashboard & analytics

1 participant