A Least Recently Used (LRU) Cache organizes items in order of use. It drops items not used for the longest amount of time in favor of caching new items. There are also expirable LRU caches that drop expired records and may prolong record lifetime on usage.
To implement those caches userver provides a base component class cache::LruCacheComponent that manages an instance of cache::ExpirableLruCache.
LRU cache is useful for reducing the network interactions while working with data that does not fit into service memory and rarely changes. Examples:
Expirable LRU cache is useful for reducing the network interactions while working with data does not fit into service memory and changes in predetermined intervals. Examples:
Using cache::LruCacheComponent is quite straightforward. Write a component that derives from it:
After that, get the component using the components::ComponentContext::FindComponent() and call cache::LruCacheComponent::GetCache(). Use the returned cache::LruCacheWrapper.
cache::LruCacheComponent should be your choice by default for implementing all the LRU caches. However, in some cases there is a need to avoid using the component system, or to avoid synchronization, or to control the expiration logic more precisely.
Here's a brief introduction into LRU types: