Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
util::BlockingCache< ValueType, ErrorType > Class Template Reference

A thread-safe cache that blocks getting operations until the cache is updated. More...

#include <BlockingCache.hpp>

Public Types

enum class  State { NoValue , Updating , HasValue }
 Possible states of the cache.
 
using Updater = std::function<std::expected<ValueType, ErrorType>(boost::asio::yield_context)>
 Function type for cache update operations.
 
using Verifier = std::function<bool(ValueType const&)>
 Function type to verify if a value should be cached.
 

Public Member Functions

 BlockingCache ()=default
 Default constructor - creates an empty cache.
 
 BlockingCache (ValueType initialValue)
 Construct a cache with an initial value.
 
 BlockingCache (BlockingCache &&)=delete
 
 BlockingCache (BlockingCache const &)=delete
 
BlockingCacheoperator= (BlockingCache &&)=delete
 
BlockingCacheoperator= (BlockingCache const &)=delete
 
std::expected< ValueType, ErrorType > asyncGet (boost::asio::yield_context yield, Updater updater, Verifier verifier)
 Asynchronously get a value from the cache, updating if necessary.
 
std::expected< ValueType, ErrorType > update (boost::asio::yield_context yield, Updater updater, Verifier verifier)
 Force an update of the cache value.
 
void invalidate ()
 Invalidates the currently cached value if present.
 
State state () const
 Returns the current state of the cache.
 

Detailed Description

template<typename ValueType, typename ErrorType>
requires (not std::same_as<ValueType, ErrorType>)
class util::BlockingCache< ValueType, ErrorType >

A thread-safe cache that blocks getting operations until the cache is updated.

Template Parameters
ValueTypeThe type of value to be cached
ErrorTypeThe type of error that can occur during updates

Member Typedef Documentation

◆ Updater

template<typename ValueType , typename ErrorType >
using util::BlockingCache< ValueType, ErrorType >::Updater = std::function<std::expected<ValueType, ErrorType>(boost::asio::yield_context)>

Function type for cache update operations.

Called when the cache needs to be populated or refreshed

◆ Verifier

template<typename ValueType , typename ErrorType >
using util::BlockingCache< ValueType, ErrorType >::Verifier = std::function<bool(ValueType const&)>

Function type to verify if a value should be cached.

Returns true if the value should be stored in the cache

Constructor & Destructor Documentation

◆ BlockingCache()

template<typename ValueType , typename ErrorType >
util::BlockingCache< ValueType, ErrorType >::BlockingCache ( ValueType initialValue)
inlineexplicit

Construct a cache with an initial value.

Parameters
initialValueThe value to initialize the cache with

Member Function Documentation

◆ asyncGet()

template<typename ValueType , typename ErrorType >
std::expected< ValueType, ErrorType > util::BlockingCache< ValueType, ErrorType >::asyncGet ( boost::asio::yield_context yield,
Updater updater,
Verifier verifier )
inlinenodiscard

Asynchronously get a value from the cache, updating if necessary.

Parameters
yieldThe asio yield context for coroutine suspension
updaterFunction to generate a new value if needed
verifierFunction to validate whether a value should be cached
Returns
std::expected<ValueType, ErrorType> The cached value or an error

Depending on the current cache state, this will either:

  • Return the cached value if it's already present
  • Wait for an ongoing update to complete
  • Trigger a new update if the cache is empty

◆ invalidate()

template<typename ValueType , typename ErrorType >
void util::BlockingCache< ValueType, ErrorType >::invalidate ( )
inline

Invalidates the currently cached value if present.

Clears the cache and sets its state to Empty. Has no effect if the cache is already empty or being updated.

◆ state()

template<typename ValueType , typename ErrorType >
State util::BlockingCache< ValueType, ErrorType >::state ( ) const
inlinenodiscard

Returns the current state of the cache.

Returns
Current cache state (Empty, Updating, or Full)

◆ update()

template<typename ValueType , typename ErrorType >
std::expected< ValueType, ErrorType > util::BlockingCache< ValueType, ErrorType >::update ( boost::asio::yield_context yield,
Updater updater,
Verifier verifier )
inlinenodiscard

Force an update of the cache value.

Parameters
yieldThe ASIO yield context for coroutine suspension
updaterFunction to generate a new value
verifierFunction to validate whether a value should be cached
Returns
std::expected<ValueType, ErrorType> The new value or an error

Initiates a cache update operation regardless of current state. If another update is already in progress, waits for it to complete.


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