1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#ifndef EXPIRINGCACHE_H
#define EXPIRINGCACHE_H
#include <QCache>
#include <QDateTime>
#include <QTimer>
#include <QDebug>
template <class K, class V>
class ExpiringCache : public QCache<K, V>
{
public:
ExpiringCache(int maxCost = 100, int expireMs = 1000);
~ExpiringCache();
bool insert(const K& key, V* object, int cost = 1);
bool contains(const K& key) const;
V* object(const K& key, bool noExpireCheck = false) const;
V* operator[](const K& key) const;
V* take(const K& key);
QList<K> keys() const;
bool remove(const K& key);
int count() const;
void clear();
bool isEmpty() const;
void setExpireTime(int ms);
private:
bool expired(const K& key) const;
mutable QHash<K, qint64> expires;
int expireMs;
};
template <class K, class V>
ExpiringCache<K, V>::ExpiringCache(int maxCost, int expireMs) :
QCache<K, V>(maxCost), expireMs(expireMs)
{
}
template <class K, class V>
ExpiringCache<K, V>::~ExpiringCache()
{
}
template <class K, class V>
bool ExpiringCache<K, V>::insert(const K& key, V* object, int cost)
{
QList<K> keysBefore = QCache<K, V>::keys();
bool result = QCache<K, V>::insert(key, object, cost);
if (!result)
return false;
QList<K> keysAfter = QCache<K, V>::keys();
for (const K& keyBefore : keysBefore)
{
if (!keysAfter.contains(keyBefore))
expires.remove(keyBefore);
}
expires[key] = QDateTime::currentMSecsSinceEpoch() + expireMs;
return true;
}
template <class K, class V>
bool ExpiringCache<K, V>::contains(const K& key) const
{
if (expired(key))
return false;
return QCache<K, V>::contains(key);
}
template <class K, class V>
V* ExpiringCache<K, V>::object(const K& key, bool noExpireCheck) const
{
if (!noExpireCheck && expired(key))
return nullptr;
return QCache<K, V>::object(key);
}
template <class K, class V>
V* ExpiringCache<K, V>::operator[](const K& key) const
{
if (expired(key))
return nullptr;
return QCache<K, V>::operator[](key);
}
template <class K, class V>
V* ExpiringCache<K, V>::take(const K& key)
{
if (expired(key))
return nullptr;
expires.remove(key);
return QCache<K, V>::take(key);
}
template <class K, class V>
QList<K> ExpiringCache<K, V>::keys() const
{
QList<K> keyList;
for (const K& key : QCache<K, V>::keys())
{
if (!expired(key))
keyList << key;
}
return keyList;
}
template <class K, class V>
bool ExpiringCache<K, V>::remove(const K& key)
{
expires.remove(key);
return QCache<K, V>::remove(key);
}
template <class K, class V>
int ExpiringCache<K, V>::count() const
{
return keys().count();
}
template <class K, class V>
void ExpiringCache<K, V>::clear()
{
expires.clear();
QCache<K, V>::clear();
}
template <class K, class V>
bool ExpiringCache<K, V>::isEmpty() const
{
return keys().isEmpty();
}
template <class K, class V>
void ExpiringCache<K, V>::setExpireTime(int ms)
{
expireMs = ms;
}
template <class K, class V>
bool ExpiringCache<K, V>::expired(const K& key) const
{
if (expires.contains(key) && QDateTime::currentMSecsSinceEpoch() > expires[key])
{
expires.remove(key);
return true;
}
return false;
}
#endif // EXPIRINGCACHE_H
|