11#include <boost/multi_index_container.hpp>
12#include <boost/multi_index/sequenced_index.hpp>
13#include <boost/multi_index/hashed_index.hpp>
14#include <boost/multi_index/member.hpp>
48 template <
class KEY,
class VALUE>
class MRUCache {
53 typedef boost::multi_index_container <
55 boost::multi_index::indexed_by <
56 boost::multi_index::sequenced<>,
57 boost::multi_index::hashed_unique <
58 boost::multi_index::member<value_type, KEY, &value_type::first>
63 typedef typename container_type::const_iterator
iterator;
65 typedef typename container_type::template nth_index<1>::type::const_iterator
hash_iterator;
88 std::pair<iterator, bool> p =
m_container.push_front(item);
Class implementing a generic Most Recently Used cache.
iterator begin() const
Return iterator to the begin of the cache.
void update(const iterator &item)
Update an item, thus marking it as recently accessed and putting it to the front of the list.
void insert(const value_type &item)
Insert a key value pair into the cache.
size_t size() const
Return actual size of the cache.
size_t getMaxSize() const
Get maximum number of cache entries.
unsigned int getHits() const
Get number of cache hits since creation/last clear.
void insert(const KEY &key, const VALUE &value)
Insert a key value pair into the cache.
container_type m_container
Container for the items.
unsigned int getOverflows() const
Get number of overflows (dropped items) since creation/last clear.
unsigned int getMisses() const
Get number of cache misses since creation/last clear.
iterator end() const
Return iterator to the end of the cache.
container_type::template nth_index< 1 >::type::const_iterator hash_iterator
iterator over the hash index to the items
container_type::const_iterator iterator
iterator over all cached items, sorted by access: most recent used first
unsigned int m_hits
Number of hits.
size_t m_maxSize
Maximum size of the cache.
unsigned int m_misses
Number of misses.
boost::multi_index_container< value_type, boost::multi_index::indexed_by< boost::multi_index::sequenced<>, boost::multi_index::hashed_unique< boost::multi_index::member< value_type, KEY, &value_type::first > > > > container_type
type of container for the elements
bool retrieve(const KEY &key, VALUE &value)
Retrieve a value from the cache if it exists.
std::pair< KEY, VALUE > value_type
type of elements stored in the cache
void setMaxSize(size_t maxSize)
Set maximum number of cache entries.
unsigned int m_overflows
Number of overflows.
MRUCache(size_t maxSize)
Constructor setting the maximum number of cached items.
Abstract base class for different kinds of events.