libcommonc++
0.7
|
An LRU cache implementation in which cache objects are deleted when they are removed from the cache. More...
#include <DynamicCache.h++>
Public Member Functions | |
DynamicCache (uint_t maxSize) | |
Construct a new DynamicCache with the given maximum size. More... | |
virtual | ~DynamicCache () |
Destructor. More... | |
void | put (K key, T *item) |
Put an item in the cache. More... | |
virtual T * | get (const K &key) |
Look up an item in the cache. More... | |
T * | take (const K &key) |
Remove an item from the cache, and return the item. More... | |
bool | remove (const K &key) |
Remove an item 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... | |
virtual 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... | |
bool | isFull () const |
Test if the cache is full. More... | |
Protected Member Functions | |
virtual T * | loadItem (const K &key) |
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... | |
An LRU cache implementation in which cache objects are deleted when they are removed from the cache.
For an implementation that recycles existing objects, see StaticCache.
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.
DynamicCache | ( | uint_t | maxSize | ) |
Construct a new DynamicCache with the given maximum size.
maxSize | The maximum number of items that the cache will hold. If 0, the value 1 will be used instead. |
|
virtual |
Destructor.
Clears the cache.
|
virtualinherited |
Remove all items from the cache.
Reimplemented in StaticCache< K, T >.
|
inherited |
Test if the cache contains an item with the specified key.
key | The 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.
key | The item key. |
Implements AbstractCache< K, T >.
|
inlineinherited |
Return the maximum size of the cache, i.e., the maximum number of items that the cache can hold.
|
inlineinherited |
Return the current size of the cache, i.e., the number of items in the cache.
|
inlineinherited |
Test if the cache is empty.
|
inlineinherited |
Test if the cache is full.
|
protectedvirtual |
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.
key | The key for the item to load. |
|
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.
key | The key for the item being removed. |
item | The item being removed. |
|
inlineinherited |
Index operator.
Equivalent to get()
.
void put | ( | K | key, |
T * | item | ||
) |
Put an item in the cache.
Takes ownership of the item.
key | The key. |
item | The item. |
bool remove | ( | const K & | key | ) |
Remove an item from the cache.
key | The item key. |
T* take | ( | const K & | key | ) |
Remove an item from the cache, and return the item.
The onRemove()
method is not called.
key | The key. |