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/dump/dumper.hpp>
12#include <userver/dump/meta.hpp>
13#include <userver/dump/operations.hpp>
14#include <userver/dynamic_config/source.hpp>
15#include <userver/logging/log.hpp>
16#include <userver/testsuite/cache_control.hpp>
17#include <userver/utils/statistics/fwd.hpp>
18#include <userver/yaml_config/schema.hpp>
20USERVER_NAMESPACE_BEGIN
26void RegisterOnStatisticsStorage(
28 const std::string& name,
32dynamic_config::Source FindDynamicConfigSource(
const components::ComponentContext& context);
34dynamic_config::NoblockSubscriber FindDynamicConfigNoblockSubscriber(
const components::ComponentContext& context);
36bool IsDumpSupportEnabled(
const components::ComponentConfig& config);
38yaml_config::Schema GetLruCacheComponentBaseSchema();
70template <
typename Key,
typename Value,
typename Hash = std::hash<Key>,
typename Equal = std::equal_to<Key>>
74 using Cache = ExpirableLruCache<Key, Value, Hash, Equal>;
75 using CacheWrapper = LruCacheWrapper<Key, Value, Hash, Equal>;
79 ~LruCacheComponent()
override;
81 CacheWrapper GetCache();
83 static yaml_config::Schema GetStaticConfigSchema();
86 virtual Value DoGetByKey(
const Key& key) = 0;
88 std::shared_ptr<Cache> GetCacheRaw() {
return cache_; }
93 Value GetByKey(
const Key& key);
95 void OnConfigUpdate(
const dynamic_config::Snapshot& cfg);
97 void OnConfigUpdateWithoutWaiting(
const dynamic_config::Diff& diff);
99 void UpdateConfig(
const LruCacheConfig& config);
103 void GetAndWrite(
dump::
Writer& writer)
const override;
104 void ReadAndSet(
dump::
Reader& reader)
override;
106 const std::string name_;
107 const LruCacheConfigStatic static_config_;
108 const std::shared_ptr<Cache> cache_;
109 std::shared_ptr<
dump::Dumper> dumper_;
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 if (static_config_.wait_for_dynamic_configs) {
139 impl::FindDynamicConfigSource(context).UpdateAndListen(
146 impl::FindDynamicConfigNoblockSubscriber(context).UpdateIfHasConfigAndListen(
154 LOG_INFO() <<
"Dynamic LRU cache config is disabled, cache=" << name_;
157 impl::RegisterOnStatisticsStorage(context, name_, [
this](
utils::
statistics::Writer& writer) { writer = *cache_; });
162template <
typename Key,
typename Value,
typename Hash,
typename Equal>
169template <
typename Key,
typename Value,
typename Hash,
typename Equal>
175 return CacheWrapper(cache_, [
this](
const Key& key) {
return GetByKey(key); });
178template <
typename Key,
typename Value,
typename Hash,
typename Equal>
180 cache_->Invalidate();
183template <
typename Key,
typename Value,
typename Hash,
typename Equal>
185 return DoGetByKey(key);
188template <
typename Key,
typename Value,
typename Hash,
typename Equal>
189void LruCacheComponent<Key, Value, Hash, Equal>::OnConfigUpdate(
const dynamic_config::Snapshot& cfg) {
190 const auto config = GetLruConfig(cfg, name_);
192 LOG_DEBUG() <<
"Using dynamic config for LRU cache";
193 UpdateConfig(*config);
195 LOG_DEBUG() <<
"Using static config for LRU cache";
196 UpdateConfig(static_config_.config);
200template <
typename Key,
typename Value,
typename Hash,
typename Equal>
201void LruCacheComponent<Key, Value, Hash, Equal>::OnConfigUpdateWithoutWaiting(
const dynamic_config::Diff& diff) {
202 OnConfigUpdate(diff.current);
205template <
typename Key,
typename Value,
typename Hash,
typename Equal>
206void LruCacheComponent<Key, Value, Hash, Equal>::UpdateConfig(
const LruCacheConfig& config) {
207 cache_->SetWaySize(config.GetWaySize(static_config_.ways));
208 cache_->SetMaxLifetime(config.lifetime);
209 cache_->SetBackgroundUpdate(config.background_update);
212template <
typename Key,
typename Value,
typename Hash,
typename Equal>
213yaml_config::Schema
LruCacheComponent<Key, Value, Hash, Equal>::GetStaticConfigSchema() {
214 return impl::GetLruCacheComponentBaseSchema();
217template <
typename Key,
typename Value,
typename Hash,
typename Equal>
219 if constexpr (kCacheIsDumpable) {
220 cache_->Write(writer);
222 dump::ThrowDumpUnimplemented(name_);
226template <
typename Key,
typename Value,
typename Hash,
typename Equal>
228 if constexpr (kCacheIsDumpable) {
229 cache_->Read(reader);
231 dump::ThrowDumpUnimplemented(name_);