6#include "Stroika/Foundation/Execution/Common.h"
15 template <
typename KEY,
typename VALUE,
typename TRAITS>
16 TimedCache<KEY, VALUE, TRAITS>::TimedCache (
const Time::Duration& minimumAllowedFreshness)
17 : fMinimumAllowedFreshness_{minimumAllowedFreshness}
18 , fNextAutoClearAt_{Time::
GetTickCount () + minimumAllowedFreshness}
20 Require (fMinimumAllowedFreshness_ > 0.0s);
22 template <
typename KEY,
typename VALUE,
typename TRAITS>
25 shared_lock
critSec{fAssertExternallySyncrhonized_};
28 template <
typename KEY,
typename VALUE,
typename TRAITS>
32 lock_guard
critSec{fAssertExternallySyncrhonized_};
38 template <
typename KEY,
typename VALUE,
typename TRAITS>
42 r.reserve (fMap_.size ());
44 for (
const auto& i : fMap_) {
46 r.push_back (CacheElement{i.first, i.second.fResult, i.second.fLastRefreshedAt});
51 template <
typename KEY,
typename VALUE,
typename TRAITS>
54 shared_lock
critSec{fAssertExternallySyncrhonized_};
55 typename MyMapType_::const_iterator i = fMap_.find (
key);
57 if (i == fMap_.end ()) {
58 fStats_.IncrementMisses ();
72 fStats_.IncrementMisses ();
75 fStats_.IncrementHits ();
79 return i->second.fResult;
82 template <
typename KEY,
typename VALUE,
typename TRAITS>
85 lock_guard
critSec{fAssertExternallySyncrhonized_};
86 typename MyMapType_::iterator i = fMap_.find (
key);
88 if (i == fMap_.end ()) {
89 fStats_.IncrementMisses ();
103 fStats_.IncrementMisses ();
107 i->second.fLastRefreshedAt = Time::GetTickCount ();
109 fStats_.IncrementHits ();
110 return i->second.fResult;
113 template <
typename KEY,
typename VALUE,
typename TRAITS>
128 template <
typename KEY,
typename VALUE,
typename TRAITS>
132 lock_guard
critSec{fAssertExternallySyncrhonized_};
133 if (
prgeSpoiledData == PurgeSpoiledDataFlagType::eAutomaticallyPurgeSpoiledData) {
136 typename MyMapType_::iterator i = fMap_.find (
key);
137 if (i == fMap_.end ()) {
138 fMap_.insert ({
key, MyResult_{result}});
141 i->second = MyResult_{result};
144 template <
typename KEY,
typename VALUE,
typename TRAITS>
148 lock_guard
critSec{fAssertExternallySyncrhonized_};
149 fMap_.insert ({key, MyResult_{result, freshAsOf}});
151 template <
typename KEY,
typename VALUE,
typename TRAITS>
152 inline void TimedCache<KEY, VALUE, TRAITS>::Remove (
typename Common::ArgByValueType<KEY> key)
154 lock_guard critSec{fAssertExternallySyncrhonized_};
157 template <
typename KEY,
typename VALUE,
typename TRAITS>
160 lock_guard
critSec{fAssertExternallySyncrhonized_};
163 template <
typename KEY,
typename VALUE,
typename TRAITS>
166 lock_guard
critSec{fAssertExternallySyncrhonized_};
169 template <
typename KEY,
typename VALUE,
typename TRAITS>
172 if (fNextAutoClearAt_ < Time::GetTickCount ()) {
176 template <
typename KEY,
typename VALUE,
typename TRAITS>
177 void TimedCache<KEY, VALUE, TRAITS>::ClearOld_ ()
180 fNextAutoClearAt_ = now + fMinimumAllowedFreshness_ * 0.75;
182 for (
typename MyMapType_::iterator i = fMap_.begin (); i != fMap_.end ();) {
183 if (i->second.fLastRefreshedAt < lastAccessThreshold) {
time_point< RealtimeClock, DurationSeconds > TimePointSeconds
TimePointSeconds is a simpler approach to chrono::time_point, which doesn't require using templates e...
TimePointSeconds GetTickCount() noexcept
get the current (monotonically increasing) time - from RealtimeClock
LRUCache implements a simple least-recently-used caching strategy, with optional hashing (of keys) to...
Keep track of a bunch of objects, each with an associated 'freshness' which meet a TimedCache-associa...
Duration is a chrono::duration<double> (=.
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
conditional_t<(sizeof(CHECK_T)<=2 *sizeof(void *)) and is_trivially_copyable_v< CHECK_T >, CHECK_T, const CHECK_T & > ArgByValueType
This is an alias for 'T' - but how we want to pass it on stack as formal parameter.