20 #ifndef RIPPLE_BASICS_KEYCACHE_H_INCLUDED
21 #define RIPPLE_BASICS_KEYCACHE_H_INCLUDED
23 #include <ripple/basics/UnorderedContainers.h>
24 #include <ripple/basics/hardened_hash.h>
25 #include <ripple/beast/clock/abstract_clock.h>
26 #include <ripple/beast/insight/Insight.h>
39 class Hash = hardened_hash<>,
52 template <
class Handler>
55 Handler
const& handler,
57 :
hook(collector->make_hook(handler))
58 ,
size(collector->make_gauge(prefix,
"size"))
59 ,
hit_rate(collector->make_gauge(prefix,
"hit_rate"))
193 template <
class KeyComparable>
198 typename map_type::const_iterator
const iter(
m_map.
find(key));
218 std::piecewise_construct,
223 it->second.last_access = now;
232 template <
class KeyComparable>
283 if (when_expire > (now - minimumAge))
284 when_expire = now - minimumAge;
291 if (it->second.last_access > now)
293 it->second.last_access = now;
296 else if (it->second.last_access <= when_expire)
typename map_type::iterator iterator
size_type size() const
Returns the number of items in the container.
bool erase(key_type const &key)
Remove the specified cache entry.
Stats(std::string const &prefix, Handler const &handler, beast::insight::Collector::ptr const &collector)
beast::insight::Gauge size
virtual time_point now() const =0
Returns the current time.
bool touch_if_exists(KeyComparable const &key)
Refresh the last access time on a key if present.
Integers of any length that is a multiple of 32-bits.
KeyCache(std::string const &name, clock_type &clock, beast::insight::Collector::ptr const &collector, size_type target_size=0, std::chrono::seconds expiration=std::chrono::minutes{2})
Construct with the specified name.
bool insert(Key const &key)
Insert the specified key.
beast::abstract_clock< std::chrono::steady_clock > clock_type
beast::insight::Gauge hit_rate
bool exists(KeyComparable const &key) const
Returns true if the key was found.
clock_type::duration m_target_age
T forward_as_tuple(T... args)
void sweep()
Remove stale entries from the cache.
Entry(clock_type::time_point const &last_access_)
A metric for measuring an integral value.
KeyCache(std::string const &name, clock_type &clock, size_type target_size=0, std::chrono::seconds expiration=std::chrono::minutes{2})
void clear()
Empty the cache.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
clock_type::time_point last_access
void setTargetSize(size_type s)
typename map_type::size_type size_type
void setTargetAge(std::chrono::seconds s)
std::string const & name() const
Retrieve the name of this object.
clock_type & clock()
Return the clock associated with the cache.
Maintains a cache of keys with no associated data.
typename std::chrono::steady_clock ::time_point time_point
A reference to a handler for performing polled collection.
static std::shared_ptr< Collector > New()
typename std::chrono::steady_clock ::duration duration
beast::insight::Hook hook
void set(value_type value) const
Set the value on the gauge.