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
72 std::unordered_set<std::string> reset_only_names,
73 const std::unordered_set<std::string>& force_incremental_names
76 CacheControl(CacheControl&&) =
delete;
77 CacheControl& operator=(CacheControl&&) =
delete;
82 explicit UnitTests() =
default;
85 enum class ExecPolicy {
90 CacheControl(impl::PeriodicUpdatesMode, UnitTests);
91 CacheControl(impl::PeriodicUpdatesMode, ExecPolicy,
components::State);
95 bool IsPeriodicUpdateEnabled(
const cache::Config& cache_config,
const std::string& cache_name)
const;
101 template <
typename Component>
102 CacheResetRegistration RegisterCache(Component* self, std::string_view name,
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
127 void DoResetCachesConcurrently(
129 std::unordered_set<std::string>* reset_only_names,
130 const std::unordered_set<std::string>& force_incremental_names,
131 const std::unordered_set<std::string>* exclude_names
134 void UnregisterCache(CacheInfoIterator)
noexcept;
136 static void DoResetSingleCache(
137 const CacheInfo& info,
139 const std::unordered_set<std::string>& force_incremental_names
143 std::unique_ptr<Impl> impl_;
151class [[nodiscard]] CacheResetRegistration
final {
153 CacheResetRegistration()
noexcept;
155 CacheResetRegistration(CacheResetRegistration&&)
noexcept;
156 CacheResetRegistration& operator=(CacheResetRegistration&&)
noexcept;
157 ~CacheResetRegistration();
165 CacheResetRegistration(CacheControl&, CacheControl::CacheInfoIterator);
169 CacheControl* cache_control_{
nullptr};
170 CacheControl::CacheInfoIterator cache_info_iterator_{};
184template <
typename Component>
189 void (Component::*reset_method)()
192 return cc.RegisterCache(self, components::GetCurrentComponentName(config), reset_method);
195template <
typename Component>
196CacheResetRegistration
197CacheControl::RegisterCache(Component* self, std::string_view name,
void (Component::*reset_method)()) {
205 info.name = std::string{name};
206 info.reset = [self, reset_method]([[maybe_unused]]
cache::
UpdateType) { (self->*reset_method)(); };
207 info.needs_span =
true;
209 auto iter = DoRegisterCache(std::move(info));
210 return CacheResetRegistration(*
this, std::move(iter));