62 #include <boost/thread/mutex.hpp>
64 #define SCOPED_MUTEX boost::mutex::scoped_lock lock(this->_mutex);
72 unsigned long operator()(
const T& ) {
return 1; }
86 template<
class Key,
class Data,
class Sizefn = Countfn< Data > >
class LRUCache {
88 typedef std::list< std::pair< Key, Data > >
List;
94 typedef std::map< Key, List_Iter >
Map;
95 typedef std::pair< Key, List_Iter >
Pair;
103 unsigned long _max_size;
104 unsigned long _curr_size;
129 for (
List_Iter it = _list.begin(); it != _list.end(); ++it)
130 _index.insert(std::make_pair(it->first, it));
131 _max_size = that._max_size;
132 _curr_size = that._curr_size;
143 inline unsigned long size(
void )
const {
return _curr_size; }
148 inline unsigned long max_size(
void )
const {
return _max_size; }
163 inline bool exists(
const Key &key ) {
166 inline bool exists(
const Key &key )
const {
168 return _index.find( key ) != _index.end();
174 inline void remove(
const Key &key ) {
178 Map_Iter miter = _index.find( key );
179 if( miter == _index.end() )
return;
186 inline void touch(
const Key &key ) {
196 inline Data *
fetch_ptr(
const Key &key,
bool touch_data =
true ) {
198 Map_Iter miter = _index.find( key );
199 if( miter == _index.end() )
return NULL;
201 _list.splice( _list.begin(), _list, miter->second );
202 return &(miter->second->second);
210 inline Data
fetch(
const Key &key,
bool touch_data =
true ) {
212 Map_Iter miter = _index.find( key );
213 if( miter == _index.end() )
215 Data tmp = miter->second->second;
217 _list.splice( _list.begin(), _list, miter->second );
227 inline bool fetch(
const Key &key, Data &data,
bool touch_data =
true ) {
229 Map_Iter miter = _index.find( key );
230 if( miter == _index.end() )
return false;
232 _list.splice( _list.begin(), _list, miter->second );
233 data = miter->second->second;
242 inline void insert(
const Key &key,
const Data &data ) {
246 if( miter != _index.end() )
250 _list.push_front( std::make_pair( key, data ) );
254 _index.insert( std::make_pair( key, liter ) );
255 _curr_size += Sizefn()( data );
262 while( _curr_size > _max_size ) {
266 _remove( liter->first );
273 for (
size_t i=0; i<_curr_size; ++i, ++lit)
276 void *ptr1 = &(lit->second);
277 Key k2 = _index.find(k1)->first;
278 void *ptr2 = &(_index.find(k1)->second->second);
279 fprintf(stderr,
"cache: info: [%zu]: %zu, %zu (%zu) | %p, %p (%zu).\n",
280 i, k1, k2, k1 - k2, ptr1, ptr2, (
char*)ptr1 - (
char*)ptr2);
290 for(
List_cIter liter = _list.begin(); liter != _list.end(); liter++ )
291 ret.push_back( liter->first );
301 inline Map_Iter _touch(
const Key &key ) {
302 Map_Iter miter = _index.find( key );
303 if( miter == _index.end() )
return miter;
310 _list.splice( _list.begin(), _list, miter->second );
322 inline void _remove(
const Map_Iter &miter ) {
323 _curr_size -= Sizefn()( miter->second->second );
324 _list.erase( miter->second );
325 _index.erase( miter );
331 inline void _remove(
const Key &key ) {
332 Map_Iter miter = _index.find( key );
337 #endif // _lru_cache_h
void insert(const Key &key, const Data &data)
Inserts a key-data pair into the cache and removes entries if neccessary.
Definition: lru_cache.h:242
Key_List::const_iterator Key_List_cIter
Main cache iterator (const)
Definition: lru_cache.h:93
Map::iterator Map_Iter
Index iterator.
Definition: lru_cache.h:96
Map::const_iterator Map_cIter
Index iterator (const)
Definition: lru_cache.h:97
Key_List::iterator Key_List_Iter
Main cache iterator.
Definition: lru_cache.h:92
bool fetch(const Key &key, Data &data, bool touch_data=true)
Fetches a pointer to cache data.
Definition: lru_cache.h:227
std::list< std::pair< Key, Data > > List
Main cache storage typedef.
Definition: lru_cache.h:88
const Key_List get_all_keys(void)
Get a list of keys.
Definition: lru_cache.h:287
void touch(const Key &key)
Touches a key in the Cache and makes it the most recently used.
Definition: lru_cache.h:186
unsigned long size(void) const
Gets the current abstract size of the cache.
Definition: lru_cache.h:143
List::const_iterator List_cIter
Main cache iterator (const)
Definition: lru_cache.h:90
std::vector< Key > Key_List
List of keys.
Definition: lru_cache.h:91
LRUCache(const unsigned long Size)
Creates a cache that holds at most Size worth of elements.
Definition: lru_cache.h:115
Data fetch(const Key &key, bool touch_data=true)
Fetches a copy of cached data.
Definition: lru_cache.h:210
Data * fetch_ptr(const Key &key, bool touch_data=true)
Fetches a pointer to cache data.
Definition: lru_cache.h:196
List::iterator List_Iter
Main cache iterator.
Definition: lru_cache.h:89
#define SCOPED_MUTEX
If we aren't reentrant then don't do anything.
Definition: lru_cache.h:67
unsigned long max_size(void) const
Gets the maximum sbstract size of the cache.
Definition: lru_cache.h:148
std::pair< Key, List_Iter > Pair
Pair of Map elements.
Definition: lru_cache.h:95
void clear(void)
Clears all storage and indices.
Definition: lru_cache.h:151
Template cache with an LRU removal policy.
Definition: lru_cache.h:86
std::map< Key, List_Iter > Map
Index typedef.
Definition: lru_cache.h:94
~LRUCache()
Destructor - cleans up both index and storage.
Definition: lru_cache.h:138
Definition: lru_cache.h:71
bool exists(const Key &key) const
Checks for the existance of a key in the cache.
Definition: lru_cache.h:166