Clio develop
The XRP Ledger API server.
|
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, Error > | getOrUpdate (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. | |
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.
util::ResponseExpirationCache::ResponseExpirationCache | ( | std::chrono::steady_clock::duration | cacheTimeout, |
std::unordered_set< std::string > const & | cmds ) |
Construct a new ResponseExpirationCache object.
cacheTimeout | The time period after which cached entries expire |
cmds | The commands that should be cached (requests for other commands won't be cached) |
|
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.
yield | Asio yield context for coroutine suspension |
cmd | The command to get the response for |
updater | Function to generate the response if not in cache or expired |
verifier | Function to validate if a response should be cached |
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.
bool util::ResponseExpirationCache::shouldCache | ( | std::string const & | cmd | ) |
Check if the given command should be cached.
cmd | The command to check |