Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
util::ResponseExpirationCache Class Reference

Cache of requests' responses with TTL support and configurable cachable commands. More...

#include <ResponseExpirationCache.hpp>

Classes

struct  EntryData
 A data structure to store a cache entry with its timestamp. More...
 
struct  Error
 A data structure to represent errors that can occur during an update of the cache. More...
 

Public Types

using CacheEntry = util::BlockingCache<EntryData, Error>
 
using Updater = CacheEntry::Updater
 
using Verifier = CacheEntry::Verifier
 

Public Member Functions

 ResponseExpirationCache (std::chrono::steady_clock::duration cacheTimeout, std::unordered_set< std::string > const &cmds)
 Construct a new ResponseExpirationCache object.
 
bool shouldCache (std::string const &cmd)
 Check if the given command should be cached.
 
std::expected< boost::json::object, ErrorgetOrUpdate (boost::asio::yield_context yield, std::string const &cmd, Updater updater, Verifier verifier)
 Get a cached response or update the cache if necessary.
 
void invalidate ()
 Invalidate all entries in the cache.
 

Detailed Description

Cache of requests' responses with TTL support and configurable cachable commands.

This class implements a time-based expiration cache for RPC responses. It allows caching responses for specified commands and automatically invalidates them after a configured timeout period. The cache uses BlockingCache internally to handle concurrent access and updates.

Constructor & Destructor Documentation

◆ ResponseExpirationCache()

util::ResponseExpirationCache::ResponseExpirationCache ( std::chrono::steady_clock::duration cacheTimeout,
std::unordered_set< std::string > const & cmds )

Construct a new ResponseExpirationCache object.

Parameters
cacheTimeoutThe time period after which cached entries expire
cmdsThe commands that should be cached (requests for other commands won't be cached)

Member Function Documentation

◆ getOrUpdate()

std::expected< boost::json::object, ResponseExpirationCache::Error > util::ResponseExpirationCache::getOrUpdate ( boost::asio::yield_context yield,
std::string const & cmd,
Updater updater,
Verifier verifier )
nodiscard

Get a cached response or update the cache if necessary.

This method returns a cached response if it exists and hasn't expired. If the cache entry is expired or doesn't exist, it calls the updater to generate a new value. If multiple coroutines request the same entry simultaneously, only one updater will be called while others wait.

Note
cmd must be one of the commands that are cached. There is an ASSERT() inside the function
Parameters
yieldAsio yield context for coroutine suspension
cmdThe command to get the response for
updaterFunction to generate the response if not in cache or expired
verifierFunction to validate if a response should be cached
Returns
The cached or newly generated response, or an error

◆ invalidate()

void util::ResponseExpirationCache::invalidate ( )

Invalidate all entries in the cache.

This causes all cached entries to be cleared, forcing the next access to generate new responses.

◆ shouldCache()

bool util::ResponseExpirationCache::shouldCache ( std::string const & cmd)

Check if the given command should be cached.

Parameters
cmdThe command to check
Returns
true if the command should be cached, false otherwise

The documentation for this class was generated from the following files: