8 #define LRUCACHE_DEBUG 1
15 CacheStats(
unsigned entries,
unsigned maxEntries,
unsigned queries,
float hitRatio )
40 template<
typename K,
typename T,
typename COMPARE=std::less<K> >
56 virtual void operator()(
const K& key,
const T& value) =0;
60 typedef typename std::list<K>::iterator
lru_iter;
63 typedef typename std::map<K, map_value_type>
map_type;
100 void insert(
const K& key,
const T& value ) {
110 bool get(
const K& key, Record& out ) {
121 bool has(
const K& key ) {
187 if ( mi !=
_map.end() ) {
189 _lru.erase(valType.second);
190 valType.first = value;
191 _lru.push_back( key );
192 valType.second =
_lru.end();
197 std::cout <<
"Cache hit: " << key << std::endl;
202 _lru.push_back( key );
204 _map[key] = std::make_pair(value, last);
209 std::cout <<
"Cache insert: " << key << std::endl;
215 for(
unsigned i=0; i <
_buf; ++i ) {
216 const K& key =
_lru.front();
220 std::cout <<
"Ejected: " <<
_map.find(key)->second.first.get() << std::endl;
232 if ( mi !=
_map.end() ) {
234 _lru.erase(valType.second);
235 _lru.push_back( key );
237 valType.second = new_iter;
239 result._value = valType.first;
240 result._valid =
true;
245 return _map.find( key ) !=
_map.end();
250 if ( mi !=
_map.end() ) {
251 _lru.erase( mi->second.second );
264 _max = std::max(max, 10u);
267 const K& key =
_lru.front();
275 f(i->first, i->second.first);
unsigned _entries
Definition: DtOsgLRUCache.h:21
lru_type _lru
Definition: DtOsgLRUCache.h:68
Least-recently-used cache class.
Definition: DtOsgLRUCache.h:41
void clear()
Definition: DtOsgLRUCache.h:141
bool has(const K &key)
Definition: DtOsgLRUCache.h:121
void clear_impl()
Definition: DtOsgLRUCache.h:256
unsigned _maxEntries
Definition: DtOsgLRUCache.h:22
const T & value() const
Definition: DtOsgLRUCache.h:48
Record(const T &value)
Definition: DtOsgLRUCache.h:46
OpenThreads::ScopedLock< OpenThreads::Mutex > ScopedMutexLock
Definition: DtOsgThreadingUtils.h:13
void setMaxSize_impl(unsigned max)
Definition: DtOsgLRUCache.h:263
float _hitRatio
Definition: DtOsgLRUCache.h:24
std::list< K >::iterator lru_iter
Definition: DtOsgLRUCache.h:60
void erase_impl(const K &key)
Definition: DtOsgLRUCache.h:248
void iterate(Functor &functor) const
Definition: DtOsgLRUCache.h:170
Record()
Definition: DtOsgLRUCache.h:45
unsigned _max
Definition: DtOsgLRUCache.h:69
LRUCache(bool threadsafe, unsigned max=100)
Definition: DtOsgLRUCache.h:88
map_type _map
Definition: DtOsgLRUCache.h:67
virtual void operator()(const K &key, const T &value)=0
unsigned _queries
Definition: DtOsgLRUCache.h:71
virtual ~LRUCache()
dtor
Definition: DtOsgLRUCache.h:98
virtual ~CacheStats()
dtor
Definition: DtOsgLRUCache.h:19
void setMaxSize(unsigned max)
Definition: DtOsgLRUCache.h:151
std::pair< T, lru_iter > map_value_type
Definition: DtOsgLRUCache.h:62
Definition: DtOsgLRUCache.h:44
bool _valid
Definition: DtOsgLRUCache.h:50
map_type::const_iterator map_const_iter
Definition: DtOsgLRUCache.h:65
unsigned _queries
Definition: DtOsgLRUCache.h:23
bool myDebug
Definition: DtOsgLRUCache.h:280
OpenThreads::Mutex Mutex
Definition: DtOsgThreadingUtils.h:12
LRUCache(unsigned max=100)
Definition: DtOsgLRUCache.h:77
bool _threadsafe
Definition: DtOsgLRUCache.h:73
unsigned _buf
Definition: DtOsgLRUCache.h:70
void enableDebug(bool enable)
Definition: DtOsgLRUCache.h:181
CacheStats getStats() const
Definition: DtOsgLRUCache.h:165
unsigned _hits
Definition: DtOsgLRUCache.h:72
T _value
Definition: DtOsgLRUCache.h:51
bool has_impl(const K &key)
Definition: DtOsgLRUCache.h:244
void get_impl(const K &key, Record &result)
Definition: DtOsgLRUCache.h:229
Definition: DtOsgLRUCache.h:55
bool valid() const
Definition: DtOsgLRUCache.h:47
void erase(const K &key)
Definition: DtOsgLRUCache.h:131
std::map< K, map_value_type > map_type
Definition: DtOsgLRUCache.h:63
CacheStats(unsigned entries, unsigned maxEntries, unsigned queries, float hitRatio)
Definition: DtOsgLRUCache.h:15
void insert(const K &key, const T &value)
Definition: DtOsgLRUCache.h:100
map_type::iterator map_iter
Definition: DtOsgLRUCache.h:64
void iterate_impl(Functor &f) const
Definition: DtOsgLRUCache.h:273
Threading::Mutex _mutex
Definition: DtOsgLRUCache.h:74
unsigned getMaxSize() const
Definition: DtOsgLRUCache.h:161
std::list< K > lru_type
Definition: DtOsgLRUCache.h:61
void insert_impl(const K &key, const T &value)
Definition: DtOsgLRUCache.h:185
Definition: DtOsgLRUCache.h:12