2018-05-30 15:19:13 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
* libretroshare/src/util: rsmemcache.h *
|
|
|
|
* *
|
|
|
|
* libretroshare: retroshare core library *
|
|
|
|
* *
|
|
|
|
* Copyright 2012-2012 by Robert Fernie <retroshare@lunamutt.com> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2012-11-03 09:15:21 -04:00
|
|
|
#ifndef RS_UTIL_MEM_CACHE
|
|
|
|
#define RS_UTIL_MEM_CACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
|
|
|
|
#include <map>
|
2018-10-06 19:34:05 -04:00
|
|
|
#include "util/rstime.h"
|
2012-11-03 09:15:21 -04:00
|
|
|
#include <iostream>
|
|
|
|
#include <inttypes.h>
|
2012-11-29 17:48:28 -05:00
|
|
|
#include <string>
|
2012-11-03 08:41:11 -04:00
|
|
|
|
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
/************************************************************************************/
|
|
|
|
|
|
|
|
/* Generic Memoory Cache
|
|
|
|
*
|
|
|
|
* This is probably crude and crap to start with.
|
|
|
|
* Want Least Recently Used (LRU) discard policy, without having to search whole cache.
|
|
|
|
* Use two maps:
|
|
|
|
* - mDataMap[key] => data.
|
|
|
|
* - mLruMap[AccessTS] => key (multimap)
|
|
|
|
*/
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
/***
|
|
|
|
* #define DEBUG_RSMEMCACHE 1
|
|
|
|
***/
|
|
|
|
|
2012-11-03 10:07:26 -04:00
|
|
|
#define DEFAULT_MEM_CACHE_SIZE 100
|
2012-11-03 08:41:11 -04:00
|
|
|
|
|
|
|
template<class Key, class Value> class RsMemCache
|
|
|
|
{
|
2016-05-29 16:09:56 -04:00
|
|
|
public:
|
2012-11-03 10:07:26 -04:00
|
|
|
|
2012-11-29 17:48:28 -05:00
|
|
|
RsMemCache(uint32_t max_size = DEFAULT_MEM_CACHE_SIZE, std::string name = "UnknownMemCache")
|
2016-05-29 16:09:56 -04:00
|
|
|
:mDataCount(0), mMaxSize(max_size), mName(name)
|
2012-11-29 17:48:28 -05:00
|
|
|
{
|
|
|
|
clearStats();
|
|
|
|
return;
|
|
|
|
}
|
2012-11-03 08:41:11 -04:00
|
|
|
|
|
|
|
bool is_cached(const Key &key) const;
|
|
|
|
bool fetch(const Key &key, Value &data);
|
2020-05-07 11:31:58 -04:00
|
|
|
|
|
|
|
// Like map[] installs empty one if non-existent.
|
|
|
|
|
|
|
|
Value& ref(const Key &key);
|
|
|
|
Value& operator[](const Key& key) { return ref(key); }
|
|
|
|
|
2012-11-03 08:41:11 -04:00
|
|
|
bool store(const Key &key, const Value &data);
|
2013-10-20 05:43:30 -04:00
|
|
|
bool erase(const Key &key); // clean up cache.
|
2012-11-03 08:41:11 -04:00
|
|
|
|
2012-11-03 10:07:26 -04:00
|
|
|
bool resize(); // should be called periodically to cleanup old entries.
|
|
|
|
|
2016-05-29 16:09:56 -04:00
|
|
|
// Apply a method of a given class ClientClass to all cached data. Can be useful...
|
|
|
|
|
|
|
|
template<class ClientClass> bool applyToAllCachedEntries(ClientClass& c,bool (ClientClass::*method)(Value&));
|
|
|
|
|
2020-05-07 11:31:58 -04:00
|
|
|
uint32_t size() const { return mDataMap.size() ; }
|
|
|
|
void printStats(std::ostream& out);
|
2016-05-29 16:09:56 -04:00
|
|
|
private:
|
2012-11-03 08:41:11 -04:00
|
|
|
|
2018-10-06 19:34:05 -04:00
|
|
|
bool update_lrumap(const Key &key, rstime_t old_ts, rstime_t new_ts);
|
2012-11-03 08:41:11 -04:00
|
|
|
bool discard_LRU(int count_to_clear);
|
|
|
|
|
|
|
|
// internal class.
|
|
|
|
class cache_data
|
|
|
|
{
|
2016-05-29 16:09:56 -04:00
|
|
|
public:
|
2012-11-03 08:41:11 -04:00
|
|
|
cache_data() { return; }
|
2018-10-06 19:34:05 -04:00
|
|
|
cache_data(Key in_key, Value in_data, rstime_t in_ts)
|
2016-05-29 16:09:56 -04:00
|
|
|
:key(in_key), data(in_data), ts(in_ts) { return; }
|
2012-11-03 08:41:11 -04:00
|
|
|
Key key;
|
|
|
|
Value data;
|
2018-10-06 19:34:05 -04:00
|
|
|
rstime_t ts;
|
2012-11-03 08:41:11 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-05-29 16:09:56 -04:00
|
|
|
std::map<Key, cache_data > mDataMap;
|
2018-10-06 19:34:05 -04:00
|
|
|
std::multimap<rstime_t, Key> mLruMap;
|
2016-05-29 16:09:56 -04:00
|
|
|
uint32_t mDataCount;
|
2012-11-03 08:41:11 -04:00
|
|
|
uint32_t mMaxSize;
|
2012-11-29 17:48:28 -05:00
|
|
|
std::string mName;
|
|
|
|
|
|
|
|
// some statistics.
|
|
|
|
void clearStats();
|
|
|
|
|
|
|
|
mutable uint32_t mStats_inserted;
|
|
|
|
mutable uint32_t mStats_dropped;
|
|
|
|
mutable uint32_t mStats_iscached;
|
|
|
|
mutable uint32_t mStats_cachemiss;
|
|
|
|
mutable uint32_t mStats_access;
|
|
|
|
mutable uint32_t mStats_accessmiss;
|
2012-11-03 08:41:11 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<class Key, class Value> bool RsMemCache<Key, Value>::is_cached(const Key &key) const
|
|
|
|
{
|
2012-11-03 09:15:21 -04:00
|
|
|
typename std::map<Key,cache_data>::const_iterator it;
|
2012-11-03 08:41:11 -04:00
|
|
|
it = mDataMap.find(key);
|
|
|
|
if (it == mDataMap.end())
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
std::cerr << "RsMemCache::is_cached(" << key << ") false";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
|
2012-11-29 17:48:28 -05:00
|
|
|
mStats_cachemiss++;
|
2012-11-03 08:41:11 -04:00
|
|
|
return false;
|
|
|
|
}
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
std::cerr << "RsMemCache::is_cached(" << key << ") false";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-29 17:48:28 -05:00
|
|
|
mStats_iscached++;
|
2012-11-03 08:41:11 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-29 16:09:56 -04:00
|
|
|
template<class Key, class Value> template<class ClientClass> bool RsMemCache<Key, Value>::applyToAllCachedEntries(ClientClass& c, bool (ClientClass::*method)(Value&))
|
|
|
|
{
|
|
|
|
bool res = true ;
|
|
|
|
|
|
|
|
for(typename std::map<Key,cache_data>::iterator it(mDataMap.begin());it!=mDataMap.end();++it)
|
|
|
|
res = res && ((c.*method)(it->second.data)) ;
|
|
|
|
|
|
|
|
return res ;
|
|
|
|
}
|
2012-11-03 08:41:11 -04:00
|
|
|
|
2016-06-04 21:39:40 -04:00
|
|
|
template<class Key, class Value> bool RsMemCache<Key, Value>::fetch(const Key &key, Value& data)
|
2012-11-03 08:41:11 -04:00
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
|
|
|
std::cerr << "RsMemCache::fetch()";
|
|
|
|
std::cerr << std::endl;
|
2012-11-29 17:48:28 -05:00
|
|
|
printStats(std::cerr);
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
|
|
|
|
2012-11-03 10:07:26 -04:00
|
|
|
typename std::map<Key, cache_data>::iterator it;
|
2012-11-03 08:41:11 -04:00
|
|
|
it = mDataMap.find(key);
|
|
|
|
if (it == mDataMap.end())
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
std::cerr << "RsMemCache::fetch(" << key << ") false";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
|
2012-11-29 17:48:28 -05:00
|
|
|
mStats_accessmiss++;
|
2012-11-03 08:41:11 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
std::cerr << "RsMemCache::fetch(" << key << ") OK";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
|
|
|
|
data = it->second.data;
|
|
|
|
|
|
|
|
/* update ts on data */
|
2018-10-06 19:34:05 -04:00
|
|
|
rstime_t old_ts = it->second.ts;
|
|
|
|
rstime_t new_ts = time(NULL);
|
2012-11-03 08:41:11 -04:00
|
|
|
it->second.ts = new_ts;
|
|
|
|
|
|
|
|
update_lrumap(key, old_ts, new_ts);
|
|
|
|
|
2012-11-29 17:48:28 -05:00
|
|
|
mStats_access++;
|
2012-11-03 08:41:11 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-29 17:48:28 -05:00
|
|
|
|
2013-10-20 05:43:30 -04:00
|
|
|
template<class Key, class Value> bool RsMemCache<Key, Value>::erase(const Key &key)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
|
|
|
std::cerr << "RsMemCache::erase()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
printStats(std::cerr);
|
|
|
|
#endif // DEBUG_RSMEMCACHE
|
|
|
|
|
|
|
|
typename std::map<Key, cache_data>::iterator it;
|
|
|
|
it = mDataMap.find(key);
|
|
|
|
if (it == mDataMap.end())
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
|
|
|
std::cerr << "RsMemCache::erase(" << key << ") false";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif // DEBUG_RSMEMCACHE
|
|
|
|
|
|
|
|
mStats_accessmiss++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
|
|
|
std::cerr << "RsMemCache::erase(" << key << ") OK";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif // DEBUG_RSMEMCACHE
|
|
|
|
|
|
|
|
|
|
|
|
/* get timestamps */
|
2018-10-06 19:34:05 -04:00
|
|
|
rstime_t old_ts = it->second.ts;
|
|
|
|
rstime_t new_ts = 0;
|
2013-10-20 05:43:30 -04:00
|
|
|
|
|
|
|
// remove from lru.
|
|
|
|
mDataMap.erase(it);
|
|
|
|
update_lrumap(key, old_ts, new_ts);
|
2013-11-05 18:33:12 -05:00
|
|
|
mDataCount--;
|
2013-10-20 05:43:30 -04:00
|
|
|
|
|
|
|
mStats_access++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-29 17:48:28 -05:00
|
|
|
template<class Key, class Value> Value &RsMemCache<Key, Value>::ref(const Key &key)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
|
|
|
std::cerr << "RsMemCache::ref()";
|
|
|
|
std::cerr << std::endl;
|
2012-11-29 17:48:28 -05:00
|
|
|
printStats(std::cerr);
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
|
|
|
|
2012-11-29 17:48:28 -05:00
|
|
|
typename std::map<Key, cache_data>::iterator it;
|
|
|
|
it = mDataMap.find(key);
|
|
|
|
if (it == mDataMap.end())
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-29 17:48:28 -05:00
|
|
|
std::cerr << "RsMemCache::ref(" << key << ") ERROR missing Key inserting Empty Data in LRU slot";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-29 17:48:28 -05:00
|
|
|
|
|
|
|
// insert operation.
|
2018-10-06 19:34:05 -04:00
|
|
|
rstime_t new_ts = 0;
|
2012-11-29 17:48:28 -05:00
|
|
|
Value data;
|
|
|
|
mDataMap[key] = cache_data(key, data, new_ts);
|
|
|
|
mDataCount++;
|
|
|
|
|
|
|
|
update_lrumap(key, 0, new_ts);
|
|
|
|
it = mDataMap.find(key);
|
|
|
|
|
|
|
|
mStats_accessmiss++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-29 17:48:28 -05:00
|
|
|
std::cerr << "RsMemCache::ref(" << key << ") OK";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-29 17:48:28 -05:00
|
|
|
|
|
|
|
/* update ts on data */
|
2018-10-06 19:34:05 -04:00
|
|
|
rstime_t old_ts = it->second.ts;
|
|
|
|
rstime_t new_ts = time(NULL);
|
2012-11-29 17:48:28 -05:00
|
|
|
it->second.ts = new_ts;
|
|
|
|
|
|
|
|
update_lrumap(key, old_ts, new_ts);
|
|
|
|
|
|
|
|
mStats_access++;
|
|
|
|
}
|
|
|
|
return it->second.data;
|
|
|
|
}
|
|
|
|
|
2012-11-03 08:41:11 -04:00
|
|
|
template<class Key, class Value> bool RsMemCache<Key, Value>::store(const Key &key, const Value &data)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
std::cerr << "RsMemCache::store()";
|
|
|
|
std::cerr << std::endl;
|
2012-11-29 17:48:28 -05:00
|
|
|
printStats(std::cerr);
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
|
2012-11-19 17:00:05 -05:00
|
|
|
/* update lrumap entry */
|
2018-10-06 19:34:05 -04:00
|
|
|
rstime_t old_ts = 0;
|
|
|
|
rstime_t new_ts = time(NULL);
|
2012-11-19 17:00:05 -05:00
|
|
|
|
2012-11-03 08:41:11 -04:00
|
|
|
// For consistency
|
2012-11-03 09:15:21 -04:00
|
|
|
typename std::map<Key, cache_data>::const_iterator it;
|
2012-11-03 08:41:11 -04:00
|
|
|
it = mDataMap.find(key);
|
|
|
|
if (it != mDataMap.end())
|
|
|
|
{
|
|
|
|
// ERROR.
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-19 17:00:05 -05:00
|
|
|
std::cerr << "RsMemCache::store() WARNING overriding existing entry";
|
2012-11-03 08:41:11 -04:00
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-19 17:00:05 -05:00
|
|
|
|
|
|
|
old_ts = it->second.ts;
|
2012-11-03 08:41:11 -04:00
|
|
|
}
|
2012-11-29 17:48:28 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
mDataCount++;
|
|
|
|
}
|
2012-11-03 08:41:11 -04:00
|
|
|
|
|
|
|
mDataMap[key] = cache_data(key, data, new_ts);
|
|
|
|
|
|
|
|
update_lrumap(key, old_ts, new_ts);
|
|
|
|
|
2012-11-29 17:48:28 -05:00
|
|
|
mStats_inserted++;
|
2012-11-03 08:41:11 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-06 19:34:05 -04:00
|
|
|
template<class Key, class Value> bool RsMemCache<Key, Value>::update_lrumap(const Key &key, rstime_t old_ts, rstime_t new_ts)
|
2012-11-03 08:41:11 -04:00
|
|
|
{
|
|
|
|
if (old_ts == 0)
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
std::cerr << "p3IdService::locked_cache_update_lrumap(" << key << ") just insert!";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
|
|
|
|
/* new insertion */
|
|
|
|
mLruMap.insert(std::make_pair(new_ts, key));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find old entry */
|
2018-10-06 19:34:05 -04:00
|
|
|
typename std::multimap<rstime_t, Key>::iterator mit;
|
|
|
|
typename std::multimap<rstime_t, Key>::iterator sit = mLruMap.lower_bound(old_ts);
|
|
|
|
typename std::multimap<rstime_t, Key>::iterator eit = mLruMap.upper_bound(old_ts);
|
2012-11-03 08:41:11 -04:00
|
|
|
|
2014-10-24 18:07:26 -04:00
|
|
|
for(mit = sit; mit != eit; ++mit)
|
2012-11-03 08:41:11 -04:00
|
|
|
{
|
|
|
|
if (mit->second == key)
|
|
|
|
{
|
|
|
|
mLruMap.erase(mit);
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
std::cerr << "p3IdService::locked_cache_update_lrumap(" << key << ") rm old";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
|
|
|
|
if (new_ts != 0) // == 0, means remove.
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
std::cerr << "p3IdService::locked_cache_update_lrumap(" << key << ") added new_ts";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
mLruMap.insert(std::make_pair(new_ts, key));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
std::cerr << "p3IdService::locked_cache_update_lrumap(" << key << ") ERROR";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Key, class Value> bool RsMemCache<Key, Value>::resize()
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
std::cerr << "RsMemCache::resize()";
|
|
|
|
std::cerr << std::endl;
|
2012-11-29 17:48:28 -05:00
|
|
|
printStats(std::cerr);
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
|
|
|
|
int count_to_clear = 0;
|
|
|
|
{
|
|
|
|
// consistency check.
|
|
|
|
if ((mDataMap.size() != mDataCount) ||
|
|
|
|
(mLruMap.size() != mDataCount))
|
|
|
|
{
|
|
|
|
// ERROR.
|
|
|
|
std::cerr << "RsMemCache::resize() CONSISTENCY ERROR";
|
|
|
|
std::cerr << std::endl;
|
2013-11-05 18:33:12 -05:00
|
|
|
std::cerr << "\tmDataMap.size() = " << mDataMap.size();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tmLruMap.size() = " << mLruMap.size();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tmDataCount = " << mDataCount;
|
|
|
|
std::cerr << std::endl;
|
2012-11-03 08:41:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mDataCount > mMaxSize)
|
|
|
|
{
|
|
|
|
count_to_clear = mDataCount - mMaxSize;
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
std::cerr << "RsMemCache::resize() to_clear: " << count_to_clear;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count_to_clear > 0)
|
|
|
|
{
|
|
|
|
discard_LRU(count_to_clear);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class Key, class Value> bool RsMemCache<Key, Value>::discard_LRU(int count_to_clear)
|
|
|
|
{
|
|
|
|
while(count_to_clear > 0)
|
|
|
|
{
|
2018-10-06 19:34:05 -04:00
|
|
|
typename std::multimap<rstime_t, Key>::iterator mit = mLruMap.begin();
|
2012-11-03 08:41:11 -04:00
|
|
|
if (mit != mLruMap.end())
|
|
|
|
{
|
|
|
|
Key key = mit->second;
|
|
|
|
mLruMap.erase(mit);
|
|
|
|
|
|
|
|
/* now clear from real cache */
|
|
|
|
//std::map<Key, cache_data<Key, Value> >::iterator it;
|
2012-11-03 09:15:21 -04:00
|
|
|
typename std::map<Key, cache_data>::iterator it;
|
2012-11-03 08:41:11 -04:00
|
|
|
it = mDataMap.find(key);
|
|
|
|
if (it == mDataMap.end())
|
|
|
|
{
|
|
|
|
// ERROR
|
|
|
|
std::cerr << "RsMemCache::discard_LRU(): ERROR Missing key: " << key;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
std::cerr << "RsMemCache::discard_LRU() removing: " << key;
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
mDataMap.erase(it);
|
|
|
|
mDataCount--;
|
2012-11-29 17:48:28 -05:00
|
|
|
mStats_dropped++;
|
2012-11-03 08:41:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No More Data, ERROR.
|
2013-02-27 16:16:03 -05:00
|
|
|
#ifdef DEBUG_RSMEMCACHE
|
2012-11-03 08:41:11 -04:00
|
|
|
std::cerr << "RsMemCache::discard_LRU(): INFO more more cache data";
|
|
|
|
std::cerr << std::endl;
|
2013-02-27 16:16:03 -05:00
|
|
|
#endif // DEBUG_RSMEMCACHE
|
|
|
|
|
2012-11-03 08:41:11 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
count_to_clear--;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-29 17:48:28 -05:00
|
|
|
// These aren't templated functions.
|
|
|
|
template<class Key, class Value> void RsMemCache<Key, Value>::printStats(std::ostream &out)
|
|
|
|
{
|
2018-10-06 19:34:05 -04:00
|
|
|
typename std::multimap<rstime_t, Key>::iterator mit = mLruMap.begin();
|
|
|
|
rstime_t age = 0;
|
2012-11-29 17:48:28 -05:00
|
|
|
if (mit != mLruMap.end())
|
|
|
|
{
|
|
|
|
age = time(NULL) - mit->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
out << "RsMemCache<" << mName << ">::printStats() Size: " << mDataCount << " Size2: " << mDataMap.size() << " Size3: " << mLruMap.size() << " MaxSize: " << mMaxSize << " LRU Age: " << age;
|
|
|
|
out << std::endl;
|
|
|
|
|
|
|
|
out << "\tInsertions: " << mStats_inserted << " Drops: " << mStats_dropped;
|
|
|
|
out << std::endl;
|
|
|
|
|
|
|
|
out << "\tCache Hits: " << mStats_iscached << " Misses: " << mStats_cachemiss;
|
|
|
|
out << std::endl;
|
|
|
|
|
|
|
|
out << "\tAccess Hits: " << mStats_access << " Misses: " << mStats_accessmiss;
|
|
|
|
out << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Key, class Value> void RsMemCache<Key, Value>::clearStats()
|
|
|
|
{
|
|
|
|
mStats_inserted = 0;
|
|
|
|
mStats_dropped = 0;
|
|
|
|
mStats_iscached = 0;
|
|
|
|
mStats_cachemiss = 0;
|
|
|
|
mStats_access = 0;
|
|
|
|
mStats_accessmiss = 0;
|
|
|
|
}
|
|
|
|
|
2012-11-03 08:41:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-03 09:15:21 -04:00
|
|
|
#endif // RS_UTIL_MEM_CACHE
|