mirror of
https://github.com/XRPLF/rippled.git
synced 2026-06-03 08:46:46 +00:00
Co-authored-by: Bart <11445373+bthomee@users.noreply.github.com> Co-authored-by: Bart <bthomee@users.noreply.github.com>
78 lines
2.0 KiB
C++
78 lines
2.0 KiB
C++
#include <xrpl/ledger/CachedView.h>
|
|
|
|
#include <xrpl/basics/CountedObject.h>
|
|
#include <xrpl/basics/TaggedCache.ipp> // IWYU pragma: keep
|
|
#include <xrpl/basics/base_uint.h>
|
|
#include <xrpl/beast/utility/instrumentation.h>
|
|
#include <xrpl/protocol/Keylet.h>
|
|
#include <xrpl/protocol/STLedgerEntry.h>
|
|
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <optional>
|
|
|
|
namespace xrpl::detail {
|
|
|
|
bool
|
|
CachedViewImpl::exists(Keylet const& k) const
|
|
{
|
|
return read(k) != nullptr;
|
|
}
|
|
|
|
std::shared_ptr<SLE const>
|
|
CachedViewImpl::read(Keylet const& k) const
|
|
{
|
|
static CountedObjects::Counter hits{"CachedView::hit"};
|
|
static CountedObjects::Counter hitsexpired{"CachedView::hitExpired"};
|
|
static CountedObjects::Counter misses{"CachedView::miss"};
|
|
bool cacheHit = false;
|
|
bool baseRead = false;
|
|
|
|
auto const digest = [&]() -> std::optional<uint256> {
|
|
{
|
|
std::lock_guard const lock(mutex_);
|
|
auto const iter = map_.find(k.key);
|
|
if (iter != map_.end())
|
|
{
|
|
cacheHit = true;
|
|
return iter->second;
|
|
}
|
|
}
|
|
return base_.digest(k.key);
|
|
}();
|
|
if (!digest)
|
|
return nullptr;
|
|
auto sle = cache_.fetch(*digest, [&]() {
|
|
baseRead = true;
|
|
return base_.read(k);
|
|
});
|
|
// If the sle is null, then a failure must have occurred in base_.read()
|
|
XRPL_ASSERT(sle || baseRead, "xrpl::CachedView::read : null SLE result from base");
|
|
if (cacheHit && baseRead)
|
|
{
|
|
hitsexpired.increment();
|
|
}
|
|
else if (cacheHit)
|
|
{
|
|
hits.increment();
|
|
}
|
|
else
|
|
{
|
|
misses.increment();
|
|
}
|
|
|
|
if (!cacheHit)
|
|
{
|
|
// Avoid acquiring this lock unless necessary. It is only necessary if
|
|
// the key was not found in the map_. The lock is needed to add the key
|
|
// and digest.
|
|
std::lock_guard const lock(mutex_);
|
|
map_.emplace(k.key, *digest);
|
|
}
|
|
if (!sle || !k.check(*sle))
|
|
return nullptr;
|
|
return sle;
|
|
}
|
|
|
|
} // namespace xrpl::detail
|