Skip to content

feat: Implement centralized cache invalidation service#847

Open
softnationz wants to merge 1 commit into
Netwalls:mainfrom
softnationz:feature/cache-invalidation-service
Open

feat: Implement centralized cache invalidation service#847
softnationz wants to merge 1 commit into
Netwalls:mainfrom
softnationz:feature/cache-invalidation-service

Conversation

@softnationz
Copy link
Copy Markdown

closes #457

This PR implements a centralized cache invalidation service to prevent stale reads in the BOXMEOUT platform. The service provides a clean API for cache operations with automatic invalidation when underlying data changes.

Changes

Core Service Enhancements (cache.service.ts)

  • ✅ Added get<T>(key) - Type-safe cache retrieval
  • ✅ Added set(key, value, ttl) - Cache storage with TTL
  • ✅ Added del(key) - Single key deletion
  • ✅ Added delPattern(pattern) - Pattern-based bulk invalidation using Redis SCAN
  • ✅ Added getOrSet<T>(key, ttl, compute) - Read-through caching pattern
  • ✅ Maintained backward compatibility with legacy API (cacheGet, cacheSet, etc.)

Cache Key Namespaces

Implemented consistent namespacing for organized cache management:

  • market:{id} - Individual market data
  • market:{id}:stats - Market statistics
  • markets:* - Market list queries
  • leaderboard:global:* - Global leaderboard data
  • user:{id}:balance - User balance data
  • platform:stats - Platform-wide statistics

MarketService Integration

  • Updated all cache operations to use new API
  • Added proper cache invalidation after write operations
  • Implemented invalidateMarketCache() for comprehensive market cache clearing
  • Applied invalidation in bulkPauseMarkets() and bulkCancelMarkets()

Testing

  • ✅ 23 comprehensive unit tests covering:
    • Basic CRUD operations (get, set, del)
    • Pattern-based invalidation with Redis SCAN
    • getOrSet read-through caching
    • Error handling and graceful degradation
    • Namespaced key operations
    • Cache invalidation workflows
    • Legacy API compatibility

Documentation

  • Created comprehensive CACHE_SERVICE_README.md with:
    • API reference and usage examples
    • Cache key namespace conventions
    • Invalidation strategies and best practices
    • Integration examples for services
    • Performance considerations
    • Migration guide from legacy API

Acceptance Criteria

  • cache.service.ts exposes: get, set, del, delPattern, getOrSet
  • delPattern('market:*') purges all market cache entries using Redis SCAN
  • Cache keys namespaced: market:{id}, leaderboard:global:*, user:{id}:balance
  • After any write in market.service.ts, relevant cache keys are invalidated
  • Unit test: set → get → invalidate → get returns null ✅ (23 tests passing)

- Enhanced cache.service.ts with new API methods: get, set, del, delPattern, getOrSet
- Added comprehensive namespaced key support (market:{id}, leaderboard:global:*, user:{id}:balance)
- Implemented pattern-based cache invalidation using Redis SCAN
- Updated MarketService to use new cache API and invalidate caches after writes
- Added 23 unit tests covering all cache operations and edge cases
- Included detailed documentation in CACHE_SERVICE_README.md
- Maintained backward compatibility with legacy API (cacheGet, cacheSet, etc.)

Resolves Netwalls#60
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.

Issue #60 — Cache invalidation service

2 participants