VR-Forces 4.10 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtOsgLRUCache.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <osg/ref_ptr>
6 #include <iostream>
7 
8 #define LRUCACHE_DEBUG 1
9 
10 namespace makVrv {
11 
12 struct CacheStats
13 {
14 public:
15  CacheStats( unsigned entries, unsigned maxEntries, unsigned queries, float hitRatio )
16  : _entries(entries), _maxEntries(maxEntries), _queries(queries), _hitRatio(hitRatio) { }
17 
19  virtual ~CacheStats() { }
20 
21  unsigned _entries;
22  unsigned _maxEntries;
23  unsigned _queries;
24  float _hitRatio;
25 };
26 
27 
28 
40 template<typename K, typename T, typename COMPARE=std::less<K> >
41 class LRUCache
42 {
43 public:
44  struct Record {
45  Record() : _valid(false) { }
46  Record(const T& value) : _value(value), _valid(true) { }
47  bool valid() const { return _valid; }
48  const T& value() const { return _value; }
49  private:
50  bool _valid;
51  T _value;
52  friend class LRUCache;
53  };
54 
55  struct Functor {
56  virtual void operator()(const K& key, const T& value) =0;
57  };
58 
59 protected:
60  typedef typename std::list<K>::iterator lru_iter;
61  typedef typename std::list<K> lru_type;
62  typedef typename std::pair<T, lru_iter> map_value_type;
63  typedef typename std::map<K, map_value_type> map_type;
64  typedef typename map_type::iterator map_iter;
65  typedef typename map_type::const_iterator map_const_iter;
66 
69  unsigned _max;
70  unsigned _buf;
71  unsigned _queries;
72  unsigned _hits;
75 
76 public:
77  LRUCache( unsigned max =100 )
78  : _max(max)
79  , _threadsafe(false)
80  , _queries(0)
81  , _hits(0)
82  {
83  setMaxSize_impl(max);
84 #if LRUCACHE_DEBUG
85  myDebug = false;
86 #endif
87  }
88  LRUCache( bool threadsafe, unsigned max =100 ) : _max(max), _threadsafe(threadsafe) {
89  _queries = 0;
90  _hits = 0;
91  setMaxSize_impl(max);
92 #if LRUCACHE_DEBUG
93  myDebug = false;
94 #endif
95  }
96 
98  virtual ~LRUCache() { }
99 
100  void insert( const K& key, const T& value ) {
101  if ( _threadsafe ) {
103  insert_impl( key, value );
104  }
105  else {
106  insert_impl( key, value );
107  }
108  }
109 
110  bool get( const K& key, Record& out ) {
111  if ( _threadsafe ) {
113  get_impl( key, out );
114  }
115  else {
116  get_impl( key, out );
117  }
118  return out.valid();
119  }
120 
121  bool has( const K& key ) {
122  if ( _threadsafe ) {
124  return has_impl( key );
125  }
126  else {
127  return has_impl( key );
128  }
129  }
130 
131  void erase( const K& key ) {
132  if ( _threadsafe ) {
134  erase_impl( key );
135  }
136  else {
137  erase_impl( key );
138  }
139  }
140 
141  void clear() {
142  if ( _threadsafe ) {
144  clear_impl();
145  }
146  else {
147  clear_impl();
148  }
149  }
150 
151  void setMaxSize( unsigned max ) {
152  if ( _threadsafe ) {
154  setMaxSize_impl( max );
155  }
156  else {
157  setMaxSize_impl( max );
158  }
159  }
160 
161  unsigned getMaxSize() const {
162  return _max;
163  }
164 
166  return CacheStats(
167  _map.size(), _max, _queries, _queries > 0 ? (float)_hits/(float)_queries : 0.0f );
168  }
169 
170  void iterate(Functor& functor) const {
171  if (_threadsafe) {
173  iterate_impl(functor);
174  }
175  else {
176  iterate_impl(functor);
177  }
178  }
179 
180 #if LRUCACHE_DEBUG
181  void enableDebug(bool enable) { myDebug = enable; }
182 #endif
183 
184 private:
185  void insert_impl( const K& key, const T& value ) {
186  map_iter mi = _map.find( key );
187  if ( mi != _map.end() ) {
188  map_value_type& valType = mi->second;
189  _lru.erase(valType.second);
190  valType.first = value;
191  _lru.push_back( key );
192  valType.second = _lru.end();
193  valType.second--;
194 #if LRUCACHE_DEBUG
195  if (myDebug)
196  {
197  std::cout << "Cache hit: " << key << std::endl;
198  }
199 #endif
200  }
201  else {
202  _lru.push_back( key );
203  lru_iter last = _lru.end(); --last;
204  _map[key] = std::make_pair(value, last);
205 
206 #if LRUCACHE_DEBUG
207  if (myDebug)
208  {
209  std::cout << "Cache insert: " << key << std::endl;
210  }
211 #endif
212  }
213 
214  if ( _map.size() > _max ) {
215  for( unsigned i=0; i < _buf; ++i ) {
216  const K& key = _lru.front();
217 #if LRUCACHE_DEBUG
218  if (myDebug)
219  {
220  std::cout << "Ejected: " << _map.find(key)->second.first.get() << std::endl;
221  }
222 #endif
223  _map.erase( key );
224  _lru.pop_front();
225  }
226  }
227  }
228 
229  void get_impl( const K& key, Record& result ) {
230  _queries++;
231  map_iter mi = _map.find( key );
232  if ( mi != _map.end() ) {
233  map_value_type& valType = mi->second;
234  _lru.erase(valType.second);
235  _lru.push_back( key );
236  lru_iter new_iter = _lru.end(); --new_iter;
237  valType.second = new_iter;
238  _hits++;
239  result._value = valType.first;
240  result._valid = true;
241  }
242  }
243 
244  bool has_impl( const K& key ) {
245  return _map.find( key ) != _map.end();
246  }
247 
248  void erase_impl( const K& key ) {
249  map_iter mi = _map.find( key );
250  if ( mi != _map.end() ) {
251  _lru.erase( mi->second.second );
252  _map.erase( mi );
253  }
254  }
255 
256  void clear_impl() {
257  _lru.clear();
258  _map.clear();
259  _queries = 0;
260  _hits = 0;
261  }
262 
263  void setMaxSize_impl( unsigned max ) {
264  _max = std::max(max, 10u);
265  _buf = _max / 10u;
266  while( _map.size() > _max ) {
267  const K& key = _lru.front();
268  _map.erase( key );
269  _lru.pop_front();
270  }
271  }
272 
273  void iterate_impl(Functor& f) const {
274  for (map_const_iter i = _map.begin(); i != _map.end(); ++i) {
275  f(i->first, i->second.first);
276  }
277  }
278 
279 #if LRUCACHE_DEBUG
280  bool myDebug;
281 #endif
282 
283 };
284 }
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

Document ID: Generated on Tue Sep 21 17:42:52 EDT 2021 from SVN revision 234861
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)