20 #ifndef RIPPLE_BASICS_TAGGEDCACHE_H_INCLUDED
21 #define RIPPLE_BASICS_TAGGEDCACHE_H_INCLUDED
23 #include <ripple/basics/Log.h>
24 #include <ripple/basics/UnorderedContainers.h>
25 #include <ripple/basics/hardened_hash.h>
26 #include <ripple/beast/clock/abstract_clock.h>
27 #include <ripple/beast/insight/Insight.h>
49 class Hash = hardened_hash<>,
171 int cacheRemovals = 0;
197 if (when_expire > (now - minimumAge))
198 when_expire = now - minimumAge;
203 << (now - when_expire).count() <<
" of "
213 if (cit->second.isWeak())
216 if (cit->second.isExpired())
226 else if (cit->second.last_access <= when_expire)
231 if (cit->second.ptr.unique())
240 cit->second.ptr.reset();
253 if (mapRemovals || cacheRemovals)
257 << cacheRemovals <<
", map-=" << mapRemovals;
276 Entry& entry = cit->second;
280 if (entry.isCached())
287 if (!valid || entry.isExpired())
307 template <
bool replace>
325 std::piecewise_construct,
332 Entry& entry = cit->second;
335 if (entry.isCached())
337 if constexpr (replace)
340 entry.weak_ptr = data;
350 auto cachedData = entry.lock();
354 if constexpr (replace)
357 entry.weak_ptr = data;
361 entry.ptr = cachedData;
370 entry.weak_ptr = data;
382 return canonicalize<true>(key, data);
388 return canonicalize<false>(key, data);
405 Entry& entry = cit->second;
408 if (entry.isCached())
414 entry.ptr = entry.lock();
416 if (entry.isCached())
435 auto p = std::make_shared<T>(
std::cref(value));
448 auto entry =
fetch(key);
472 Entry& entry = cit->second;
474 if (!entry.isCached())
477 entry.ptr = entry.lock();
479 if (entry.isCached())
537 hit_rate = (
m_hits * 100) / total;
546 template <
class Handler>
549 Handler
const& handler,
551 :
hook(collector->make_hook(handler))
552 ,
size(collector->make_gauge(prefix,
"size"))
553 ,
hit_rate(collector->make_gauge(prefix,
"hit_rate"))
579 return ptr ==
nullptr;
584 return ptr !=
nullptr;
beast::insight::Hook hook
bool del(const key_type &key, bool valid)
A ledger that has become irrevocable.
Stream trace() const
Severity stream access functions.
beast::abstract_clock< std::chrono::steady_clock > clock_type
std::shared_ptr< mapped_type > lock()
std::shared_ptr< T > fetch(const key_type &key)
std::shared_ptr< mapped_type > ptr
bool canonicalize_replace_cache(const key_type &key, std::shared_ptr< T > const &data)
T max_load_factor(T... args)
virtual time_point now() const =0
Returns the current time.
int getTargetSize() const
std::vector< key_type > getKeys() const
Entry(clock_type::time_point const &last_access_, std::shared_ptr< mapped_type > const &ptr_)
A generic endpoint for log messages.
T forward_as_tuple(T... args)
clock_type::duration getTargetAge() const
clock_type::duration m_target_age
A metric for measuring an integral value.
beast::insight::Gauge size
bool refreshIfPresent(const key_type &key)
Refresh the expiration time on a key.
void setTargetAge(clock_type::duration s)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
void touch(clock_type::time_point const &now)
Stats(std::string const &prefix, Handler const &handler, beast::insight::Collector::ptr const &collector)
void setTargetSize(int s)
bool insert(key_type const &key, T const &value)
Insert the element into the container.
std::weak_ptr< mapped_type > weak_ptr
bool canonicalize(const key_type &key, std::conditional_t< replace, std::shared_ptr< T > const, std::shared_ptr< T >> &data)
Replace aliased objects with originals.
bool canonicalize_replace_client(const key_type &key, std::shared_ptr< T > &data)
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())
clock_type & clock()
Return the clock associated with the cache.
typename std::chrono::steady_clock ::time_point time_point
A reference to a handler for performing polled collection.
clock_type::time_point last_access
static std::shared_ptr< Collector > New()
typename std::chrono::steady_clock ::duration duration
beast::insight::Gauge hit_rate
void set(value_type value) const
Set the value on the gauge.
bool retrieve(const key_type &key, T &data)