userver: cache::LruCacheComponent< Key, Value, Hash, Equal > Class Template Reference
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
cache::LruCacheComponent< Key, Value, Hash, Equal > Class Template Referenceabstract

#include <userver/cache/lru_cache_component_base.hpp>

Detailed Description

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 cache::LruCacheComponent<Key, Value> {
public:
static constexpr std::string_view kName = "example-cache";
ExampleCacheComponent(const components::ComponentConfig& config,
: ::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:

auto component_list = components::MinimalComponentList();
component_list.Append<ExampleCacheComponent>();

Example config:

    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.

+ Inheritance diagram for cache::LruCacheComponent< Key, Value, Hash, Equal >:
+ Collaboration diagram for cache::LruCacheComponent< Key, Value, Hash, Equal >:

Public Types

using Cache = ExpirableLruCache<Key, Value, Hash, Equal>
 
using CacheWrapper = LruCacheWrapper<Key, Value, Hash, Equal>
 

Public Member Functions

 LruCacheComponent (const components::ComponentConfig &, const components::ComponentContext &)
 
CacheWrapper GetCache ()
 
- Public Member Functions inherited from components::LoggableComponentBase
 LoggableComponentBase (const ComponentConfig &, const ComponentContext &)
 
 LoggableComponentBase (LoggableComponentBase &&)=delete
 
 LoggableComponentBase (const LoggableComponentBase &)=delete
 
 ~LoggableComponentBase () override=default
 
ComponentHealth GetComponentHealth () const override
 
void OnLoadingCancelled () override
 
void OnAllComponentsLoaded () override
 
void OnAllComponentsAreStopping () override
 

Static Public Member Functions

static yaml_config::Schema GetStaticConfigSchema ()
 
- Static Public Member Functions inherited from components::LoggableComponentBase
static yaml_config::Schema GetStaticConfigSchema ()
 

Protected Member Functions

virtual Value DoGetByKey (const Key &key)=0
 
std::shared_ptr< CacheGetCacheRaw ()
 

Member Typedef Documentation

◆ Cache

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
using cache::LruCacheComponent< Key, Value, Hash, Equal >::Cache = ExpirableLruCache<Key, Value, Hash, Equal>

Definition at line 80 of file lru_cache_component_base.hpp.

◆ CacheWrapper

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
using cache::LruCacheComponent< Key, Value, Hash, Equal >::CacheWrapper = LruCacheWrapper<Key, Value, Hash, Equal>

Definition at line 81 of file lru_cache_component_base.hpp.

Constructor & Destructor Documentation

◆ LruCacheComponent()

template<typename Key , typename Value , typename Hash , typename Equal >
cache::LruCacheComponent< Key, Value, Hash, Equal >::LruCacheComponent ( const components::ComponentConfig & config,
const components::ComponentContext & context )

Definition at line 125 of file lru_cache_component_base.hpp.

◆ ~LruCacheComponent()

template<typename Key , typename Value , typename Hash , typename Equal >
cache::LruCacheComponent< Key, Value, Hash, Equal >::~LruCacheComponent ( )
override

Definition at line 164 of file lru_cache_component_base.hpp.

Member Function Documentation

◆ GetCache()

template<typename Key , typename Value , typename Hash , typename Equal >
LruCacheComponent< Key, Value, Hash, Equal >::CacheWrapper cache::LruCacheComponent< Key, Value, Hash, Equal >::GetCache ( )

Definition at line 176 of file lru_cache_component_base.hpp.

◆ GetCacheRaw()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
std::shared_ptr< Cache > cache::LruCacheComponent< Key, Value, Hash, Equal >::GetCacheRaw ( )
inlineprotected

Definition at line 95 of file lru_cache_component_base.hpp.

◆ GetStaticConfigSchema()

template<typename Key , typename Value , typename Hash , typename Equal >
yaml_config::Schema cache::LruCacheComponent< Key, Value, Hash, Equal >::GetStaticConfigSchema ( )
static

Definition at line 213 of file lru_cache_component_base.hpp.


The documentation for this class was generated from the following file: