10#include <unordered_set>
12#include <userver/cache/update_type.hpp>
13#include <userver/components/component_fwd.hpp>
14#include <userver/components/state.hpp>
15#include <userver/utils/assert.hpp>
17USERVER_NAMESPACE_BEGIN
31enum class PeriodicUpdatesMode { kDefault, kEnabled, kDisabled };
34class CacheResetRegistration;
54class CacheControl
final {
62 const std::unordered_set<std::string>& force_incremental_names,
63 const std::unordered_set<std::string>& exclude_names);
71 std::unordered_set<std::string> reset_only_names,
72 const std::unordered_set<std::string>& force_incremental_names);
74 CacheControl(CacheControl&&) =
delete;
75 CacheControl& operator=(CacheControl&&) =
delete;
80 explicit UnitTests() =
default;
83 enum class ExecPolicy {
88 CacheControl(impl::PeriodicUpdatesMode, UnitTests);
89 CacheControl(impl::PeriodicUpdatesMode, ExecPolicy,
components::State);
93 bool IsPeriodicUpdateEnabled(
const cache::Config& cache_config,
94 const std::string& cache_name)
const;
100 template <
typename Component>
101 CacheResetRegistration RegisterCache(Component* self, std::string_view name,
102 void (Component::*reset_method)());
104 struct CacheInfo
final {
107 bool needs_span{
true};
109 struct CacheInfoNode;
110 using CacheInfoIterator = CacheInfoNode*;
113 CacheInfoIterator DoRegisterCache(CacheInfo&& info);
116 friend class CacheResetRegistration;
122 std::unordered_set<std::string>* reset_only_names,
123 const std::unordered_set<std::string>& force_incremental_names,
124 const std::unordered_set<std::string>* exclude_names);
126 void DoResetCachesConcurrently(
128 std::unordered_set<std::string>* reset_only_names,
129 const std::unordered_set<std::string>& force_incremental_names,
130 const std::unordered_set<std::string>* exclude_names);
132 void UnregisterCache(CacheInfoIterator)
noexcept;
134 static void DoResetSingleCache(
136 const std::unordered_set<std::string>& force_incremental_names);
139 std::unique_ptr<Impl> impl_;
147class [[nodiscard]] CacheResetRegistration
final {
149 CacheResetRegistration()
noexcept;
151 CacheResetRegistration(CacheResetRegistration&&)
noexcept;
152 CacheResetRegistration& operator=(CacheResetRegistration&&)
noexcept;
153 ~CacheResetRegistration();
161 CacheResetRegistration(CacheControl&, CacheControl::CacheInfoIterator);
165 CacheControl* cache_control_{
nullptr};
166 CacheControl::CacheInfoIterator cache_info_iterator_{};
180template <
typename Component>
183 const components::ComponentContext& context, Component* self,
184 void (Component::*reset_method)()) {
190template <
typename Component>
191CacheResetRegistration CacheControl::RegisterCache(
192 Component* self, std::string_view name,
void (Component::*reset_method)()) {
194 "CacheControl can only be used with components");
199 info.name = std::string{name};
200 info.reset = [self, reset_method]([[maybe_unused]]
cache::
UpdateType) {
201 (self->*reset_method)();
203 info.needs_span =
true;
205 auto iter = DoRegisterCache(std::move(info));
206 return CacheResetRegistration(*
this, std::move(iter));