#include <CallerStalenessCache.h>
Public Member Functions | |
nonvirtual void | Clear () |
nonvirtual void | Add (Common::ArgByValueType< VALUE > v) |
nonvirtual optional< VALUE > | Lookup (TimeStampType staleIfOlderThan) const |
nonvirtual VALUE | LookupValue (TimeStampType staleIfOlderThan, const function< VALUE()> &cacheFiller) |
nonvirtual void | clear () |
Static Public Member Functions | |
static TimeStampType | Ago (TimeStampDifferenceType backThisTime) |
The idea behind this cache is to track when something is added, and that the lookup function can avoid a costly call to compute something if its been recently enough added.
For example, consider a system where memory is stored across a slow bus, and several components need to read data from across that bus. But the different components have different tolerance for staleness (e.g. PID loop needs fresh temperature sensor data but GUI can use more stale data).
This CallerStalenessCache will store when the value is updated, and let the caller either return the value from cache, or fetch it and update the cache if needed.
This differs from other forms of caches in that: o It records the timestamp when a value is last-updated o It doesn't EXPIRE the data ever (except by explicit Clear or ClearOlderThan call) o The lookup caller specifies its tolerance for data staleness, and refreshes the data as needed.
Because of this, when you use either of these caches with a KEY=void (essentially to cache a single thing) they become indistinguishable.
N.B. the KEY=void functionality is NYI for TimedCache, so best to use CallerStalenessCache for that, at least for now.
Definition at line 134 of file CallerStalenessCache.h.
|
static |
Return the timestamp backwards the given timestamp.
Definition at line 29 of file CallerStalenessCache.inl.
void Stroika::Foundation::Cache::CallerStalenessCache< KEY, VALUE, TIME_TRAITS >::Clear | ( | ) |
Clear () – clear all Clear (KEY k) - clear just that key
Definition at line 54 of file CallerStalenessCache.inl.
void Stroika::Foundation::Cache::CallerStalenessCache< KEY, VALUE, TIME_TRAITS >::Add | ( | Common::ArgByValueType< VALUE > | v | ) |
This not only adds the association of KEY k to VALUE v, but updates the timestamp associated with k.
Definition at line 71 of file CallerStalenessCache.inl.
optional< VALUE > Stroika::Foundation::Cache::CallerStalenessCache< KEY, VALUE, TIME_TRAITS >::Lookup | ( | TimeStampType | staleIfOlderThan | ) | const |
Usually one will use this as (cache fillter overload): VALUE v = cache.Lookup (key, ts, [this] () -> VALUE {return this->realLookup(key); });
However, the overload returning an optional is occasionally useful, if you don't want to fill the cache but just see if a value is present.
Both the overload with cacheFiller, and defaultValue will update the 'time stored' for the argument key.
Definition at line 84 of file CallerStalenessCache.inl.
VALUE Stroika::Foundation::Cache::CallerStalenessCache< KEY, VALUE, TIME_TRAITS >::LookupValue | ( | TimeStampType | staleIfOlderThan, |
const function< VALUE()> & | cacheFiller | ||
) |
Lookup the value associated with the given key (or key omitted of KEY type is void) and always return it. In case it was stale (or missing) return the provided defaule value (or cacheFiller computed value).
Definition at line 105 of file CallerStalenessCache.inl.
void Stroika::Foundation::Cache::CallerStalenessCache< KEY, VALUE, TIME_TRAITS >::clear | ( | ) |
Definition at line 140 of file CallerStalenessCache.inl.