libcommonc++  0.7
Cache< K, T > Class Template Reference

A general-purpose LRU-cache with a maximum capacity. More...

#include <Cache.h++>

Public Member Functions

 Cache (uint_t maxSize)
 Construct a new cache with the given maximum size. More...
 
virtual ~Cache ()
 Destructor. More...
 
void put (K key, T *item)
 Put an item in the cache. More...
 
T * get (K key) const
 Look up an item in the cache. More...
 
T * operator[] (K key) const
 Index operator. More...
 
T * take (K key)
 Remove an item from the cache, and return the item. More...
 
bool remove (K key)
 Remove an item from the cache. More...
 
bool contains (K key) const
 Test if the cache contains an item with the specified key. More...
 
void clear ()
 Remove all items from the cache. More...
 
uint_t getMaxSize () const
 Return the maximum size of the cache, i.e., the maximum number of items that the cache can hold. More...
 
uint_t getSize () const
 Return the current size of the cache, i.e., the number of items in the cache. More...
 
bool isEmpty () const
 Test if the cache is empty. More...
 

Protected Member Functions

virtual void onRemove (K key, T *item)
 A method that is called whenever an item is being removed from the cache, but just before it has been deleted. More...
 

Detailed Description

template<typename K, typename T>
class ccxx::Cache< K, T >

A general-purpose LRU-cache with a maximum capacity.

The cache maintains a key/value map, and takes ownership of its items. If the cache is at full capacity when a new item is inserted, the least recently accessed item is removed to make room.

Whenever an item is removed from the cache as a result of a call to clear() remove(), put(), or the destructor, the onRemove() method is called just before the item is deleted.

Author
Mark Lindner

Constructor & Destructor Documentation

◆ Cache()

Cache ( uint_t  maxSize)

Construct a new cache with the given maximum size.

Parameters
maxSizeThe maximum number of items that the cache will hold. If 0, the value 1 will be used instead.

◆ ~Cache()

virtual ~Cache ( )
virtual

Destructor.

Clears the cache.

Member Function Documentation

◆ clear()

void clear ( )

Remove all items from the cache.

◆ contains()

bool contains ( key) const

Test if the cache contains an item with the specified key.

Parameters
keyThe key.
Returns
true the cache contains an item with the specified key, false otherwise.

◆ get()

T* get ( key) const

Look up an item in the cache.

Parameters
keyThe item key.
Returns
The item associated with the key, or NULL if no item with the specified key was found.

◆ getMaxSize()

uint_t getMaxSize ( ) const
inline

Return the maximum size of the cache, i.e., the maximum number of items that the cache can hold.

◆ getSize()

uint_t getSize ( ) const
inline

Return the current size of the cache, i.e., the number of items in the cache.

◆ isEmpty()

bool isEmpty ( ) const
inline

Test if the cache is empty.

◆ onRemove()

virtual void onRemove ( key,
T *  item 
)
protectedvirtual

A method that is called whenever an item is being removed from the cache, but just before it has been deleted.

The default implementation is a no-op. Subclasses may override this method to do any necessary cleanup of the item before it is deleted, but should not delete the item itself.

Parameters
keyThe key for the item being removed.
itemThe item being removed.

◆ operator[]()

T* operator[] ( key) const
inline

Index operator.

Equivalent to get().

◆ put()

void put ( key,
T *  item 
)

Put an item in the cache.

Parameters
keyThe key.
itemThe item.

◆ remove()

bool remove ( key)

Remove an item from the cache.

Parameters
keyThe item key.
Returns
true if an item was removed, false if no item with the specified key was found.

◆ take()

T* take ( key)

Remove an item from the cache, and return the item.

The onRemove() method is not called.

Parameters
keyThe key.
Returns
The item associated with the key, or NULL if no item with the specified key was found.

The documentation for this class was generated from the following file: