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
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 /// Starts periodic updates
70 void StartPeriodicUpdates(utils::Flags<Flag> flags = {});
71
72 /// @brief Stops periodic updates
73 /// @warning Should be called in destructor of derived class.
75
76 void AssertPeriodicUpdateStarted();
77
78 void AssertPeriodicUpdateStopped();
79
80 /// Called in `CachingComponentBase::Set` during update to indicate
81 /// that the cached data has been modified
83
84 /// @cond
85 // For internal use only
86 rcu::ReadablePtr<Config> GetConfig() const;
87
88 // Checks for the presence of the flag for pre-assign check.
89 // For internal use only.
90 bool HasPreAssignCheck() const;
91
92 // Returns value of the flag safe-data-lifetime.
93 // For internal use only.
94 bool IsSafeDataLifetime() const;
95
96 // For internal use only.
97 void SetDataSizeStatistic(std::size_t size) noexcept;
98
99 // For internal use only
100 // TODO remove after TAXICOMMON-3959
101 engine::TaskProcessor& GetCacheTaskProcessor() const;
102 /// @endcond
103
104 /// @brief Should be overridden in a derived class to align the stored data
105 /// with some data source.
106 ///
107 /// `Update` implementation should do one of the following:
108 ///
109 /// A. If the update succeeded and has changes...
110 /// 1. call CachingComponentBase::Set to update the stored value and send a
111 /// notification to subscribers
112 /// 2. call UpdateStatisticsScope::Finish
113 /// 3. return normally (an exception is allowed in edge cases)
114 ///
115 /// B. If the update succeeded and verified that there are no changes...
116 /// 1. DON'T call CachingComponentBase::Set
117 /// 2. call UpdateStatisticsScope::FinishNoChanges
118 /// 3. return normally (an exception is allowed in edge cases)
119 ///
120 /// C. If the update failed...
121 /// 1. DON'T call CachingComponentBase::Set
122 /// 2. call UpdateStatisticsScope::FinishWithError, or...
123 /// 3. throw an exception, which will be logged nicely
124 /// (if there already is an exception, prefer rethrowing it instead
125 /// of calling UpdateStatisticsScope::FinishWithError)
126 ///
127 /// @param type type of the update
128 /// @param last_update time of the last update (value of `now` from previous
129 /// invocation of Update or default constructed value if this is the first
130 /// Update).
131 /// @param now current time point
132 ///
133 /// @throws std::exception on update failure
134 ///
135 /// @warning If `Update` returns without throwing an exception and without
136 /// calling one of the `Finish*` methods, the behavior is undefined.
137 ///
138 /// @see @ref scripts/docs/en/userver/caches.md
139 virtual void Update(
140 UpdateType type,
141 const std::chrono::system_clock::time_point& last_update,
142 const std::chrono::system_clock::time_point& now,
143 UpdateStatisticsScope& stats_scope
144 ) = 0;
145
146private:
147 virtual void Cleanup() = 0;
148
149 virtual void MarkAsExpired();
150
151 virtual void GetAndWrite(dump::Writer& writer) const;
152
153 virtual void ReadAndSet(dump::Reader& reader);
154
155 class Impl;
156 std::unique_ptr<Impl> impl_;
157};
158
159} // namespace cache
160
161USERVER_NAMESPACE_END