10#include <unordered_set>
12#include <userver/cache/update_type.hpp>
13#include <userver/components/component_fwd.hpp>
14#include <userver/utils/assert.hpp>
16USERVER_NAMESPACE_BEGIN
30enum class PeriodicUpdatesMode { kDefault, kEnabled, kDisabled };
33class CacheResetRegistration;
53class CacheControl
final {
61 template <
typename Component>
62 CacheResetRegistration
RegisterCache(Component* self, std::string_view name,
63 void (Component::*reset_method)());
71 const std::unordered_set<std::string>& force_incremental_names);
79 std::unordered_set<std::string> reset_only_names,
80 const std::unordered_set<std::string>& force_incremental_names);
84 explicit CacheControl(impl::PeriodicUpdatesMode);
87 CacheControl(CacheControl&&) =
delete;
88 CacheControl& operator=(CacheControl&&) =
delete;
91 bool IsPeriodicUpdateEnabled(
const cache::Config& cache_config,
92 const std::string& cache_name)
const;
95 CacheResetRegistration RegisterPeriodicCache(cache::
CacheUpdateTrait& cache);
98 friend class CacheResetRegistration;
100 struct CacheInfo
final {
103 bool needs_span{
true};
104 components::impl::ComponentBase* component =
nullptr;
107 struct CacheInfoNode;
108 using CacheInfoIterator = CacheInfoNode*;
110 CacheInfoIterator DoRegisterCache(CacheInfo&& info);
112 void UnregisterCache(CacheInfoIterator)
noexcept;
114 static void DoResetCache(
const CacheInfo& info,
118 std::unique_ptr<Impl> impl_;
126class [[nodiscard]] CacheResetRegistration
final {
128 CacheResetRegistration()
noexcept;
130 CacheResetRegistration(CacheResetRegistration&&)
noexcept;
131 CacheResetRegistration& operator=(CacheResetRegistration&&)
noexcept;
132 ~CacheResetRegistration();
136 void Unregister()
noexcept;
140 CacheResetRegistration(CacheControl&, CacheControl::CacheInfoIterator);
144 CacheControl* cache_control_{
nullptr};
145 CacheControl::CacheInfoIterator cache_info_iterator_{};
154template <
typename Component>
157 const components::ComponentContext& context, Component* self,
158 void (Component::*reset_method)()) {
164template <
typename Component>
166 Component* self, std::string_view name,
void (Component::*reset_method)()) {
167 static_assert(std::is_base_of_v<
components::impl::ComponentBase, Component>,
168 "CacheControl can only be used with components");
173 info.component = self;
174 info.name = std::string{name};
175 info.reset = [self, reset_method]([[maybe_unused]] cache::
UpdateType) {
176 (self->*reset_method)();
178 info.needs_span =
true;
180 auto iter = DoRegisterCache(std::move(info));
181 return CacheResetRegistration(*
this, std::move(iter));