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
|
this class can be used to implement a general cache for values More...
#include <jkqtpcachingtools.h>
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, TData > | m_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 |
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!
|
inline |
|
delete |
|
delete |
|
default |
|
inlineprivate |
clean the cache, so at m_retainFraction*m_maxEntries entries remain.
|
inline |
|
inline |
|
inlinestaticprivate |
generate a timestamp
|
inline |
|
inline |
|
inline |
|
delete |
|
default |
|
inline |
|
inlineprivate |
|
inlineprivate |
|
private |
|
private |
|
private |
|
private |
|
mutableprivate |
|
private |