8#include <userver/cache/expirable_lru_cache.hpp>
9#include <userver/cache/lru_cache_config.hpp>
10#include <userver/components/loggable_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
27utils::statistics::Entry RegisterOnStatisticsStorage(
28 const components::ComponentContext& context,
const std::string& name,
29 std::function<
void(utils::statistics::Writer&)> func);
31dynamic_config::Source FindDynamicConfigSource(
34bool IsDumpSupportEnabled(
const components::ComponentConfig& config);
36yaml_config::Schema GetLruCacheComponentBaseSchema();
74template <
typename Key,
typename Value,
typename Hash = std::hash<Key>,
75 typename Equal = std::equal_to<Key>>
80 using Cache = ExpirableLruCache<Key, Value, Hash, Equal>;
81 using CacheWrapper = LruCacheWrapper<Key, Value, Hash, Equal>;
83 LruCacheComponent(
const components::ComponentConfig&,
86 ~LruCacheComponent()
override;
88 CacheWrapper GetCache();
93 virtual Value DoGetByKey(
const Key& key) = 0;
95 std::shared_ptr<Cache> GetCacheRaw() {
return cache_; }
100 Value GetByKey(
const Key& key);
102 void OnConfigUpdate(
const dynamic_config::Snapshot& cfg);
104 void UpdateConfig(
const LruCacheConfig& config);
106 static constexpr bool kCacheIsDumpable =
107 dump::kIsDumpable<Key> &&
dump::kIsDumpable<Value>;
109 void GetAndWrite(
dump::
Writer& writer)
const override;
110 void ReadAndSet(
dump::
Reader& reader)
override;
112 const std::string name_;
113 const LruCacheConfigStatic static_config_;
114 std::shared_ptr<
dump::Dumper> dumper_;
115 const std::shared_ptr<Cache> cache_;
118 concurrent::AsyncEventSubscriberScope config_subscription_;
119 utils::statistics::Entry statistics_holder_;
120 testsuite::CacheResetRegistration reset_registration_;
124template <
typename Key,
typename Value,
typename Hash,
typename Equal>
130 static_config_(config),
131 cache_(std::make_shared<Cache>(static_config_.ways,
132 static_config_.GetWaySize())) {
133 if (
impl::IsDumpSupportEnabled(config)) {
134 dumper_ = std::make_shared<dump::Dumper>(
135 config, context,
static_cast<dump::DumpableEntity&>(*
this));
136 cache_->SetDumper(dumper_);
140 cache_->SetMaxLifetime(static_config_.config.lifetime);
141 cache_->SetBackgroundUpdate(static_config_.config.background_update);
143 if (static_config_.use_dynamic_config) {
144 LOG_INFO() <<
"Dynamic LRU cache config is enabled, subscribing on "
145 "dynamic-config updates, cache="
148 config_subscription_ =
149 impl::FindDynamicConfigSource(context).UpdateAndListen(
152 LOG_INFO() <<
"Dynamic LRU cache config is disabled, cache=" << name_;
155 statistics_holder_ = impl::RegisterOnStatisticsStorage(
157 [
this](utils::statistics::Writer& writer) { writer = *cache_; });
159 reset_registration_ =
testsuite::RegisterCache(config, context,
this,
163template <
typename Key,
typename Value,
typename Hash,
typename Equal>
166 statistics_holder_.Unregister();
170 dumper_->CancelWriteTaskAndWait();
174template <
typename Key,
typename Value,
typename Hash,
typename Equal>
177 return CacheWrapper(cache_, [
this](
const Key& key) {
return GetByKey(key); });
180template <
typename Key,
typename Value,
typename Hash,
typename Equal>
182 cache_->Invalidate();
185template <
typename Key,
typename Value,
typename Hash,
typename Equal>
187 return DoGetByKey(key);
190template <
typename Key,
typename Value,
typename Hash,
typename Equal>
192 const dynamic_config::Snapshot& cfg) {
193 const auto config = GetLruConfig(cfg, name_);
195 LOG_DEBUG() <<
"Using dynamic config for LRU cache";
196 UpdateConfig(*config);
198 LOG_DEBUG() <<
"Using static config for LRU cache";
199 UpdateConfig(static_config_.config);
203template <
typename Key,
typename Value,
typename Hash,
typename Equal>
205 const LruCacheConfig& config) {
206 cache_->SetWaySize(config.GetWaySize(static_config_.ways));
207 cache_->SetMaxLifetime(config.lifetime);
208 cache_->SetBackgroundUpdate(config.background_update);
211template <
typename Key,
typename Value,
typename Hash,
typename Equal>
214 return impl::GetLruCacheComponentBaseSchema();
217template <
typename Key,
typename Value,
typename Hash,
typename Equal>
220 if constexpr (kCacheIsDumpable) {
221 cache_->Write(writer);
223 dump::ThrowDumpUnimplemented(name_);
227template <
typename Key,
typename Value,
typename Hash,
typename Equal>
230 if constexpr (kCacheIsDumpable) {
231 cache_->Read(reader);
233 dump::ThrowDumpUnimplemented(name_);