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
27utils::statistics::Entry RegisterOnStatisticsStorage(
29 const std::string& name,
30 std::function<
void(
utils::statistics::Writer&)> func
33dynamic_config::Source FindDynamicConfigSource(
const components::ComponentContext& context);
35bool IsDumpSupportEnabled(
const components::ComponentConfig& config);
37yaml_config::Schema GetLruCacheComponentBaseSchema();
75template <
typename Key,
typename Value,
typename Hash = std::hash<Key>,
typename Equal = std::equal_to<Key>>
79 using Cache = ExpirableLruCache<Key, Value, Hash, Equal>;
80 using CacheWrapper = LruCacheWrapper<Key, Value, Hash, Equal>;
84 ~LruCacheComponent()
override;
86 CacheWrapper GetCache();
91 virtual Value DoGetByKey(
const Key& key) = 0;
93 std::shared_ptr<Cache> GetCacheRaw() {
return cache_; }
98 Value GetByKey(
const Key& key);
100 void OnConfigUpdate(
const dynamic_config::Snapshot& cfg);
102 void UpdateConfig(
const LruCacheConfig& config);
104 static constexpr bool kCacheIsDumpable =
dump::kIsDumpable<Key> &&
dump::kIsDumpable<Value>;
106 void GetAndWrite(
dump::
Writer& writer)
const override;
107 void ReadAndSet(
dump::
Reader& reader)
override;
109 const std::string name_;
110 const LruCacheConfigStatic static_config_;
111 std::shared_ptr<
dump::Dumper> dumper_;
112 const std::shared_ptr<Cache> cache_;
115 concurrent::AsyncEventSubscriberScope config_subscription_;
116 utils::statistics::Entry statistics_holder_;
117 testsuite::CacheResetRegistration reset_registration_;
121template <
typename Key,
typename Value,
typename Hash,
typename Equal>
128 static_config_(config),
129 cache_(std::make_shared<Cache>(static_config_.ways, static_config_.GetWaySize())) {
130 if (impl::IsDumpSupportEnabled(config)) {
131 dumper_ = std::make_shared<dump::Dumper>(config, context,
static_cast<dump::DumpableEntity&>(*
this));
132 cache_->SetDumper(dumper_);
136 cache_->SetMaxLifetime(static_config_.config.lifetime);
137 cache_->SetBackgroundUpdate(static_config_.config.background_update);
139 if (static_config_.use_dynamic_config) {
140 LOG_INFO() <<
"Dynamic LRU cache config is enabled, subscribing on "
141 "dynamic-config updates, cache="
144 config_subscription_ = impl::FindDynamicConfigSource(context).UpdateAndListen(
148 LOG_INFO() <<
"Dynamic LRU cache config is disabled, cache=" << name_;
151 statistics_holder_ = impl::RegisterOnStatisticsStorage(context, name_, [
this](utils::statistics::Writer& writer) {
158template <
typename Key,
typename Value,
typename Hash,
typename Equal>
161 statistics_holder_.Unregister();
165 dumper_->CancelWriteTaskAndWait();
169template <
typename Key,
typename Value,
typename Hash,
typename Equal>
172 return CacheWrapper(cache_, [
this](
const Key& key) {
return GetByKey(key); });
175template <
typename Key,
typename Value,
typename Hash,
typename Equal>
177 cache_->Invalidate();
180template <
typename Key,
typename Value,
typename Hash,
typename Equal>
182 return DoGetByKey(key);
185template <
typename Key,
typename Value,
typename Hash,
typename Equal>
186void LruCacheComponent<Key, Value, Hash, Equal>::OnConfigUpdate(
const dynamic_config::Snapshot& cfg) {
187 const auto config = GetLruConfig(cfg, name_);
189 LOG_DEBUG() <<
"Using dynamic config for LRU cache";
190 UpdateConfig(*config);
192 LOG_DEBUG() <<
"Using static config for LRU cache";
193 UpdateConfig(static_config_.config);
197template <
typename Key,
typename Value,
typename Hash,
typename Equal>
198void LruCacheComponent<Key, Value, Hash, Equal>::UpdateConfig(
const LruCacheConfig& config) {
199 cache_->SetWaySize(config.GetWaySize(static_config_.ways));
200 cache_->SetMaxLifetime(config.lifetime);
201 cache_->SetBackgroundUpdate(config.background_update);
204template <
typename Key,
typename Value,
typename Hash,
typename Equal>
206 return impl::GetLruCacheComponentBaseSchema();
209template <
typename Key,
typename Value,
typename Hash,
typename Equal>
211 if constexpr (kCacheIsDumpable) {
212 cache_->Write(writer);
214 dump::ThrowDumpUnimplemented(name_);
218template <
typename Key,
typename Value,
typename Hash,
typename Equal>
220 if constexpr (kCacheIsDumpable) {
221 cache_->Read(reader);
223 dump::ThrowDumpUnimplemented(name_);