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
31 public:
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
46 protected:
47 /// @cond
48 // For internal use only
49 CacheUpdateTrait(const components::ComponentConfig& config,
50 const components::ComponentContext& context);
51
52 // For internal use only
53 explicit CacheUpdateTrait(CacheDependencies&& dependencies);
54
55 virtual ~CacheUpdateTrait();
56 /// @endcond
57
58 /// Update types configured for the cache
60
61 /// Periodic update flags
62 enum class Flag {
63 kNone = 0,
64 kNoFirstUpdate = 1 << 0, ///< Disable initial update on start
65 };
66
67 /// Starts periodic updates
68 void StartPeriodicUpdates(utils::Flags<Flag> flags = {});
69
70 /// @brief Stops periodic updates
71 /// @warning Should be called in destructor of derived class.
73
74 void AssertPeriodicUpdateStarted();
75
76 /// Called in `CachingComponentBase::Set` during update to indicate
77 /// that the cached data has been modified
79
80 /// @cond
81 // For internal use only
82 rcu::ReadablePtr<Config> GetConfig() const;
83
84 /// Checks for the presence of the flag for pre-assign check
85 bool HasPreAssignCheck() const;
86
87 // For internal use only
88 // TODO remove after TAXICOMMON-3959
89 engine::TaskProcessor& GetCacheTaskProcessor() const;
90 /// @endcond
91
92 /// @brief Should be overridden in a derived class to align the stored data
93 /// with some data source.
94 ///
95 /// `Update` implementation should do one of the following:
96 ///
97 /// A. If the update succeeded and has changes...
98 /// 1. call CachingComponentBase::Set to update the stored value and send a
99 /// notification to subscribers
100 /// 2. call UpdateStatisticsScope::Finish
101 /// 3. return normally (an exception is allowed in edge cases)
102 /// B. If the update succeeded and verified that there are no changes...
103 /// 1. DON'T call CachingComponentBase::Set
104 /// 2. call UpdateStatisticsScope::FinishNoChanges
105 /// 3. return normally (an exception is allowed in edge cases)
106 /// C. If the update failed...
107 /// 1. DON'T call CachingComponentBase::Set
108 /// 2. call UpdateStatisticsScope::FinishWithError, or...
109 /// 3. throw an exception, which will be logged nicely
110 ///
111 /// @param type type of the update
112 /// @param last_update time of the last update (value of `now` from previous
113 /// invocation of Update or default constructed value if this is the first
114 /// Update).
115 /// @param now current time point
116 ///
117 /// @throws std::exception on update failure
118 ///
119 /// @see @ref scripts/docs/en/userver/caches.md
120 virtual void Update(UpdateType type,
121 const std::chrono::system_clock::time_point& last_update,
122 const std::chrono::system_clock::time_point& now,
123 UpdateStatisticsScope& stats_scope) = 0;
124
125 private:
126 virtual void Cleanup() = 0;
127
128 virtual void MarkAsExpired();
129
130 virtual void GetAndWrite(dump::Writer& writer) const;
131
132 virtual void ReadAndSet(dump::Reader& reader);
133
134 class Impl;
135 std::unique_ptr<Impl> impl_;
136};
137
138} // namespace cache
139
140USERVER_NAMESPACE_END