VR-Forces 4.8 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 ) : _max(max), _threadsafe(false) {
78  _queries = 0;
79  _hits = 0;
80  setMaxSize_impl(max);
81 #if LRUCACHE_DEBUG
82  myDebug = false;
83 #endif
84  }
85  LRUCache( bool threadsafe, unsigned max =100 ) : _max(max), _threadsafe(threadsafe) {
86  _queries = 0;
87  _hits = 0;
88  setMaxSize_impl(max);
89 #if LRUCACHE_DEBUG
90  myDebug = false;
91 #endif
92  }
93 
95  virtual ~LRUCache() { }
96 
97  void insert( const K& key, const T& value ) {
98  if ( _threadsafe ) {
100  insert_impl( key, value );
101  }
102  else {
103  insert_impl( key, value );
104  }
105  }
106 
107  bool get( const K& key, Record& out ) {
108  if ( _threadsafe ) {
110  get_impl( key, out );
111  }
112  else {
113  get_impl( key, out );
114  }
115  return out.valid();
116  }
117 
118  bool has( const K& key ) {
119  if ( _threadsafe ) {
121  return has_impl( key );
122  }
123  else {
124  return has_impl( key );
125  }
126  }
127 
128  void erase( const K& key ) {
129  if ( _threadsafe ) {
131  erase_impl( key );
132  }
133  else {
134  erase_impl( key );
135  }
136  }
137 
138  void clear() {
139  if ( _threadsafe ) {
141  clear_impl();
142  }
143  else {
144  clear_impl();
145  }
146  }
147 
148  void setMaxSize( unsigned max ) {
149  if ( _threadsafe ) {
151  setMaxSize_impl( max );
152  }
153  else {
154  setMaxSize_impl( max );
155  }
156  }
157 
158  unsigned getMaxSize() const {
159  return _max;
160  }
161 
163  return CacheStats(
164  _map.size(), _max, _queries, _queries > 0 ? (float)_hits/(float)_queries : 0.0f );
165  }
166 
167  void iterate(Functor& functor) const {
168  if (_threadsafe) {
170  iterate_impl(functor);
171  }
172  else {
173  iterate_impl(functor);
174  }
175  }
176 
177 #if LRUCACHE_DEBUG
178  void enableDebug(bool enable) { myDebug = enable; }
179 #endif
180 
181 private:
182  void insert_impl( const K& key, const T& value ) {
183  map_iter mi = _map.find( key );
184  if ( mi != _map.end() ) {
185  map_value_type& valType = mi->second;
186  _lru.erase(valType.second);
187  valType.first = value;
188  _lru.push_back( key );
189  valType.second = _lru.end();
190  valType.second--;
191 #if LRUCACHE_DEBUG
192  if (myDebug)
193  {
194  std::cout << "Cache hit: " << key << std::endl;
195  }
196 #endif
197  }
198  else {
199  _lru.push_back( key );
200  lru_iter last = _lru.end(); --last;
201  _map[key] = std::make_pair(value, last);
202 
203 #if LRUCACHE_DEBUG
204  if (myDebug)
205  {
206  std::cout << "Cache insert: " << key << std::endl;
207  }
208 #endif
209  }
210 
211  if ( _map.size() > _max ) {
212  for( unsigned i=0; i < _buf; ++i ) {
213  const K& key = _lru.front();
214 #if LRUCACHE_DEBUG
215  if (myDebug)
216  {
217  std::cout << "Ejected: " << _map.find(key)->second.first.get() << std::endl;
218  }
219 #endif
220  _map.erase( key );
221  _lru.pop_front();
222  }
223  }
224  }
225 
226  void get_impl( const K& key, Record& result ) {
227  _queries++;
228  map_iter mi = _map.find( key );
229  if ( mi != _map.end() ) {
230  map_value_type& valType = mi->second;
231  _lru.erase(valType.second);
232  _lru.push_back( key );
233  lru_iter new_iter = _lru.end(); --new_iter;
234  valType.second = new_iter;
235  _hits++;
236  result._value = valType.first;
237  result._valid = true;
238  }
239  }
240 
241  bool has_impl( const K& key ) {
242  return _map.find( key ) != _map.end();
243  }
244 
245  void erase_impl( const K& key ) {
246  map_iter mi = _map.find( key );
247  if ( mi != _map.end() ) {
248  _lru.erase( mi->second.second );
249  _map.erase( mi );
250  }
251  }
252 
253  void clear_impl() {
254  _lru.clear();
255  _map.clear();
256  _queries = 0;
257  _hits = 0;
258  }
259 
260  void setMaxSize_impl( unsigned max ) {
261  _max = std::max(max, 10u);
262  _buf = _max / 10u;
263  while( _map.size() > _max ) {
264  const K& key = _lru.front();
265  _map.erase( key );
266  _lru.pop_front();
267  }
268  }
269 
270  void iterate_impl(Functor& f) const {
271  for (map_const_iter i = _map.begin(); i != _map.end(); ++i) {
272  f(i->first, i->second.first);
273  }
274  }
275 
276 #if LRUCACHE_DEBUG
277  bool myDebug;
278 #endif
279 
280 };
281 }
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

Document ID: Generated on Thu Aug 27 10:56:05 EDT 2020 from SVN revision 217100
Copyright © 2005-2020 MAK Technologies. All Rights Reserved (www.mak.com)