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;
97 void insert(
const K& key,
const T& value ) {
107 bool get(
const K& key, Record& out ) {
118 bool has(
const K& key ) {
184 if ( mi !=
_map.end() ) {
186 _lru.erase(valType.second);
187 valType.first = value;
188 _lru.push_back( key );
189 valType.second =
_lru.end();
194 std::cout <<
"Cache hit: " << key << std::endl;
199 _lru.push_back( key );
201 _map[key] = std::make_pair(value, last);
206 std::cout <<
"Cache insert: " << key << std::endl;
212 for(
unsigned i=0; i <
_buf; ++i ) {
213 const K& key =
_lru.front();
217 std::cout <<
"Ejected: " <<
_map.find(key)->second.first.get() << std::endl;
229 if ( mi !=
_map.end() ) {
231 _lru.erase(valType.second);
232 _lru.push_back( key );
234 valType.second = new_iter;
236 result._value = valType.first;
237 result._valid =
true;
242 return _map.find( key ) !=
_map.end();
247 if ( mi !=
_map.end() ) {
248 _lru.erase( mi->second.second );
261 _max = std::max(max, 10u);
264 const K& key =
_lru.front();
272 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:138
bool has(const K &key)
Definition: DtOsgLRUCache.h:118
void clear_impl()
Definition: DtOsgLRUCache.h:253
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:260
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:245
void iterate(Functor &functor) const
Definition: DtOsgLRUCache.h:167
Record()
Definition: DtOsgLRUCache.h:45
unsigned _max
Definition: DtOsgLRUCache.h:69
LRUCache(bool threadsafe, unsigned max=100)
Definition: DtOsgLRUCache.h:85
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:95
virtual ~CacheStats()
dtor
Definition: DtOsgLRUCache.h:19
void setMaxSize(unsigned max)
Definition: DtOsgLRUCache.h:148
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:277
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:178
CacheStats getStats() const
Definition: DtOsgLRUCache.h:162
unsigned _hits
Definition: DtOsgLRUCache.h:72
T _value
Definition: DtOsgLRUCache.h:51
bool has_impl(const K &key)
Definition: DtOsgLRUCache.h:241
void get_impl(const K &key, Record &result)
Definition: DtOsgLRUCache.h:226
Definition: DtOsgLRUCache.h:55
bool valid() const
Definition: DtOsgLRUCache.h:47
void erase(const K &key)
Definition: DtOsgLRUCache.h:128
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:97
map_type::iterator map_iter
Definition: DtOsgLRUCache.h:64
void iterate_impl(Functor &f) const
Definition: DtOsgLRUCache.h:270
Threading::Mutex _mutex
Definition: DtOsgLRUCache.h:74
unsigned getMaxSize() const
Definition: DtOsgLRUCache.h:158
std::list< K > lru_type
Definition: DtOsgLRUCache.h:61
void insert_impl(const K &key, const T &value)
Definition: DtOsgLRUCache.h:182
Definition: DtOsgLRUCache.h:12