Template Class DataCache#

Inheritance Relationships#

Base Type#

Class Documentation#

template<class T>
class DataCache : public Karana::Core::LockingBase#

A cache for managing a value of type T with a not healthy flag mechanism.

The DataCache class provides the following features:

  • Caches a value of type T.

  • Marks the value as not healthy when certain conditions are met.

  • Provides a callback mechanism to refresh the cached value.

  • Supports registering dependent “child” caches that are marked not healthy when the parent cache becomes not healthy.

Template Parameters:

T – The type of the cached value.

Public Functions

inline DataCache(std::string_view name, const std::function<void(T&)> &callback, std::optional<std::string> type_string = std::nullopt)#

Constructs a DataCache object.

Parameters:
  • name – The name of the cache.

  • callback – A callback function to refresh the cached value when it becomes not healthy.

  • type_string – An optional string representation of the type T.

inline const T &get()#

Retrieves the cached value.

If the cached value is healthy, it returns the current value. If the value is not healthy, the callback function is called to refresh the value, and the refreshed value is returned.

Returns:

A const reference to the cached value.

inline void setCallback(const std::function<void(T&)> &callback)#

Set the callback method for the class.

This method is handy for deferred setting of the callback method when the method is not available (or perhaps its closures are not available) at the time the cache is constructed.

Parameters:

callback – The callback to set as this DataCache’s callback.

inline void updateDataBuffer(const std::function<void(T&)> &callback)#

Invoke the callback on the cached value to set it up.

This method is handy for cases when the cached value needs to modified in some way, such as to resize a vector or matrix in the beginning.

Parameters:

callback – The callback to use to update the data buffer.

inline const T &getRaw() const#

Retrieves the raw cached value - without triggering updates.

This method directly returns the cached value without triggering updates (such as by the get() method when the cache value happens to be not healthy).

Returns:

A const reference to the cached value.

inline virtual std::string_view typeString() const noexcept override#

Returns the type string of LockingBase.

This version returns a non-const version. This is needed to pre-set additional data in the these cached buffers (e.g., list of onodes in the pnode buffer) that will be needed at run-time by the cache

Returns:

The type string.