3#include <xrpl/basics/SharedWeakCachePointer.h>
4#include <xrpl/basics/SharedWeakCachePointer.ipp>
5#include <xrpl/basics/UnorderedContainers.h>
6#include <xrpl/basics/hardened_hash.h>
7#include <xrpl/beast/clock/abstract_clock.h>
8#include <xrpl/beast/insight/Collector.h>
9#include <xrpl/beast/insight/Gauge.h>
10#include <xrpl/beast/insight/Hook.h>
11#include <xrpl/beast/insight/NullCollector.h>
12#include <xrpl/beast/utility/Journal.h>
40struct ReplaceDynamically;
60 bool IsKeyCache =
false,
62 class SharedPointer = std::shared_ptr<T>,
65 class Mutex = std::recursive_mutex>
117 template <
class KeyComparable>
133 template <
typename Policy>
148 template <
class Policy,
class Callback = std::
nullptr_t>
154 Callback&& replaceCallback =
nullptr);
177 template <
class Callback>
224 template <
class ReturnType =
bool>
227 requires(!IsKeyCache);
229 template <
class ReturnType =
bool>
261 template <
class Handler>
276 template <
class Handler>
279 Handler
const& handler,
281 :
hook(collector->makeHook(handler))
282 ,
size(collector->makeGauge(prefix,
"size"))
283 ,
hitRate(collector->makeGauge(prefix,
"hit_rate"))
333 return ptr &&
ptr.isStrong();
338 return ptr.expired();
Abstract interface to a clock.
std::chrono::steady_clock::duration duration
std::chrono::steady_clock::time_point time_point
A generic endpoint for log messages.
std::shared_ptr< Collector > Ptr
A metric for measuring an integral value.
A reference to a handler for performing polled collection.
static std::shared_ptr< Collector > make()
Seed functor once per construction.
std::unordered_map< key_type, mapped_type, hasher, key_equal, allocator_type > MapType
A combination of a std::shared_ptr and a std::weak_pointer.
ClockType::time_point lastAccess
void touch(ClockType::time_point const &now)
KeyOnlyEntry(ClockType::time_point const &lastAccess)
SharedWeakComboPointerType ptr
void touch(ClockType::time_point const &now)
ValueEntry(ClockType::time_point const &lastAccess, SharedPointerType const &ptr)
ClockType::time_point lastAccess
SharedPointerType fetch(key_type const &key)
std::thread sweepHelper(ClockType::time_point const &whenExpire, ClockType::time_point const &now, KeyValueCacheType::MapType &partition, SweptPointersVector &stuffToSweep, std::atomic< int > &allRemovals, std::scoped_lock< std::recursive_mutex > const &)
bool canonicalizeReplaceClient(key_type const &key, SharedPointerType &data)
Insert the canonical entry for key, keeping any existing cached value.
SharedPointerType initialFetch(key_type const &key, std::scoped_lock< MutexType > const &l)
bool canonicalize(key_type const &key, SharedPointerType &data, Callback &&replaceCallback)
Replace aliased objects with originals.
bool canonicalizeImpl(key_type const &key, CanonicalizeClientPointerType< Policy > data, Policy policy, Callback &&replaceCallback=nullptr)
Shared implementation of the canonicalize family.
beast::AbstractClock< std::chrono::steady_clock > ClockType
bool retrieve(key_type const &key, T &data)
bool touchIfExists(KeyComparable const &key)
Refresh the last access time on a key if present.
bool del(key_type const &key, bool valid)
HardenedPartitionedHashMap< key_type, Entry, HardenedHash<>, std::equal_to< UInt256 > > CacheType
std::conditional_t< IsKeyCache, KeyOnlyEntry, ValueEntry > Entry
std::recursive_mutex MutexType
double rate() const
Returns the fraction of cache hits.
std::thread sweepHelper(ClockType::time_point const &whenExpire, ClockType::time_point const &now, KeyOnlyCacheType::MapType &partition, SweptPointersVector &, std::atomic< int > &allRemovals, std::scoped_lock< std::recursive_mutex > const &)
auto insert(key_type const &key, T const &value) -> ReturnType requires(!IsKeyCache)
Insert the element into the container.
std::vector< key_type > getKeys() const
std::shared_ptr< int > SharedPointerType
HardenedPartitionedHashMap< key_type, KeyOnlyEntry, HardenedHash<>, std::equal_to< UInt256 > > KeyOnlyCacheType
HardenedPartitionedHashMap< key_type, ValueEntry, HardenedHash<>, std::equal_to< UInt256 > > KeyValueCacheType
TaggedCache(std::string const &name, int size, ClockType::duration expiration, ClockType &clock, beast::Journal journal, beast::insight::Collector::Ptr const &collector=beast::insight::NullCollector::make())
auto insert(key_type const &key) -> ReturnType
bool canonicalizeReplaceCache(key_type const &key, SharedPointerType const &data)
Insert/update the canonical entry for key, always replacing the cached value with data.
SharedWeakCachePointer< int > SharedWeakComboPointerType
std::conditional_t< std::is_same_v< detail::ReplaceCached, Policy >, SharedPointerType const &, SharedPointerType & > CanonicalizeClientPointerType
ClockType::duration const targetAge_
SharedPointerType fetch(key_type const &digest, Handler const &h)
Fetch an item from the cache.
std::vector< SharedWeakCachePointer< int > > SweptPointersVector
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.
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
PartitionedUnorderedMap< Key, Value, Hash, Pred, Allocator > HardenedPartitionedHashMap
beast::insight::Gauge size
beast::insight::Gauge hitRate
beast::insight::Hook hook
Stats(std::string const &prefix, Handler const &handler, beast::insight::Collector::Ptr const &collector)