20USERVER_NAMESPACE_BEGIN
26void RegisterOnStatisticsStorage(
27 const components::ComponentContext& context,
28 const std::string& name,
29 std::function<
void(utils::statistics::Writer&)> func
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>>
79 ~LruCacheComponent()
override;
81 CacheWrapper GetCache();
86 virtual Value DoGetByKey(
const Key& key) = 0;
88 std::shared_ptr<Cache> GetCacheRaw() {
return cache_; }
93 Value GetByKey(
const Key& key);
106 const std::string name_;
108 const std::shared_ptr<Cache> cache_;
109 std::shared_ptr<dump::Dumper> dumper_;
112template <
typename Key,
typename Value,
typename Hash,
typename Equal>
118 : ComponentBase(config, context),
119 name_(
components::GetCurrentComponentName(context)),
120 static_config_(config),
121 cache_(
std::make_shared<Cache>(static_config_.ways, static_config_.GetWaySize()))
123 if (impl::IsDumpSupportEnabled(config)) {
124 dumper_ = std::make_shared<dump::Dumper>(config, context,
static_cast<dump::DumpableEntity&
>(*
this));
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(
140 components::GetResourceScopes(context),
143 &LruCacheComponent::OnConfigUpdate
146 impl::FindDynamicConfigNoblockSubscriber(context).UpdateIfHasConfigAndListen(
147 components::GetResourceScopes(context),
150 &LruCacheComponent::OnConfigUpdateWithoutWaiting
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>
163LruCacheComponent<Key, Value, Hash, Equal>::~LruCacheComponent() {
165 dumper_->CancelWriteTaskAndWait();
169template <
typename Key,
typename Value,
typename Hash,
typename Equal>
170typename LruCacheComponent<Key, Value, Hash, Equal>::CacheWrapper LruCacheComponent<
175 return CacheWrapper(cache_, [
this](
const Key& key) {
return GetByKey(key); });
178template <
typename Key,
typename Value,
typename Hash,
typename Equal>
179void LruCacheComponent<Key, Value, Hash, Equal>::DropCache() {
180 cache_->Invalidate();
183template <
typename Key,
typename Value,
typename Hash,
typename Equal>
184Value LruCacheComponent<Key, Value, Hash, Equal>::GetByKey(
const Key& key) {
185 return DoGetByKey(key);
188template <
typename Key,
typename Value,
typename Hash,
typename Equal>
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>
214 return impl::GetLruCacheComponentBaseSchema();
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);
222 dump::ThrowDumpUnimplemented(name_);
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);
231 dump::ThrowDumpUnimplemented(name_);