1010#include < fmt/format.h>
1111
1212#include < userver/cache/cache_update_trait.hpp>
13+ #include < userver/cache/data_provider.hpp>
1314#include < userver/cache/exceptions.hpp>
1415#include < userver/compiler/demangle.hpp>
1516#include < userver/components/component_base.hpp>
@@ -150,7 +151,7 @@ namespace components {
150151
151152template <typename T>
152153// NOLINTNEXTLINE(fuchsia-multiple-inheritance)
153- class CachingComponentBase : public ComponentBase , protected cache ::CacheUpdateTrait {
154+ class CachingComponentBase : public ComponentBase , public cache ::DataProvider<T>, protected cache::CacheUpdateTrait {
154155public:
155156 CachingComponentBase (const ComponentConfig& config, const ComponentContext&);
156157 ~CachingComponentBase () override ;
@@ -165,7 +166,7 @@ class CachingComponentBase : public ComponentBase, protected cache::CacheUpdateT
165166 // / returns `true`.
166167 // / @throws cache::EmptyCacheError if the contents are `nullptr`, and
167168 // / `MayReturnNull` returns `false` (which is the default behavior).
168- utils::SharedReadablePtr<T> Get () const ;
169+ utils::SharedReadablePtr<T> Get () const final ;
169170
170171 // / @return cache contents. May be nullptr regardless of `MayReturnNull`.
171172 utils::SharedReadablePtr<T> GetUnsafe () const ;
@@ -181,16 +182,24 @@ class CachingComponentBase : public ComponentBase, protected cache::CacheUpdateT
181182 static yaml_config::Schema GetStaticConfigSchema ();
182183
183184protected:
184- // / Sets the new value of cache. As a result the Get() member function starts
185- // / returning the value passed into this function after the Update() finishes.
185+ // / Sets the new value of cache. As a result the ` Get()` member function starts
186+ // / returning the value passed into this function after the ` Update()` finishes.
186187 // /
187- // / @warning Do not forget to update cache::UpdateStatisticsScope, otherwise
188+ // / @warning Do not forget to update @ref cache::UpdateStatisticsScope, otherwise
188189 // / the behavior is undefined.
189190 void Set (std::unique_ptr<const T> value_ptr);
190191
191192 // / @overload
192193 void Set (T&& value);
193194
195+ // / Attach the value of cache. As a result the `Get()` member function starts returning the value passed into
196+ // / this function after the `Update()` finishes. Does not take over into sole ownership. Do not use unless
197+ // / absolutely necessary. The object must be strictly thread-safe.
198+ // /
199+ // / @warning Do not forget to update @ref cache::UpdateStatisticsScope, otherwise
200+ // / the behavior is undefined.
201+ void Attach (const std::shared_ptr<const T>& value_ptr);
202+
194203 // / @overload Set()
195204 template <typename ... Args>
196205 void Emplace (Args&&... args);
@@ -284,8 +293,16 @@ utils::SharedReadablePtr<T> CachingComponentBase<T>::GetUnsafe() const {
284293
285294template <typename T>
286295void CachingComponentBase<T>::Set(std::unique_ptr<const T> value_ptr) {
287- const std::shared_ptr<const T> new_value = TransformNewValue (std::move (value_ptr));
296+ Attach (TransformNewValue (std::move (value_ptr)));
297+ }
288298
299+ template <typename T>
300+ void CachingComponentBase<T>::Set(T&& value) {
301+ Emplace (std::move (value));
302+ }
303+
304+ template <typename T>
305+ void CachingComponentBase<T>::Attach(const std::shared_ptr<const T>& new_value) {
289306 if (HasPreAssignCheck ()) {
290307 auto old_value = cache_.Read ();
291308 PreAssignCheck (old_value->get (), new_value.get ());
@@ -296,11 +313,6 @@ void CachingComponentBase<T>::Set(std::unique_ptr<const T> value_ptr) {
296313 OnCacheModified ();
297314}
298315
299- template <typename T>
300- void CachingComponentBase<T>::Set(T&& value) {
301- Emplace (std::move (value));
302- }
303-
304316template <typename T>
305317template <typename ... Args>
306318void CachingComponentBase<T>::Emplace(Args&&... args) {
0 commit comments