Keep track of a bunch of objects, each with an associated 'freshness' which meet a TimedCache-associated minimal reshness limit. More...
#include <TimedCache.h>
Public Member Functions | |
nonvirtual Time::Duration | GetMinimumAllowedFreshness () const |
nonvirtual void | SetMinimumAllowedFreshness (Time::Duration minimumAllowedFreshness) |
nonvirtual Traversal::Iterable< CacheElement > | Elements () const |
nonvirtual optional< VALUE > | Lookup (typename Common::ArgByValueType< KEY > key, Time::TimePointSeconds *lastRefreshedAt=nullptr) const |
Returns the value associated with argument 'key', or nullopt, if its missing (missing same as expired). Can be used to retrieve lastRefreshedAt. | |
nonvirtual optional< VALUE > | Lookup (typename Common::ArgByValueType< KEY > key, LookupMarksDataAsRefreshed successfulLookupRefreshesAcceesFlag) |
nonvirtual VALUE | LookupValue (typename Common::ArgByValueType< KEY > key, const function< VALUE(typename Common::ArgByValueType< KEY >)> &cacheFiller, LookupMarksDataAsRefreshed successfulLookupRefreshesAcceesFlag=LookupMarksDataAsRefreshed::eDontTreatFoundThroughLookupAsRefreshed, PurgeSpoiledDataFlagType purgeSpoiledData=PurgeSpoiledDataFlagType::eAutomaticallyPurgeSpoiledData) |
nonvirtual void | Add (typename Common::ArgByValueType< KEY > key, typename Common::ArgByValueType< VALUE > result, PurgeSpoiledDataFlagType purgeSpoiledData=PurgeSpoiledDataFlagType::eAutomaticallyPurgeSpoiledData) |
nonvirtual void | clear () |
nonvirtual void | PurgeSpoiledData () |
Keep track of a bunch of objects, each with an associated 'freshness' which meet a TimedCache-associated minimal reshness limit.
We define 'fresheness' somewhat arbitrarily, but by default, this means since the item was added. However, the TimedCache also provides other apis to update the 'freshness' of a stored object, depending on application needs.
Keeps track of all items - indexed by Key - but throws away items which are any more stale than given by the staleness limit.
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.
For most use cases (when caching something) - the default behavior of only updating the last-access time on Add makes sense. But for the case where this class is used to OWN an object (see shared_ptr example below) - then treating a read asccess as a refresh can be helpful.
Before Stroika 3.0d1, there was a template parameter which allowed treating Lookup () this way. But that has significant downsides (making lookup non-const which has threading implications). Instead now, we have non-const methods you can use todo this instead of Lookup, and then there is no need for special template paremeters, or non-cost Lookup.
In this example, there is a set of files on disk in a folder, which is complex to analyze but once analyzed, lots of calls come in at once to read (and maybe update) the set of files and once nobody has asked for a while, we throw that cache away, and rebuild it as needed.
This example ALSO shows how to wrap a cache object in 'Synchronized' for thread safety.
Definition at line 257 of file TimedCache.h.
Time::Duration Stroika::Foundation::Cache::TimedCache< KEY, VALUE, TRAITS >::GetMinimumAllowedFreshness | ( | ) | const |
When items are added to the timed cache, there is a universal (for the entire cache) minimum allowed freshness (how old item allowed to be before thrown away).
Definition at line 23 of file TimedCache.inl.
void Stroika::Foundation::Cache::TimedCache< KEY, VALUE, TRAITS >::SetMinimumAllowedFreshness | ( | Time::Duration | minimumAllowedFreshness | ) |
Definition at line 29 of file TimedCache.inl.
auto Stroika::Foundation::Cache::TimedCache< KEY, VALUE, TRAITS >::Elements | ( | ) | const |
Definition at line 39 of file TimedCache.inl.
optional< VALUE > Stroika::Foundation::Cache::TimedCache< KEY, VALUE, TRAITS >::Lookup | ( | typename Common::ArgByValueType< KEY > | key, |
Time::TimePointSeconds * | lastRefreshedAt = nullptr |
||
) | const |
Returns the value associated with argument 'key', or nullopt, if its missing (missing same as expired). Can be used to retrieve lastRefreshedAt.
If lastRefreshedAt is provided, it is ignored, except if Lookup returns true, the value pointed to will contain the last time the data was refreshed.
Occasionally, a caller might want to have the ACT of doing a lookup mark the item as fresh, in which case call Lookup (..., eTreatFoundThroughLookupAsRefreshed) instead.
Occasionally, a caller might want to ASSURE it gets data, and just use the cached value if fresh enuf, and specify a lookup lambda to fetch the actual data if its not fresh, in which case call LookupValue ().
Before Stroika 3.0d1, we used to remove the entry from the list (an optimization). But that required Lookup to be non-const (with synchronization in mind probably a pessimization). So instead, count on PurgeUnusedData being called automatically on future adds, explicit user calls to purge unused data.
i = fMap_.erase (i);
Definition at line 52 of file TimedCache.inl.
optional< VALUE > Stroika::Foundation::Cache::TimedCache< KEY, VALUE, TRAITS >::Lookup | ( | typename Common::ArgByValueType< KEY > | key, |
LookupMarksDataAsRefreshed | successfulLookupRefreshesAcceesFlag | ||
) |
Before Stroika 3.0d1, we used to remove the entry from the list (an optimization). But that required Lookup to be non-const (with synchronization in mind probably a pessimization). So instead, count on PurgeUnusedData being called automatically on future adds, explicit user calls to purge unused data.
i = fMap_.erase (i);
Definition at line 83 of file TimedCache.inl.
VALUE Stroika::Foundation::Cache::TimedCache< KEY, VALUE, TRAITS >::LookupValue | ( | typename Common::ArgByValueType< KEY > | key, |
const function< VALUE(typename Common::ArgByValueType< KEY >)> & | cacheFiller, | ||
LookupMarksDataAsRefreshed | successfulLookupRefreshesAcceesFlag = LookupMarksDataAsRefreshed::eDontTreatFoundThroughLookupAsRefreshed , |
||
PurgeSpoiledDataFlagType | purgeSpoiledData = PurgeSpoiledDataFlagType::eAutomaticallyPurgeSpoiledData |
||
) |
Usually one will use this as (cacheFiller overload):
However, the method (Lookup) returing an optional is occasionally useful, if you don't want to fill the cache but just see if a value is present.
The overload with cacheFiller, will update the 'time stored' for the argument key if a new value is fetched.
Definition at line 114 of file TimedCache.inl.
void Stroika::Foundation::Cache::TimedCache< KEY, VALUE, TRAITS >::Add | ( | typename Common::ArgByValueType< KEY > | key, |
typename Common::ArgByValueType< VALUE > | result, | ||
PurgeSpoiledDataFlagType | purgeSpoiledData = PurgeSpoiledDataFlagType::eAutomaticallyPurgeSpoiledData |
||
) |
Updates/adds the given value associated with key, and updates the last-access date to now (or argument freshAsOf).
The parameter PurgeSpoiledData defaults to eAutomaticallyPurgeSpoiledData; this allows the accumulated data to automatically be purged as it becomes irrelevant (
Definition at line 129 of file TimedCache.inl.
void Stroika::Foundation::Cache::TimedCache< KEY, VALUE, TRAITS >::clear | ( | ) |
Remove everything from the cache
Definition at line 158 of file TimedCache.inl.
void Stroika::Foundation::Cache::TimedCache< KEY, VALUE, TRAITS >::PurgeSpoiledData | ( | ) |
May be called occasionally to free resources used by cached items that are out of date. Not necessary to call - but can save memory.
Can be triggered automatically (so not explicitly) by passing eAutomaticallyPurgeSpoiledData to Add ()
Definition at line 164 of file TimedCache.inl.