164
169
* @author prozac <prozac@rottenboy.com>
165
170
* @brief Insert an object with a time-to-live and check later if it still exists
172
template<typename K, typename V = bool>
168
173
class TCacheMap {
170
175
TCacheMap(unsigned int uTTL = 5000) {
186
191
* @param Item the item to add to the cache
187
192
* @param uTTL the time-to-live for this specific item
189
void AddItem(const T& Item, unsigned int uTTL) {
190
if (!uTTL) { // If time-to-live is zero we don't want to waste our time adding it
191
RemItem(Item); // Remove the item incase it already exists
195
m_mItems[Item] = CUtils::GetMillTime() + uTTL;
194
void AddItem(const K& Item, unsigned int uTTL) {
195
if (!uTTL) { // If time-to-live is zero we don't want to waste our time adding it
196
RemItem(Item); // Remove the item incase it already exists
200
m_mItems[Item] = value(CUtils::GetMillTime() + uTTL, V());
205
* @brief This function adds an item to the cache using the default time-to-live value
206
* @param Item the item to add to the cache
207
* @param Val The value associated with the key Item
209
void AddItem(const K& Item, const V& Val) {
210
AddItem(Item, Val, m_uTTL);
214
* @brief This function adds an item to the cache using a custom time-to-live value
215
* @param Item the item to add to the cache
216
* @param Val The value associated with the key Item
217
* @param uTTL the time-to-live for this specific item
219
void AddItem(const K& Item, const V& Val, unsigned int uTTL) {
220
if (!uTTL) { // If time-to-live is zero we don't want to waste our time adding it
221
RemItem(Item); // Remove the item incase it already exists
225
m_mItems[Item] = value(CUtils::GetMillTime() + uTTL, Val);
201
231
* @param Item The item to check for
202
232
* @return true if item exists
204
bool HasItem(const T& Item) {
234
bool HasItem(const K& Item) {
206
236
return (m_mItems.find(Item) != m_mItems.end());
240
* @brief Performs a Cleanup() and returns a pointer to the object, or NULL
241
* @param Item The item to check for
242
* @return Pointer to the item or NULL if there is no suitable one
244
V* GetItem(const K& Item) {
246
iterator it = m_mItems.find(Item);
247
if (it == m_mItems.end())
249
return &it->second.second;
210
253
* @brief Removes a specific item from the cache
211
254
* @param Item The item to be removed
212
255
* @return true if item existed and was removed, false if it never existed
214
bool RemItem(const T& Item) {
257
bool RemItem(const K& Item) {
215
258
return m_mItems.erase(Item);
219
262
* @brief Cycles through the queue removing all of the stale entries
222
typename map<T, unsigned long long>::iterator it = m_mItems.begin();
265
iterator it = m_mItems.begin();
224
267
while (it != m_mItems.end()) {
225
if (CUtils::GetMillTime() > (it->second)) {
268
if (CUtils::GetMillTime() > (it->second.first)) {
226
269
m_mItems.erase(it++);
234
277
void SetTTL(unsigned int u) { m_uTTL = u; }
280
unsigned int GetTTL() { return m_uTTL; }
237
map<T, unsigned long long> m_mItems; //!< Map of cached items. The value portion of the map is for the expire time
283
typedef pair<unsigned long long, V> value;
284
typedef typename map<K, value>::iterator iterator;
285
map<K, value> m_mItems; //!< Map of cached items. The value portion of the map is for the expire time
238
286
unsigned int m_uTTL; //!< Default time-to-live duration