JKQTPlotter trunk/v5.0.0
an extensive Qt5+Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Loading...
Searching...
No Matches
JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature > Struct Template Reference

this class can be used to implement a general cache for values More...

#include <jkqtpcachingtools.h>

Collaboration diagram for JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >:
[legend]

Public Member Functions

 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)
 
JKQTPDataCacheoperator= (const JKQTPDataCache &)=delete
 
JKQTPDataCacheoperator= (JKQTPDataCache &&)=default
 
int size () const
 

Private Member Functions

void cleanCache_notThreadSafe ()
 clean the cache, so at m_retainFraction*m_maxEntries entries remain.
 
template<typename TSS >
std::enable_if< std::is_same< JKQTPDataCacheThreadSafe, TSS >::value, int >::type size_impl () const
 
template<typename TSS >
std::enable_if< std::is_same< JKQTPDataCacheNotThreadSafe, TSS >::value, int >::type size_impl () const
 

Static Private Member Functions

static int64_t currenTimestamp ()
 generate a timestamp
 

Private Attributes

std::unordered_map< TKey, TDatam_cache
 
std::unordered_map< TKey, std::shared_ptr< std::atomic< int64_t > > > m_cacheLastUseTimestamps
 
const std::function< TData(TKeyInSignature)> m_generateData
 
const int m_maxEntries
 
QReadWriteLock m_mutex
 
const double m_retainFraction
 

Detailed Description

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!

Constructor & Destructor Documentation

◆ JKQTPDataCache() [1/4]

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
template<typename FF >
JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::JKQTPDataCache ( FF  generateData,
int  maxEntries = 10000,
double  retainFraction = 0.8 
)
inline

◆ JKQTPDataCache() [2/4]

◆ JKQTPDataCache() [3/4]

◆ JKQTPDataCache() [4/4]

Member Function Documentation

◆ cleanCache_notThreadSafe()

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
void JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::cleanCache_notThreadSafe ( )
inlineprivate

clean the cache, so at m_retainFraction*m_maxEntries entries remain.

◆ contains() [1/2]

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
template<typename TSS = ThreadSafe>
bool JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::contains ( const typename std::enable_if< std::is_same< JKQTPDataCacheNotThreadSafe, TSS >::value, TKeyInSignature >::type &  key) const
inline

◆ contains() [2/2]

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
template<typename TSS = ThreadSafe>
bool JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::contains ( const typename std::enable_if< std::is_same< JKQTPDataCacheThreadSafe, TSS >::value, TKeyInSignature >::type &  key) const
inline

◆ currenTimestamp()

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
static int64_t JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::currenTimestamp ( )
inlinestaticprivate

generate a timestamp

◆ get() [1/2]

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
template<typename TSS = ThreadSafe>
TData JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::get ( const typename std::enable_if< std::is_same< JKQTPDataCacheNotThreadSafe, TSS >::value, TKeyInSignature >::type &  key)
inline

◆ get() [2/2]

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
template<typename TSS = ThreadSafe>
TData JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::get ( const typename std::enable_if< std::is_same< JKQTPDataCacheThreadSafe, TSS >::value, TKeyInSignature >::type &  key)
inline

◆ get_inline()

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
template<class... Args>
TData JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::get_inline ( Args...  args)
inline

◆ operator=() [1/2]

◆ operator=() [2/2]

◆ size()

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
int JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::size ( ) const
inline

◆ size_impl() [1/2]

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
template<typename TSS >
std::enable_if< std::is_same< JKQTPDataCacheThreadSafe, TSS >::value, int >::type JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::size_impl ( ) const
inlineprivate

◆ size_impl() [2/2]

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
template<typename TSS >
std::enable_if< std::is_same< JKQTPDataCacheNotThreadSafe, TSS >::value, int >::type JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::size_impl ( ) const
inlineprivate

Member Data Documentation

◆ m_cache

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
std::unordered_map<TKey, TData> JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::m_cache
private

◆ m_cacheLastUseTimestamps

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
std::unordered_map<TKey, std::shared_ptr<std::atomic<int64_t> > > JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::m_cacheLastUseTimestamps
private

◆ m_generateData

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
const std::function<TData(TKeyInSignature)> JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::m_generateData
private

◆ m_maxEntries

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
const int JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::m_maxEntries
private

◆ m_mutex

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
QReadWriteLock JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::m_mutex
mutableprivate

◆ m_retainFraction

template<class TData , class TKey , typename ThreadSafe = JKQTPDataCacheThreadSafe, class TKeyInSignature = TKey>
const double JKQTPDataCache< TData, TKey, ThreadSafe, TKeyInSignature >::m_retainFraction
private

The documentation for this struct was generated from the following file: