Skip to content

Commit 62d3df4

Browse files
author
svetoch
committed
feat userver: implement data_provider
commit_hash:41d39f49c4499c0d36016cb51adf560a7505b7ff
1 parent 7f4e169 commit 62d3df4

4 files changed

Lines changed: 93 additions & 11 deletions

File tree

.mapping.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@
801801
"core/include/userver/cache/cache_statistics.hpp":"taxi/uservices/userver/core/include/userver/cache/cache_statistics.hpp",
802802
"core/include/userver/cache/cache_update_trait.hpp":"taxi/uservices/userver/core/include/userver/cache/cache_update_trait.hpp",
803803
"core/include/userver/cache/caching_component_base.hpp":"taxi/uservices/userver/core/include/userver/cache/caching_component_base.hpp",
804+
"core/include/userver/cache/data_provider.hpp":"taxi/uservices/userver/core/include/userver/cache/data_provider.hpp",
804805
"core/include/userver/cache/exceptions.hpp":"taxi/uservices/userver/core/include/userver/cache/exceptions.hpp",
805806
"core/include/userver/cache/expirable_lru_cache.hpp":"taxi/uservices/userver/core/include/userver/cache/expirable_lru_cache.hpp",
806807
"core/include/userver/cache/lru_cache_component_base.hpp":"taxi/uservices/userver/core/include/userver/cache/lru_cache_component_base.hpp",
@@ -2055,6 +2056,7 @@
20552056
"core/sys_coro/include/coroutines/coroutine.hpp":"taxi/uservices/userver/core/sys_coro/include/coroutines/coroutine.hpp",
20562057
"core/uboost_coro/include/coroutines/coroutine.hpp":"taxi/uservices/userver/core/uboost_coro/include/coroutines/coroutine.hpp",
20572058
"core/utest/CMakeLists.txt":"taxi/uservices/userver/core/utest/CMakeLists.txt",
2059+
"core/utest/include/userver/cache/data_provider_mock.hpp":"taxi/uservices/userver/core/utest/include/userver/cache/data_provider_mock.hpp",
20582060
"core/utest/include/userver/cache/statistics_mock.hpp":"taxi/uservices/userver/core/utest/include/userver/cache/statistics_mock.hpp",
20592061
"core/utest/include/userver/dump/operations_mock.hpp":"taxi/uservices/userver/core/utest/include/userver/dump/operations_mock.hpp",
20602062
"core/utest/include/userver/dump/test_helpers.hpp":"taxi/uservices/userver/core/utest/include/userver/dump/test_helpers.hpp",

core/include/userver/cache/caching_component_base.hpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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

151152
template <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 {
154155
public:
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

183184
protected:
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

285294
template <typename T>
286295
void 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-
304316
template <typename T>
305317
template <typename... Args>
306318
void CachingComponentBase<T>::Emplace(Args&&... args) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
/// @file userver/cache/data_provider.hpp
4+
/// @brief A simple interface for hiding implementation and making mocks easy to implement
5+
6+
#include <userver/utils/shared_readable_ptr.hpp>
7+
8+
USERVER_NAMESPACE_BEGIN
9+
10+
namespace cache {
11+
12+
/// @brief Interface for providing cached data of type T
13+
/// @tparam T The type of data being provided
14+
///
15+
/// DataProvider is a simple interface that allows hiding implementation
16+
/// details and making mocks easy to implement. It provides a way to
17+
/// retrieve shared readable pointers to cached data.
18+
19+
template <typename T>
20+
class DataProvider {
21+
public:
22+
using DataType = T;
23+
24+
virtual ~DataProvider() = default;
25+
26+
virtual utils::SharedReadablePtr<DataType> Get() const = 0;
27+
};
28+
29+
} // namespace cache
30+
31+
USERVER_NAMESPACE_END
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
3+
/// @file userver/cache/data_provider_mock.hpp
4+
/// @brief Mock implementation of DataProvider
5+
6+
#include <userver/cache/data_provider.hpp>
7+
8+
USERVER_NAMESPACE_BEGIN
9+
10+
namespace cache {
11+
12+
/// @brief Mock implementation of DataProvider for testing purposes
13+
/// @tparam T The type of data being provided
14+
///
15+
/// DataProviderMock is a mock implementation of the DataProvider interface
16+
/// that stores static data and returns shared readable pointers to it.
17+
/// It's designed for use in unit tests where you want to provide
18+
/// controlled data to components that depend on DataProvider.
19+
template <typename T>
20+
class DataProviderMock : public DataProvider<T> {
21+
public:
22+
using DataProvider<T>::DataType;
23+
24+
explicit DataProviderMock(DataType data) : data_{std::make_shared<DataType>(std::move(data))} {}
25+
explicit DataProviderMock(std::shared_ptr<DataType> data) : data_{std::move(data)} {}
26+
27+
utils::SharedReadablePtr<DataType> Get() const final {
28+
return utils::SharedReadablePtr<DataType>(data_.ReadCopy());
29+
};
30+
31+
private:
32+
rcu::Variable<std::shared_ptr<const DataType>> data_;
33+
};
34+
35+
} // namespace cache
36+
37+
USERVER_NAMESPACE_END

0 commit comments

Comments
 (0)