userver: MultiIndex LRU Container
Loading...
Searching...
No Matches
MultiIndex LRU Container

Generic LRU (Least Recently Used) cache container implementation that combines Boost.MultiIndex for flexible indexing with efficient LRU tracking. The container maintains elements in access order while supporting multiple indexing strategies through Boost.MultiIndex. The LRU eviction policy automatically removes the least recently accessed items when capacity is reached.

Two container variants are provided:

  • Container - Basic LRU cache with capacity management
  • ExpirableContainer - Extended version with time-based expiration (TTL)

MultiIndex LRU Implementation Notes

MultiIndex LRU Node Reuse Strategy

The container maintains a free list of allocated nodes to reduce memory allocations. When items are evicted or erased, their nodes are moved to the free list and reused for future insertions.

MultiIndex LRU Thread Safety

This container is not thread-safe. External synchronization is required for concurrent access.

MultiIndex LRU Iterator Invalidation

  • Insertions may invalidate iterators if capacity is exceeded and eviction occurs
  • Erasures invalidate iterators to the erased element only
  • Find operations do not invalidate iterators
  • set_capacity() may invalidate iterators when reducing capacity

Usage

using MyLruCache = multi_index_lru::Container<
MyValueT,
boost::multi_index::indexed_by<boost::multi_index::hashed_unique<
boost::multi_index::tag<MyTag>,
boost::multi_index::member<MyValueT, std::string, &MyValueT::key>>>>;
MyLruCache cache(1000); // Capacity of 1000 items
cache.insert(my_value);
auto it = cache.find<MyTag>("some_key");
EXPECT_NE(it, cache.end<MyTag>());