userver: cache::LruCacheComponent< Key, Value, Hash, Equal > Class Template Reference
Loading...
Searching...
No Matches
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.

LruCacheComponent 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, const components::ComponentContext& context)
: ::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:

# 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.

+ Inheritance 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 ()
 
ComponentHealth GetComponentHealth () const override
 
void OnLoadingCancelled () override
 
void OnAllComponentsLoaded () override
 
void OnAllComponentsAreStopping () override
 

Static Public Member Functions

static yaml_config::Schema GetStaticConfigSchema ()
 

Protected Types

using LoggableComponentBase = ComponentBase
 Legacy alias, use ComponentBase instead.
 

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 79 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 80 of file lru_cache_component_base.hpp.

◆ LoggableComponentBase

using components::ComponentBase::LoggableComponentBase = ComponentBase
protectedinherited

Legacy alias, use ComponentBase instead.

Definition at line 67 of file 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 122 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 159 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 170 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 93 of file lru_cache_component_base.hpp.

◆ GetComponentHealth()

ComponentHealth components::ComponentBase::GetComponentHealth ( ) const
inlineoverridevirtualinherited

Override this function to inform the world of the state of your component.

Warning
The function is called concurrently from multiple threads.

Reimplemented from components::RawComponentBase.

Reimplemented in server::handlers::Restart.

Definition at line 35 of file 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 205 of file lru_cache_component_base.hpp.

◆ OnAllComponentsAreStopping()

void components::ComponentBase::OnAllComponentsAreStopping ( )
inlineoverridevirtualinherited

Component may use this function to stop doing work before the stop of the components that depend on it.

Base components may override it and make final to do some work before the derived object constructor is called. Don't use it otherwise.

Reimplemented from components::RawComponentBase.

Reimplemented in urabbitmq::ConsumerComponentBase, and components::Server.

Definition at line 58 of file component_base.hpp.

◆ OnAllComponentsLoaded()

void components::ComponentBase::OnAllComponentsLoaded ( )
inlineoverridevirtualinherited

Component may use this function to finalize registration of other components that depend on it (for example, handler components register in server component, and the latter uses OnAllComponentsLoaded() to start processing requests).

Base components may override it and make final to do some work after the derived object constructor is called. Don't use it otherwise.

Reimplemented from components::RawComponentBase.

Reimplemented in urabbitmq::ConsumerComponentBase, components::Server, and server::handlers::Ping.

Definition at line 51 of file component_base.hpp.

◆ OnLoadingCancelled()

void components::ComponentBase::OnLoadingCancelled ( )
inlineoverridevirtualinherited

Called once if the creation of any other component failed. If the current component expects some other component to take any action with the current component, this call is a signal that such action may never happen due to components loading was cancelled. Application components might not want to override it.

Reimplemented from components::RawComponentBase.

Definition at line 42 of file component_base.hpp.


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