template<typename Key, typename Value, typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
class cache::LruCacheComponent< Key, Value, Hash, Equal >
Base class for LRU-cache components.
Provides facilities for creating LRU caches. You need to override LruCacheComponent::DoGetByKey to handle cache misses.
Caching components must be configured in service config (see options below) and may be reconfigured dynamically via components::DynamicConfig.
Dynamic config
Static options:
Name | Description | Default value |
size | max amount of items to store in cache | – |
ways | number of ways for associative cache | – |
lifetime | TTL for cache entries (0 is unlimited) | 0 |
config-settings | enables dynamic reconfiguration with CacheConfigSet | true |
Example usage:
using Key = std::string;
using Value = std::size_t;
class ExampleCacheComponent final
public:
static constexpr std::string_view kName = "example-cache";
: ::
cache::LruCacheComponent<Key, Value>(config, context) {}
private:
Value DoGetByKey(const Key& key) override {
return GetValueForExpiredKeyFromRemote(key);
}
Value GetValueForExpiredKeyFromRemote(const Key& key);
};
Do not forget to add the component to component list:
component_list.Append<ExampleCacheComponent>();
Example config:
# yaml
example-cache:
size: 1
ways: 1
lifetime: 1s # 0 (unlimited) by default
config-settings: false # true by default
Definition at line 77 of file lru_cache_component_base.hpp.