|
| JKQTPDataCache ()=delete |
| JKQTPDataCache (const JKQTPDataCache &)=delete |
template<typename FF> |
| JKQTPDataCache (FF generateData, int maxEntries=10000, double retainFraction=0.8) |
| JKQTPDataCache (JKQTPDataCache &&)=default |
template<typename TSS = ThreadSafe> |
bool | contains (const typename std::enable_if< std::is_same< JKQTPDataCacheNotThreadSafe, TSS >::value, TKeyInSignature >::type &key) const |
template<typename TSS = ThreadSafe> |
bool | contains (const typename std::enable_if< std::is_same< JKQTPDataCacheThreadSafe, TSS >::value, TKeyInSignature >::type &key) const |
template<typename TSS = ThreadSafe> |
TData | get (const typename std::enable_if< std::is_same< JKQTPDataCacheNotThreadSafe, TSS >::value, TKeyInSignature >::type &key) |
template<typename TSS = ThreadSafe> |
TData | get (const typename std::enable_if< std::is_same< JKQTPDataCacheThreadSafe, TSS >::value, TKeyInSignature >::type &key) |
template<class... Args> |
TData | get_inline (Args... args) |
JKQTPDataCache & | operator= (const JKQTPDataCache &)=delete |
JKQTPDataCache & | operator= (JKQTPDataCache &&)=default |
int | size () const |
template<class TData, class TKey, typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
struct JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >
this class can be used to implement a general cache for values
It is typically used to generate a static (ThreadSafe=true) of thread_local (ThreadSafe=false) cache inside a function.
The class is parametrized by a key (TKeay) and value (TData) data type and receives (in the constructor) a functor that calculates the data for a key. An additional template parameter ThreadSafe indicates (true|false) whether the class is implemented in a thread-safe way (for static instances) or not (then it should be used as thread_local instances in a multi-threaded environment or in a single-thread environment).
The class member function get(key) returns a value for a given key, which is either taken from the internal cache, or generated using the functor provided to the constructor. In the latter case the generated value is stored in the internal cache.
Internally the cache maps TKey to TData, but the signature of the get()-function and the generator functor actually uses TKeyInSignature, which may differ from TKey. The only limitation is that TKeyInSignature can be converted/assigned to a TKey
The cache has a maximmum size m_maxEntries. When you try to add a new object, after which the size would grow beyond this, a fraction 1-m_retainFraction of elements are deleted from the cache. The delete strategy is least-recently used (LRU). In order to immplement this, the cache keeps track of the last use timestamp of each entry.
You can deactivate the cleaning by setting m_maxEntries<0, but the the cache may grow indefinitely and there is possibly undefined behaviour when add one too many items!