rippled
Loading...
Searching...
No Matches
TaggedCache.h
1#ifndef XRPL_BASICS_TAGGEDCACHE_H_INCLUDED
2#define XRPL_BASICS_TAGGEDCACHE_H_INCLUDED
3
4#include <xrpl/basics/IntrusivePointer.h>
5#include <xrpl/basics/Log.h>
6#include <xrpl/basics/SharedWeakCachePointer.ipp>
7#include <xrpl/basics/UnorderedContainers.h>
8#include <xrpl/basics/hardened_hash.h>
9#include <xrpl/beast/clock/abstract_clock.h>
10#include <xrpl/beast/insight/Insight.h>
11
12#include <atomic>
13#include <functional>
14#include <mutex>
15#include <thread>
16#include <type_traits>
17#include <vector>
18
19namespace ripple {
20
33template <
34 class Key,
35 class T,
36 bool IsKeyCache = false,
37 class SharedWeakUnionPointerType = SharedWeakCachePointer<T>,
38 class SharedPointerType = std::shared_ptr<T>,
39 class Hash = hardened_hash<>,
40 class KeyEqual = std::equal_to<Key>,
41 class Mutex = std::recursive_mutex>
43{
44public:
45 using mutex_type = Mutex;
46 using key_type = Key;
47 using mapped_type = T;
49 using shared_weak_combo_pointer_type = SharedWeakUnionPointerType;
50 using shared_pointer_type = SharedPointerType;
51
52public:
54 std::string const& name,
55 int size,
56 clock_type::duration expiration,
58 beast::Journal journal,
59 beast::insight::Collector::ptr const& collector =
61
62public:
66
69 size() const;
70
71 int
72 getCacheSize() const;
73
74 int
75 getTrackSize() const;
76
77 float
79
80 void
82
83 void
85
89 template <class KeyComparable>
90 bool
91 touch_if_exists(KeyComparable const& key);
92
94
95 void
97
98 bool
99 del(key_type const& key, bool valid);
100
101public:
115 template <class R>
116 bool
118 key_type const& key,
119 SharedPointerType& data,
120 R&& replaceCallback);
121
122 bool
124 key_type const& key,
125 SharedPointerType const& data);
126
127 bool
128 canonicalize_replace_client(key_type const& key, SharedPointerType& data);
129
130 SharedPointerType
131 fetch(key_type const& key);
132
137 template <class ReturnType = bool>
138 auto
139 insert(key_type const& key, T const& value)
141
142 template <class ReturnType = bool>
143 auto
145
146 // VFALCO NOTE It looks like this returns a copy of the data in
147 // the output parameter 'data'. This could be expensive.
148 // Perhaps it should work like standard containers, which
149 // simply return an iterator.
150 //
151 bool
152 retrieve(key_type const& key, T& data);
153
156
158 getKeys() const;
159
160 // CachedSLEs functions.
162 double
163 rate() const;
164
170 template <class Handler>
171 SharedPointerType
172 fetch(key_type const& digest, Handler const& h);
173 // End CachedSLEs functions.
174
175private:
176 SharedPointerType
178
179 void
181
182private:
183 struct Stats
184 {
185 template <class Handler>
187 std::string const& prefix,
188 Handler const& handler,
189 beast::insight::Collector::ptr const& collector)
190 : hook(collector->make_hook(handler))
191 , size(collector->make_gauge(prefix, "size"))
192 , hit_rate(collector->make_gauge(prefix, "hit_rate"))
193 , hits(0)
194 , misses(0)
195 {
196 }
197
201
204 };
205
207 {
208 public:
210
211 explicit KeyOnlyEntry(clock_type::time_point const& last_access_)
212 : last_access(last_access_)
213 {
214 }
215
216 void
218 {
219 last_access = now;
220 }
221 };
222
224 {
225 public:
228
230 clock_type::time_point const& last_access_,
231 shared_pointer_type const& ptr_)
232 : ptr(ptr_), last_access(last_access_)
233 {
234 }
235
236 bool
237 isWeak() const
238 {
239 if (!ptr)
240 return true;
241 return ptr.isWeak();
242 }
243 bool
244 isCached() const
245 {
246 return ptr && ptr.isStrong();
247 }
248 bool
249 isExpired() const
250 {
251 return ptr.expired();
252 }
253 SharedPointerType
255 {
256 return ptr.lock();
257 }
258 void
260 {
261 last_access = now;
262 }
263 };
264
265 typedef
268
271
274
277
278 [[nodiscard]] std::thread
280 clock_type::time_point const& when_expire,
281 [[maybe_unused]] clock_type::time_point const& now,
282 typename KeyValueCacheType::map_type& partition,
283 SweptPointersVector& stuffToSweep,
284 std::atomic<int>& allRemovals,
286
287 [[nodiscard]] std::thread
289 clock_type::time_point const& when_expire,
290 clock_type::time_point const& now,
291 typename KeyOnlyCacheType::map_type& partition,
293 std::atomic<int>& allRemovals,
295
299
301
302 // Used for logging
304
305 // Desired number of cache entries (0 = ignore)
306 int const m_target_size;
307
308 // Desired maximum cache age
310
311 // Number of items cached
313 cache_type m_cache; // Hold strong reference to recent objects
316};
317
318} // namespace ripple
319
320#endif
A generic endpoint for log messages.
Definition Journal.h:41
typename Clock::time_point time_point
typename Clock::duration duration
A metric for measuring an integral value.
Definition Gauge.h:21
A reference to a handler for performing polled collection.
Definition Hook.h:13
static std::shared_ptr< Collector > New()
void touch(clock_type::time_point const &now)
KeyOnlyEntry(clock_type::time_point const &last_access_)
clock_type::time_point last_access
ValueEntry(clock_type::time_point const &last_access_, shared_pointer_type const &ptr_)
shared_weak_combo_pointer_type ptr
clock_type::time_point last_access
void touch(clock_type::time_point const &now)
Map/cache combination.
Definition TaggedCache.h:43
bool canonicalize(key_type const &key, SharedPointerType &data, R &&replaceCallback)
Replace aliased objects with originals.
bool touch_if_exists(KeyComparable const &key)
Refresh the last access time on a key if present.
bool canonicalize_replace_client(key_type const &key, SharedPointerType &data)
bool retrieve(key_type const &key, T &data)
bool del(key_type const &key, bool valid)
SharedPointerType fetch(key_type const &key)
beast::Journal m_journal
clock_type & clock()
Return the clock associated with the cache.
clock_type & m_clock
SharedWeakUnionPointerType shared_weak_combo_pointer_type
Definition TaggedCache.h:49
mutex_type & peekMutex()
SharedPointerType fetch(key_type const &digest, Handler const &h)
Fetch an item from the cache.
auto insert(key_type const &key, T const &value) -> std::enable_if_t<!IsKeyCache, ReturnType >
Insert the element into the container.
std::vector< key_type > getKeys() const
bool canonicalize_replace_cache(key_type const &key, SharedPointerType const &data)
std::size_t size() const
Returns the number of items in the container.
int getCacheSize() const
SharedPointerType shared_pointer_type
Definition TaggedCache.h:50
std::thread sweepHelper(clock_type::time_point const &when_expire, clock_type::time_point const &now, typename KeyOnlyCacheType::map_type &partition, SweptPointersVector &, std::atomic< int > &allRemovals, std::lock_guard< std::recursive_mutex > const &)
SharedPointerType initialFetch(key_type const &key, std::lock_guard< mutex_type > const &l)
std::thread sweepHelper(clock_type::time_point const &when_expire, clock_type::time_point const &now, typename KeyValueCacheType::map_type &partition, SweptPointersVector &stuffToSweep, std::atomic< int > &allRemovals, std::lock_guard< std::recursive_mutex > const &)
std::uint64_t m_misses
std::conditional< IsKeyCache, KeyOnlyEntry, ValueEntry >::type Entry
double rate() const
Returns the fraction of cache hits.
auto insert(key_type const &key) -> std::enable_if_t< IsKeyCache, ReturnType >
clock_type::duration const m_target_age
TaggedCache(std::string const &name, int size, clock_type::duration expiration, clock_type &clock, beast::Journal journal, beast::insight::Collector::ptr const &collector=beast::insight::NullCollector::New())
std::uint64_t m_hits
int const m_target_size
int getTrackSize() const
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition tokens.cpp:138
beast::insight::Gauge size
beast::insight::Hook hook
beast::insight::Gauge hit_rate
Stats(std::string const &prefix, Handler const &handler, beast::insight::Collector::ptr const &collector)