rippled
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
ripple::KeyCache< Key, Hash, KeyEqual, Mutex > Class Template Reference

Maintains a cache of keys with no associated data. More...

Collaboration diagram for ripple::KeyCache< Key, Hash, KeyEqual, Mutex >:
Collaboration graph
[legend]

Classes

struct  Entry
 
struct  Stats
 

Public Types

using key_type = Key
 
using clock_type = beast::abstract_clock< std::chrono::steady_clock >
 
using size_type = typename map_type::size_type
 

Public Member Functions

 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. More...
 
 KeyCache (std::string const &name, clock_type &clock, size_type target_size=0, std::chrono::seconds expiration=std::chrono::minutes{2})
 
std::string const & name () const
 Retrieve the name of this object. More...
 
clock_typeclock ()
 Return the clock associated with the cache. More...
 
size_type size () const
 Returns the number of items in the container. More...
 
void clear ()
 Empty the cache. More...
 
void reset ()
 
void setTargetSize (size_type s)
 
void setTargetAge (std::chrono::seconds s)
 
template<class KeyComparable >
bool exists (KeyComparable const &key) const
 Returns true if the key was found. More...
 
bool insert (Key const &key)
 Insert the specified key. More...
 
template<class KeyComparable >
bool touch_if_exists (KeyComparable const &key)
 Refresh the last access time on a key if present. More...
 
bool erase (key_type const &key)
 Remove the specified cache entry. More...
 
void sweep ()
 Remove stale entries from the cache. More...
 

Private Types

using map_type = hardened_hash_map< key_type, Entry, Hash, KeyEqual >
 
using iterator = typename map_type::iterator
 

Private Member Functions

void collect_metrics ()
 

Private Attributes

Mutex m_mutex
 
map_type m_map
 
Stats m_stats
 
clock_typem_clock
 
const std::string m_name
 
size_type m_target_size
 
clock_type::duration m_target_age
 

Detailed Description

template<class Key, class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
class ripple::KeyCache< Key, Hash, KeyEqual, Mutex >

Maintains a cache of keys with no associated data.

The cache has a target size and an expiration time. When cached items become older than the maximum age they are eligible for removal during a call to sweep.

Definition at line 43 of file KeyCache.h.

Member Typedef Documentation

◆ key_type

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
using ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::key_type = Key

Definition at line 46 of file KeyCache.h.

◆ clock_type

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
using ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::clock_type = beast::abstract_clock<std::chrono::steady_clock>

Definition at line 47 of file KeyCache.h.

◆ map_type

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
using ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::map_type = hardened_hash_map<key_type, Entry, Hash, KeyEqual>
private

Definition at line 83 of file KeyCache.h.

◆ iterator

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
using ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::iterator = typename map_type::iterator
private

Definition at line 84 of file KeyCache.h.

◆ size_type

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
using ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::size_type = typename map_type::size_type

Definition at line 87 of file KeyCache.h.

Constructor & Destructor Documentation

◆ KeyCache() [1/2]

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::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.

Parameters
sizeThe initial target size.
ageThe initial expiration time.

Definition at line 104 of file KeyCache.h.

◆ KeyCache() [2/2]

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::KeyCache ( std::string const &  name,
clock_type clock,
size_type  target_size = 0,
std::chrono::seconds  expiration = std::chrono::minutes{2} 
)

Definition at line 119 of file KeyCache.h.

Member Function Documentation

◆ name()

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
std::string const& ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::name ( ) const

Retrieve the name of this object.

Definition at line 139 of file KeyCache.h.

◆ clock()

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
clock_type& ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::clock ( )

Return the clock associated with the cache.

Definition at line 146 of file KeyCache.h.

◆ size()

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
size_type ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::size ( ) const

Returns the number of items in the container.

Definition at line 153 of file KeyCache.h.

◆ clear()

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
void ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::clear ( )

Empty the cache.

Definition at line 161 of file KeyCache.h.

◆ reset()

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
void ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::reset ( )

Definition at line 168 of file KeyCache.h.

◆ setTargetSize()

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
void ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::setTargetSize ( size_type  s)

Definition at line 177 of file KeyCache.h.

◆ setTargetAge()

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
void ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::setTargetAge ( std::chrono::seconds  s)

Definition at line 184 of file KeyCache.h.

◆ exists()

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
template<class KeyComparable >
bool ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::exists ( KeyComparable const &  key) const

Returns true if the key was found.

Does not update the last access time.

Definition at line 195 of file KeyCache.h.

◆ insert()

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
bool ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::insert ( Key const &  key)

Insert the specified key.

The last access time is refreshed in all cases.

Returns
true If the key was newly inserted.

Definition at line 213 of file KeyCache.h.

◆ touch_if_exists()

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
template<class KeyComparable >
bool ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::touch_if_exists ( KeyComparable const &  key)

Refresh the last access time on a key if present.

Returns
true If the key was found.

Definition at line 234 of file KeyCache.h.

◆ erase()

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
bool ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::erase ( key_type const &  key)

Remove the specified cache entry.

Parameters
keyThe key to remove.
Returns
false If the key was not found.

Definition at line 253 of file KeyCache.h.

◆ sweep()

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
void ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::sweep ( )

Remove stale entries from the cache.

Definition at line 267 of file KeyCache.h.

◆ collect_metrics()

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
void ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::collect_metrics ( )
private

Definition at line 309 of file KeyCache.h.

Member Data Documentation

◆ m_mutex

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
Mutex ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::m_mutex
mutableprivate

Definition at line 90 of file KeyCache.h.

◆ m_map

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
map_type ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::m_map
private

Definition at line 91 of file KeyCache.h.

◆ m_stats

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
Stats ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::m_stats
mutableprivate

Definition at line 92 of file KeyCache.h.

◆ m_clock

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
clock_type& ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::m_clock
private

Definition at line 93 of file KeyCache.h.

◆ m_name

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
const std::string ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::m_name
private

Definition at line 94 of file KeyCache.h.

◆ m_target_size

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
size_type ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::m_target_size
private

Definition at line 95 of file KeyCache.h.

◆ m_target_age

template<class Key , class Hash = hardened_hash<>, class KeyEqual = std::equal_to<Key>, class Mutex = std::mutex>
clock_type::duration ripple::KeyCache< Key, Hash, KeyEqual, Mutex >::m_target_age
private

Definition at line 96 of file KeyCache.h.