userver: userver/cache/lru_cache_component_base.hpp Source File
Loading...
Searching...
No Matches
lru_cache_component_base.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/cache/lru_cache_component_base.hpp
4/// @brief @copybrief cache::LruCacheComponent
5
6#include <functional>
7
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>
19
20USERVER_NAMESPACE_BEGIN
21
22namespace cache {
23
24namespace impl {
25
26void RegisterOnStatisticsStorage(
27 const components::ComponentContext& context,
28 const std::string& name,
29 std::function<void(utils::statistics::Writer&)> func
30);
31
32dynamic_config::Source FindDynamicConfigSource(const components::ComponentContext& context);
33
34dynamic_config::NoblockSubscriber FindDynamicConfigNoblockSubscriber(const components::ComponentContext& context);
35
36bool IsDumpSupportEnabled(const components::ComponentConfig& config);
37
38yaml_config::Schema GetLruCacheComponentBaseSchema();
39
40} // namespace impl
41
42/// @ingroup userver_components userver_base_classes
43///
44/// @brief Base class for @ref scripts/docs/en/userver/lru_cache.md "LRU and Expirable LRU cache" components.
45///
46/// Provides facilities for creating @ref scripts/docs/en/userver/lru_cache.md "LRU and Expirable LRU caches".
47/// You need to override LruCacheComponent::DoGetByKey to handle cache misses.
48///
49/// Caching components must be configured in service config (see options below)
50/// and may be reconfigured dynamically via @ref components::DynamicConfig.
51///
52/// ## LruCacheComponent Dynamic config
53/// * @ref USERVER_LRU_CACHES
54///
55/// ## Static options of cache::LruCacheComponent :
56/// @include{doc} scripts/docs/en/components_schema/core/src/cache/lru_cache_component_base.md
57///
58/// Options inherited from @ref components::ComponentBase :
59/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
60///
61/// ## Example usage:
62///
63/// @snippet core/src/cache/lru_cache_component_base_test.hpp Sample lru cache component
64///
65/// Do not forget to @ref userver_components "add the component to component list":
66/// @snippet core/src/cache/lru_cache_component_base_test.cpp Sample lru cache component registration
67///
68/// ## Example config:
69/// @snippet core/src/cache/lru_cache_component_base_test.cpp Sample lru cache component config
70template <typename Key, typename Value, typename Hash = std::hash<Key>, typename Equal = std::equal_to<Key>>
71// NOLINTNEXTLINE(fuchsia-multiple-inheritance)
73public:
74 using Cache = ExpirableLruCache<Key, Value, Hash, Equal>;
75 using CacheWrapper = LruCacheWrapper<Key, Value, Hash, Equal>;
76
77 LruCacheComponent(const components::ComponentConfig&, const components::ComponentContext&);
78
79 ~LruCacheComponent() override;
80
81 CacheWrapper GetCache();
82
83 static yaml_config::Schema GetStaticConfigSchema();
84
85protected:
86 virtual Value DoGetByKey(const Key& key) = 0;
87
88 std::shared_ptr<Cache> GetCacheRaw() { return cache_; }
89
90private:
91 void DropCache();
92
93 Value GetByKey(const Key& key);
94
95 void OnConfigUpdate(const dynamic_config::Snapshot& cfg);
96
97 void OnConfigUpdateWithoutWaiting(const dynamic_config::Diff& diff);
98
99 void UpdateConfig(const LruCacheConfig& config);
100
101 static constexpr bool kCacheIsDumpable = dump::kIsDumpable<Key> && dump::kIsDumpable<Value>;
102
103 void GetAndWrite(dump::Writer& writer) const override;
104 void ReadAndSet(dump::Reader& reader) override;
105
106 const std::string name_;
107 const LruCacheConfigStatic static_config_;
108 const std::shared_ptr<Cache> cache_;
109 std::shared_ptr<dump::Dumper> dumper_;
110};
111
112template <typename Key, typename Value, typename Hash, typename Equal>
114 Key,
115 Value,
116 Hash,
117 Equal>::LruCacheComponent(const components::ComponentConfig& config, const components::ComponentContext& context)
118 : ComponentBase(config, context),
120 static_config_(config),
121 cache_(std::make_shared<Cache>(static_config_.ways, static_config_.GetWaySize()))
122{
123 if (impl::IsDumpSupportEnabled(config)) {
124 dumper_ = std::make_shared<dump::Dumper>(config, context, static_cast<dump::DumpableEntity&>(*this));
125 cache_->SetDumper(dumper_);
126 dumper_->ReadDump();
127 }
128
129 cache_->SetMaxLifetime(static_config_.config.lifetime);
130 cache_->SetBackgroundUpdate(static_config_.config.background_update);
131
132 if (static_config_.use_dynamic_config) {
133 LOG_INFO()
134 << "Dynamic LRU cache config is enabled, subscribing on "
135 "dynamic-config updates, cache="
136 << name_;
137
138 if (static_config_.wait_for_dynamic_configs) {
139 impl::FindDynamicConfigSource(context).UpdateAndListen(
141 this,
142 "cache." + name_,
143 &LruCacheComponent::OnConfigUpdate
144 );
145 } else {
146 impl::FindDynamicConfigNoblockSubscriber(context).UpdateIfHasConfigAndListen(
148 this,
149 "cache." + name_,
150 &LruCacheComponent::OnConfigUpdateWithoutWaiting
151 );
152 }
153 } else {
154 LOG_INFO() << "Dynamic LRU cache config is disabled, cache=" << name_;
155 }
156
157 impl::RegisterOnStatisticsStorage(context, name_, [this](utils::statistics::Writer& writer) { writer = *cache_; });
158
159 testsuite::RegisterCacheResetter(context, this, &LruCacheComponent::DropCache);
160}
161
162template <typename Key, typename Value, typename Hash, typename Equal>
163LruCacheComponent<Key, Value, Hash, Equal>::~LruCacheComponent() {
164 if (dumper_) {
166 }
167}
168
169template <typename Key, typename Value, typename Hash, typename Equal>
170typename LruCacheComponent<Key, Value, Hash, Equal>::CacheWrapper LruCacheComponent<
171 Key,
172 Value,
173 Hash,
174 Equal>::GetCache() {
175 return CacheWrapper(cache_, [this](const Key& key) { return GetByKey(key); });
176}
177
178template <typename Key, typename Value, typename Hash, typename Equal>
179void LruCacheComponent<Key, Value, Hash, Equal>::DropCache() {
180 cache_->Invalidate();
181}
182
183template <typename Key, typename Value, typename Hash, typename Equal>
184Value LruCacheComponent<Key, Value, Hash, Equal>::GetByKey(const Key& key) {
185 return DoGetByKey(key);
186}
187
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_);
191 if (config) {
192 LOG_DEBUG() << "Using dynamic config for LRU cache";
193 UpdateConfig(*config);
194 } else {
195 LOG_DEBUG() << "Using static config for LRU cache";
196 UpdateConfig(static_config_.config);
197 }
198}
199
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);
203}
204
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);
210}
211
212template <typename Key, typename Value, typename Hash, typename Equal>
213yaml_config::Schema LruCacheComponent<Key, Value, Hash, Equal>::GetStaticConfigSchema() {
214 return impl::GetLruCacheComponentBaseSchema();
215}
216
217template <typename Key, typename Value, typename Hash, typename Equal>
218void LruCacheComponent<Key, Value, Hash, Equal>::GetAndWrite(dump::Writer& writer) const {
219 if constexpr (kCacheIsDumpable) {
220 cache_->Write(writer);
221 } else {
222 dump::ThrowDumpUnimplemented(name_);
223 }
224}
225
226template <typename Key, typename Value, typename Hash, typename Equal>
227void LruCacheComponent<Key, Value, Hash, Equal>::ReadAndSet(dump::Reader& reader) {
228 if constexpr (kCacheIsDumpable) {
229 cache_->Read(reader);
230 } else {
231 dump::ThrowDumpUnimplemented(name_);
232 }
233}
234
235} // namespace cache
236
237USERVER_NAMESPACE_END