userver: userver/testsuite/cache_control.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
cache_control.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/testsuite/cache_control.hpp
4/// @brief @copybrief testsuite::CacheControl
5
6#include <functional>
7#include <string>
8#include <unordered_set>
9#include <vector>
10
11#include <userver/cache/update_type.hpp>
12#include <userver/components/component_fwd.hpp>
13#include <userver/engine/mutex.hpp>
14#include <userver/utils/not_null.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace cache {
20struct Config;
21} // namespace cache
22
23namespace testsuite {
24
25/// @brief Periodically updated caches control interface for testsuite
26/// @details All methods are coro-safe.
27class CacheControl final {
28 public:
29 enum class PeriodicUpdatesMode { kDefault, kEnabled, kDisabled };
30
31 explicit CacheControl(PeriodicUpdatesMode);
32
33 /// Whether the cache with specified config should be updated periodically
34 bool IsPeriodicUpdateEnabled(const cache::Config& cache_config,
35 const std::string& cache_name) const;
36
37 void InvalidateAllCaches(cache::UpdateType update_type);
38
39 void InvalidateCaches(cache::UpdateType update_type,
40 std::unordered_set<std::string> names);
41
42 private:
43 friend class CacheInvalidatorHolder;
44
45 void RegisterCache(cache::CacheUpdateTrait& cache);
46
47 void UnregisterCache(cache::CacheUpdateTrait& cache);
48
49 const PeriodicUpdatesMode periodic_updates_mode_;
50
51 engine::Mutex mutex_;
52 std::vector<utils::NotNull<cache::CacheUpdateTrait*>> caches_;
53};
54
55/// RAII helper for testsuite registration
56class CacheInvalidatorHolder final {
57 public:
58 CacheInvalidatorHolder(CacheControl&, cache::CacheUpdateTrait&);
59 ~CacheInvalidatorHolder();
60
61 CacheInvalidatorHolder(const CacheInvalidatorHolder&) = delete;
62 CacheInvalidatorHolder(CacheInvalidatorHolder&&) = delete;
63 CacheInvalidatorHolder& operator=(const CacheInvalidatorHolder&) = delete;
64 CacheInvalidatorHolder& operator=(CacheInvalidatorHolder&&) = delete;
65
66 private:
67 CacheControl& cache_control_;
68 cache::CacheUpdateTrait& cache_;
69};
70
71} // namespace testsuite
72
73USERVER_NAMESPACE_END