userver: userver/cache/cache_update_trait.hpp Source File
Loading...
Searching...
No Matches
cache_update_trait.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/cache/cache_update_trait.hpp
4/// @brief @copybrief cache::CacheUpdateTrait
5
6#include <memory>
7#include <string>
8
9#include <userver/cache/cache_statistics.hpp>
10#include <userver/cache/update_type.hpp>
11#include <userver/components/component_fwd.hpp>
12#include <userver/dump/fwd.hpp>
13#include <userver/engine/task/task_processor_fwd.hpp>
14#include <userver/rcu/fwd.hpp>
15#include <userver/utils/flags.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace cache {
20
21struct CacheDependencies;
22struct Config;
23
24/// @ingroup userver_base_classes
25///
26/// @brief Base class for periodically updated caches.
27///
28/// @note Don't use directly, inherit from components::CachingComponentBase
29/// instead
31public:
32 CacheUpdateTrait(CacheUpdateTrait&&) = delete;
33 CacheUpdateTrait& operator=(CacheUpdateTrait&&) = delete;
34
35 /// @brief Non-blocking forced cache update of specified type
36 /// @see PeriodicTask::ForceStepAsync for behavior details
37 void InvalidateAsync(UpdateType update_type);
38
39 /// @brief Forces a cache update of specified type
40 /// @throws If `Update` throws
41 void UpdateSyncDebug(UpdateType update_type);
42
43 /// @return name of the component
44 const std::string& Name() const;
45
46protected:
47 /// @cond
48 // For internal use only
49 CacheUpdateTrait(const components::ComponentConfig& config, const components::ComponentContext& context);
50
51 // For internal use only
52 explicit CacheUpdateTrait(CacheDependencies&& dependencies);
53
54 virtual ~CacheUpdateTrait();
55 /// @endcond
56
57 /// Update types configured for the cache
58 AllowedUpdateTypes GetAllowedUpdateTypes() const;
59
60 /// Periodic update flags
61 enum class Flag {
62 kNone = 0,
63
64 /// @brief Disable initial update on start
65 /// @deprecated Use `first-update-fail-ok: true` instead
66 kNoFirstUpdate = 1 << 0,
67 };
68
69 /// Called in `CachingComponentBase::Set` during update to indicate
70 /// that the cached data has been modified
72
73 /// @cond
74 // For internal use only
75 rcu::ReadablePtr<Config> GetConfig() const;
76
77 // Checks for the presence of the flag for pre-assign check.
78 // For internal use only.
79 bool HasPreAssignCheck() const;
80
81 // Returns value of the flag safe-data-lifetime.
82 // For internal use only.
83 bool IsSafeDataLifetime() const;
84
85 // For internal use only.
86 void SetDataSizeStatistic(std::size_t size) noexcept;
87
88 // For internal use only
89 // TODO remove after TAXICOMMON-3959
90 engine::TaskProcessor& GetCacheTaskProcessor() const;
91 /// @endcond
92
93 /// @brief Should be overridden in a derived class to align the stored data
94 /// with some data source.
95 ///
96 /// `Update` implementation should do one of the following:
97 ///
98 /// A. If the update succeeded and has changes...
99 /// 1. call CachingComponentBase::Set to update the stored value and send a
100 /// notification to subscribers
101 /// 2. call UpdateStatisticsScope::Finish
102 /// 3. return normally (an exception is allowed in edge cases)
103 ///
104 /// B. If the update succeeded and verified that there are no changes...
105 /// 1. DON'T call CachingComponentBase::Set
106 /// 2. call UpdateStatisticsScope::FinishNoChanges
107 /// 3. return normally (an exception is allowed in edge cases)
108 ///
109 /// C. If the update failed...
110 /// 1. DON'T call CachingComponentBase::Set
111 /// 2. call UpdateStatisticsScope::FinishWithError, or...
112 /// 3. throw an exception, which will be logged nicely
113 /// (if there already is an exception, prefer rethrowing it instead
114 /// of calling UpdateStatisticsScope::FinishWithError)
115 ///
116 /// @param type type of the update
117 /// @param last_update time of the last update (value of `now` from previous
118 /// invocation of Update or default constructed value if this is the first
119 /// Update).
120 /// @param now current time point
121 /// @param stats_scope the scope that expects
122 /// one of `Finish`, `FinishNoChanges`, `FinishWithError` or an exception.
123 ///
124 /// @throws std::exception on update failure
125 ///
126 /// @warning If `Update` returns without throwing an exception and without
127 /// calling one of the `Finish*` methods, the behavior is undefined.
128 ///
129 /// @see @ref scripts/docs/en/userver/caches.md
130 virtual void Update(
131 UpdateType type,
132 const std::chrono::system_clock::time_point& last_update,
133 const std::chrono::system_clock::time_point& now,
134 UpdateStatisticsScope& stats_scope
135 ) = 0;
136
137 /// @brief Returns flags for cache start.
138 virtual utils::Flags<Flag> GetStartFlags() const;
139
140 /// @brief Call this to start periodic updates just now,
141 /// not after the constuctor.
142 void EarlyStartPeriodicUpdates(utils::Flags<Flag> flags);
143
144 /// @cond
145 // For internal use only
146 void EarlyStopPeriodicUpdates();
147 /// @endcond
148
149private:
150 void StartPeriodicUpdates();
151
152 void StopPeriodicUpdates();
153
154 virtual void Cleanup() = 0;
155
156 virtual void MarkAsExpired();
157
158 virtual void GetAndWrite(dump::Writer& writer) const;
159
160 virtual void ReadAndSet(dump::Reader& reader);
161
162 class Impl;
163 std::unique_ptr<Impl> impl_;
164};
165
166} // namespace cache
167
168USERVER_NAMESPACE_END