24#ifndef JKQTPCACHINGTOOLS_H
25#define JKQTPCACHINGTOOLS_H
27#include "jkqtcommon/jkqtcommon_imexport.h"
28#include "jkqtcommon/jkqtpmathtools.h"
29#include <QReadWriteLock>
31#include <QWriteLocker>
38#include <unordered_map>
73template <
class TData,
class TKey,
typename ThreadSafe=JKQTPDataCacheThreadSafe,
class TKeyInSignature=TKey>
75 template <
typename FF>
76 inline JKQTPDataCache(FF generateData,
int maxEntries=10000,
double retainFraction=0.8):
87 template<
class... Args>
89 return get(TKeyInSignature(args...));
92 template <
typename TSS=ThreadSafe>
93 inline TData
get(
const typename std::enable_if<std::is_same<JKQTPDataCacheThreadSafe, TSS>::value, TKeyInSignature>::type& key) {
94 const TKey cacheKey=key;
113 m_cache.emplace(cacheKey,newData);
117 template <
typename TSS=ThreadSafe>
118 inline TData
get(
const typename std::enable_if<std::is_same<JKQTPDataCacheNotThreadSafe, TSS>::value, TKeyInSignature>::type& key) {
119 const TKey cacheKey=key;
121 auto it=
m_cache.find(cacheKey);
128 m_cache.emplace(cacheKey,newData);
133 template <
typename TSS=ThreadSafe>
134 inline bool contains(
const typename std::enable_if<std::is_same<JKQTPDataCacheThreadSafe, TSS>::value, TKeyInSignature>::type& key)
const {
135 const TKey cacheKey=key;
141 template <
typename TSS=ThreadSafe>
142 inline bool contains(
const typename std::enable_if<std::is_same<JKQTPDataCacheNotThreadSafe, TSS>::value, TKeyInSignature>::type& key)
const {
143 const TKey cacheKey=key;
153 template <
typename TSS>
154 inline typename std::enable_if<std::is_same<JKQTPDataCacheThreadSafe, TSS>::value,
int>::type
size_impl()
const {
159 template <
typename TSS>
160 inline typename std::enable_if<std::is_same<JKQTPDataCacheNotThreadSafe, TSS>::value,
int>::type
size_impl()
const {
166 static auto firstTime=std::chrono::steady_clock::now();
167 return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()-
firstTime).count();
173 std::vector<QPair<TKey,int64_t> >
allItems;
this class can be used to implement a general cache for values
Definition jkqtpcachingtools.h:74
JKQTPDataCache & operator=(const JKQTPDataCache &)=delete
int size() const
Definition jkqtpcachingtools.h:148
JKQTPDataCache(FF generateData, int maxEntries=10000, double retainFraction=0.8)
Definition jkqtpcachingtools.h:76
std::unordered_map< TKey, std::shared_ptr< std::atomic< int64_t > > > m_cacheLastUseTimestamps
Definition jkqtpcachingtools.h:187
const std::function< TData(TKeyInSignature)> m_generateData
Definition jkqtpcachingtools.h:189
QReadWriteLock m_mutex
Definition jkqtpcachingtools.h:188
TData get(const typename std::enable_if< std::is_same< JKQTPDataCacheNotThreadSafe, TSS >::value, TKeyInSignature >::type &key)
Definition jkqtpcachingtools.h:118
const double m_retainFraction
Definition jkqtpcachingtools.h:185
const int m_maxEntries
Definition jkqtpcachingtools.h:184
JKQTPDataCache & operator=(JKQTPDataCache &&)=default
JKQTPDataCache(JKQTPDataCache &&)=default
std::enable_if< std::is_same< JKQTPDataCacheThreadSafe, TSS >::value, int >::type size_impl() const
Definition jkqtpcachingtools.h:154
static int64_t currenTimestamp()
generate a timestamp
Definition jkqtpcachingtools.h:165
bool contains(const typename std::enable_if< std::is_same< JKQTPDataCacheThreadSafe, TSS >::value, TKeyInSignature >::type &key) const
Definition jkqtpcachingtools.h:134
TData get_inline(Args... args)
Definition jkqtpcachingtools.h:88
JKQTPDataCache(const JKQTPDataCache &)=delete
TData get(const typename std::enable_if< std::is_same< JKQTPDataCacheThreadSafe, TSS >::value, TKeyInSignature >::type &key)
Definition jkqtpcachingtools.h:93
void cleanCache_notThreadSafe()
clean the cache, so at m_retainFraction*m_maxEntries entries remain.
Definition jkqtpcachingtools.h:170
std::enable_if< std::is_same< JKQTPDataCacheNotThreadSafe, TSS >::value, int >::type size_impl() const
Definition jkqtpcachingtools.h:160
bool contains(const typename std::enable_if< std::is_same< JKQTPDataCacheNotThreadSafe, TSS >::value, TKeyInSignature >::type &key) const
Definition jkqtpcachingtools.h:142
std::unordered_map< TKey, TData > m_cache
Definition jkqtpcachingtools.h:186