Feat: Introduce LRU cache.
Merged
KA
3mo ago+597+597+012345678901234567890123456789-1-1-0123456789
This PR introduces an LRU cache backed by hashicorp/golang-lru.
Along with the cache implementation, it provides configurable cache options that allow the LRU cache to support byte-based memory accounting in addition to the standard entry-count limit.
The cache provides the following operations:
- Put
- Get
- Delete
- Purge
- CurrentBytes
These operations have been implemented to account for the configured byte limit and maintain the current memory usage of the cache accordingly.
The cache also supports configurable cache expiration and periodic sweeping of expired entries.
Summary by cubic
Introduces a generic, thread-safe LRU cache with per-item TTL, byte budgets, and post-unlock eviction callbacks for predictable, race-free in-memory caching. No existing code paths change.
- Adds
LRUStore[K,V]with Get/Put/Delete, plus Purge and CurrentBytes; Put accepts per-item TTL. - Options: MaxSize (0=unlimited), MaxBytes (0=unlimited), SizeOf (required if MaxBytes>0), OnEvict, SweepInterval (default 5m).
- Enforces byte budgets: rejects single items > MaxBytes, evicts LRU entries to fit, and tracks CurrentBytes whenever SizeOf is set, even if MaxBytes=0.
- Periodically sweeps expired items and stops on context cancel; eviction reasons include capacity, TTL, replace, delete, and purge; callbacks run after unlocking.
- Exposes
LRUCache,Options, andEvictionReasonincache/cache.go; addsgithub.com/hashicorp/golang-lru/v2.