feat(v2/featureflag): add optional LRU evaluation cache to Client

Summary

Implements evaluation caching for LabKit's feature flag client with these key features:

  • LRU cache with TTL expiry using the expirable.LRU from golang-lru/v2 (reduces maintenance and testing surface area vs custom implementation)
  • FeatureProvider decorator pattern for clean composition and extensibility
  • Separate caches for bool and string evaluations with type-safe storage
  • HTTP client timeout control - configurable timeout (default 2 seconds) exposed in Config for timeout control
  • Comprehensive test coverage including cache hits/misses, TTL expiry, LRU eviction, and string evaluations

Design Details

The cachingProvider wraps an inner FeatureProvider and intercepts BooleanEvaluation and StringEvaluation calls. Cache keys are deterministically built from the flag name and flattened evaluation context (JSON-serialized, sorted alphabetically). The expirable LRU package handles TTL management automatically, eliminating the need for manual expiry checking.

Testing

  • Cache hit/miss behavior verified
  • TTL expiry tested with configurable durations
  • LRU eviction verified when capacity exceeded
  • Cached reason properly set in resolution details
  • String evaluation support tested alongside bool
  • HTTP client timeout enforcement tested

What was addressed from review feedback

  1. @GitLabDuo (initial AI review):

    • Cache key collision prevention: validated via buildCacheKey tests
    • Test determinism: removed flaky time.Sleep calls, simplified expiry logic
  2. @lhollinda (manual review):

    • Size validation: added return error for size <= 0
    • Sleep call cleanup: removed unnecessary if statements
    • Expirable package usage: refactored to use upstream expirable.LRU instead of custom TTL logic

The refactoring to use expirable.LRU addresses the core maintenance concern: we now rely on the well-tested upstream implementation for TTL handling, reducing our code surface and testing burden.

Edited by Elliot Forbes

Merge request reports

Loading
Loading