libcommonc++  0.7
StaticCache< K, T > Class Template Referenceabstract

An LRU cache implementation in which cache objects are recycled when they are removed from the cache. More...

#include <StaticCache.h++>

Inheritance diagram for StaticCache< K, T >:
Collaboration diagram for StaticCache< K, T >:

Public Member Functions

 StaticCache (uint_t maxSize)
 Construct a new StaticCache with the given maximum size. More...
 
virtual ~StaticCache ()
 Destructor. More...
 
virtual T * get (const K &key)
 Look up an item in the cache. More...
 
virtual void clear ()
 Remove all items from the cache. More...
 
T * operator[] (const K &key)
 Index operator. More...
 
bool contains (const K &key) const
 Test if the cache contains an item with the specified key. 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...
 
bool isFull () const
 Test if the cache is full. More...
 

Protected Member Functions

virtual T * constructItem ()=0
 Construct a new cache item. More...
 
virtual bool loadItem (const K &key, T *item)=0
 Called by get() when the cache does not contain an item with the requested key. More...
 
virtual void onRemove (const 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::StaticCache< K, T >

An LRU cache implementation in which cache objects are recycled when they are removed from the cache.

For an implementation that does not recycle existing objects, see DynamicCache.

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 recycled to become the new item.

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

Author
Mark Lindner

Constructor & Destructor Documentation

◆ StaticCache()

StaticCache ( uint_t  maxSize)

Construct a new StaticCache 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.

◆ ~StaticCache()

virtual ~StaticCache ( )
virtual

Destructor.

Clears the cache.

Member Function Documentation

◆ clear()

virtual void clear ( )
virtual

Remove all items from the cache.

Reimplemented from AbstractCache< K, T >.

◆ constructItem()

virtual T* constructItem ( )
protectedpure virtual

Construct a new cache item.

As long as the cache is not at full capacity, this method is called to construct a new item whenever one is needed.

Returns
The new, uninitialized item.

◆ contains()

bool contains ( const K &  key) const
inherited

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()

virtual T* get ( const K &  key)
virtual

Look up an item in the cache.

If the cache does not contain an item with the given key, loadItem() is called to load the item from the external data source.

Parameters
keyThe item key.
Returns
The item associated with the key, or NULL if no item with the specified key was found (and could not be loaded).

Implements AbstractCache< K, T >.

◆ getMaxSize()

uint_t getMaxSize ( ) const
inlineinherited

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

◆ getSize()

uint_t getSize ( ) const
inlineinherited

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

◆ isEmpty()

bool isEmpty ( ) const
inlineinherited

Test if the cache is empty.

◆ isFull()

bool isFull ( ) const
inlineinherited

Test if the cache is full.

◆ loadItem()

virtual bool loadItem ( const K &  key,
T *  item 
)
protectedpure virtual

Called by get() when the cache does not contain an item with the requested key.

Subclasses may override this method to load the item from an external data source. The default implementation is a no-op which returns NULL.

Parameters
keyThe key for the item to load.
itemThe item that is being recycled. The item should be re-initialized with the data for the given key.
Returns
true if the item was loaded successfully, false otherwise. The supplied item should not be modified when returning false.

◆ onRemove()

virtual void onRemove ( const K &  key,
T *  item 
)
protectedvirtualinherited

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[] ( const K &  key)
inlineinherited

Index operator.

Equivalent to get().


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