8#include <userver/cache/expirable_lru_cache.hpp>
9#include <userver/cache/lru_cache_config.hpp>
10#include <userver/components/component_base.hpp>
11#include <userver/concurrent/async_event_source.hpp>
12#include <userver/dump/dumper.hpp>
13#include <userver/dump/meta.hpp>
14#include <userver/dump/operations.hpp>
15#include <userver/dynamic_config/source.hpp>
16#include <userver/logging/log.hpp>
17#include <userver/testsuite/cache_control.hpp>
18#include <userver/utils/statistics/entry.hpp>
19#include <userver/yaml_config/schema.hpp>
21USERVER_NAMESPACE_BEGIN
27void RegisterOnStatisticsStorage(
29 const std::string& name,
33dynamic_config::Source FindDynamicConfigSource(
const components::ComponentContext& context);
35bool IsDumpSupportEnabled(
const components::ComponentConfig& config);
37yaml_config::Schema GetLruCacheComponentBaseSchema();
69template <
typename Key,
typename Value,
typename Hash = std::hash<Key>,
typename Equal = std::equal_to<Key>>
73 using Cache = ExpirableLruCache<Key, Value, Hash, Equal>;
74 using CacheWrapper = LruCacheWrapper<Key, Value, Hash, Equal>;
78 ~LruCacheComponent()
override;
80 CacheWrapper GetCache();
82 static yaml_config::Schema GetStaticConfigSchema();
85 virtual Value DoGetByKey(
const Key& key) = 0;
87 std::shared_ptr<Cache> GetCacheRaw() {
return cache_; }
92 Value GetByKey(
const Key& key);
94 void OnConfigUpdate(
const dynamic_config::Snapshot& cfg);
96 void UpdateConfig(
const LruCacheConfig& config);
100 void GetAndWrite(
dump::
Writer& writer)
const override;
101 void ReadAndSet(
dump::
Reader& reader)
override;
103 const std::string name_;
104 const LruCacheConfigStatic static_config_;
105 const std::shared_ptr<Cache> cache_;
106 std::shared_ptr<
dump::Dumper> dumper_;
109 concurrent::AsyncEventSubscriberScope config_subscription_;
112template <
typename Key,
typename Value,
typename Hash,
typename Equal>
117 Equal>::LruCacheComponent(
const components::ComponentConfig& config,
const components::ComponentContext& context)
120 static_config_(config),
121 cache_(std::make_shared<Cache>(static_config_.ways, static_config_.GetWaySize()))
123 if (impl::IsDumpSupportEnabled(config)) {
125 cache_->SetDumper(dumper_);
129 cache_->SetMaxLifetime(static_config_.config.lifetime);
130 cache_->SetBackgroundUpdate(static_config_.config.background_update);
132 if (static_config_.use_dynamic_config) {
134 <<
"Dynamic LRU cache config is enabled, subscribing on "
135 "dynamic-config updates, cache="
138 config_subscription_ =
139 impl::FindDynamicConfigSource(context)
142 LOG_INFO() <<
"Dynamic LRU cache config is disabled, cache=" << name_;
145 impl::RegisterOnStatisticsStorage(context, name_, [
this](
utils::
statistics::Writer& writer) { writer = *cache_; });
150template <
typename Key,
typename Value,
typename Hash,
typename Equal>
159template <
typename Key,
typename Value,
typename Hash,
typename Equal>
165 return CacheWrapper(cache_, [
this](
const Key& key) {
return GetByKey(key); });
168template <
typename Key,
typename Value,
typename Hash,
typename Equal>
170 cache_->Invalidate();
173template <
typename Key,
typename Value,
typename Hash,
typename Equal>
175 return DoGetByKey(key);
178template <
typename Key,
typename Value,
typename Hash,
typename Equal>
179void LruCacheComponent<Key, Value, Hash, Equal>::OnConfigUpdate(
const dynamic_config::Snapshot& cfg) {
180 const auto config = GetLruConfig(cfg, name_);
182 LOG_DEBUG() <<
"Using dynamic config for LRU cache";
183 UpdateConfig(*config);
185 LOG_DEBUG() <<
"Using static config for LRU cache";
186 UpdateConfig(static_config_.config);
190template <
typename Key,
typename Value,
typename Hash,
typename Equal>
191void LruCacheComponent<Key, Value, Hash, Equal>::UpdateConfig(
const LruCacheConfig& config) {
192 cache_->SetWaySize(config.GetWaySize(static_config_.ways));
193 cache_->SetMaxLifetime(config.lifetime);
194 cache_->SetBackgroundUpdate(config.background_update);
197template <
typename Key,
typename Value,
typename Hash,
typename Equal>
198yaml_config::Schema
LruCacheComponent<Key, Value, Hash, Equal>::GetStaticConfigSchema() {
199 return impl::GetLruCacheComponentBaseSchema();
202template <
typename Key,
typename Value,
typename Hash,
typename Equal>
204 if constexpr (kCacheIsDumpable) {
205 cache_->Write(writer);
207 dump::ThrowDumpUnimplemented(name_);
211template <
typename Key,
typename Value,
typename Hash,
typename Equal>
213 if constexpr (kCacheIsDumpable) {
214 cache_->Read(reader);
216 dump::ThrowDumpUnimplemented(name_);