Generic Cache
[UTIL]

Collaboration diagram for Generic Cache:


Files

file  ncbi_cache.hpp
 The NCBI C++ generic cache template.

Classes

struct  SCacheElement< TKey, TSize >
 Internal structure to hold cache elements. More...
struct  CCacheElement_Less< TCacheElementPtr >
 Compare cache elements by weight/order. More...
class  CCacheElement_Handler< TKey, TValue >
 Default (NOP) element handler. More...
class  CCache< TKey, TValue, THandler, TLock, TSize >
 Cache template. More...
class  CCacheException
 Exception thrown by CCache. More...

Typedefs

typedef CMutex TCacheLock_Default
 Default cache lock.

Enumerations

enum  ECache_InsertFlag { eCache_CheckSize, eCache_CanInsert, eCache_NeedCleanup, eCache_DoNotCache }
 Flag indicating if an element can be inserted into cache. More...

Functions

 CCache::CCache (TSizeType capacity, THandler *handler=new THandler())
 Create cache object with the given capacity.
 CCache::~CCache (void)
void CCache::x_EraseElement (TCacheSet_I &set_iter, TCacheMap_I &map_iter)
void CCache::x_EraseLast (void)
TCacheElement * CCache::x_InsertElement (const TKeyType &key, TWeight weight)
void CCache::x_UpdateElement (TCacheElement *elem)
TOrder CCache::x_GetNextCounter (void)
void CCache::x_PackElementIndex (void)
TOrder CCache::Add (const TKeyType &key, const TValueType &value, TWeight weight=1, TAddFlags add_flags=0, EAddResult *result=NULL)
 Add new element to the cache or replace the existing value.
TValueType CCache::operator[] (const TKeyType &key)
 Get cache element by the key.
TValueType CCache::Get (const TKeyType &key, TGetFlags get_flags=0, EGetResult *result=NULL)
 Get an object from the cache by its key.
bool CCache::Remove (const TKeyType &key)
 Remove element from cache. Do nothing if the key is not cached.
void CCache::SetCapacity (TSizeType new_capacity)
 Set new capacity of the cache.
void CCache::SetSize (TSizeType new_size)
 Truncate the cache leaving at most new_size elements.


Typedef Documentation

typedef CMutex TCacheLock_Default
 

Default cache lock.

Definition at line 134 of file ncbi_cache.hpp.


Enumeration Type Documentation

enum ECache_InsertFlag
 

Flag indicating if an element can be inserted into cache.

Enumerator:
eCache_CheckSize  Insert the element after checking max cache size.
eCache_CanInsert  The element can be inserted.
eCache_NeedCleanup  Need to cleanup cache before inserting the element.
eCache_DoNotCache  The element can not be inserted (e.g. too big).

Definition at line 96 of file ncbi_cache.hpp.


Function Documentation

template<class TKey, class TValue, class THandler, class TLock, class TSize>
CCache< TKey, TValue, THandler, TLock, TSize >::TOrder CCache< TKey, TValue, THandler, TLock, TSize >::Add const TKeyType key,
const TValueType value,
TWeight  weight = 1,
TAddFlags  add_flags = 0,
EAddResult result = NULL
[inherited]
 

Add new element to the cache or replace the existing value.

Parameters:
key Element key
value Element value
weight Weight adjustment. The lifetime of each object in the cache is proportional to its weight.
add_flags Flags to control Add() behavior.
result Pointer to a variable to store operation result code to.
Returns:
Index of the new element in the cache.

Definition at line 506 of file ncbi_cache.hpp.

References CCache< TKey, TValue, THandler, TLock, TSize >::eAdd_NotInserted, CCache< TKey, TValue, THandler, TLock, TSize >::eAdd_Replaced, map_checker< Container >::end(), CCache< TKey, TValue, THandler, TLock, TSize >::fAdd_NoReplace, set< Key, Compare >::find(), map_checker< Container >::find(), CCache< TKey, TValue, THandler, TLock, TSize >::m_CacheMap, CCache< TKey, TValue, THandler, TLock, TSize >::m_CacheSet, CCache< TKey, TValue, THandler, TLock, TSize >::m_Lock, and CCache< TKey, TValue, THandler, TLock, TSize >::x_EraseElement().

template<class TKey, class TValue, class THandler, class TLock, class TSize>
CCache< TKey, TValue, THandler, TLock, TSize >::CCache TSizeType  capacity,
THandler *  handler = new THandler()
[inherited]
 

Create cache object with the given capacity.

Definition at line 326 of file ncbi_cache.hpp.

References _ASSERT, CCache< TKey, TValue, THandler, TLock, TSize >::m_Handler, and auto_ptr< X >::reset().

template<class TKey, class TValue, class THandler, class TLock, class TSize>
CCache< TKey, TValue, THandler, TLock, TSize >::TValueType CCache< TKey, TValue, THandler, TLock, TSize >::Get const TKeyType key,
TGetFlags  get_flags = 0,
EGetResult result = NULL
[inherited]
 

Get an object from the cache by its key.

Depending on flags create and cache a new value if the key is not found. If the flags do not allow creating new elements, throws an exception.

Parameters:
key Element key
get_flags Flags to control element retrieval
result pointer to a variable to store the result code to.
Returns:
The value referenced by the key or a new value created by the handler's CreateValue().

Definition at line 589 of file ncbi_cache.hpp.

References CCache< TKey, TValue, THandler, TLock, TSize >::eGet_Found, map_checker< Container >::end(), CCache< TKey, TValue, THandler, TLock, TSize >::fGet_NoTouch, map_checker< Container >::find(), CCache< TKey, TValue, THandler, TLock, TSize >::m_CacheMap, CCache< TKey, TValue, THandler, TLock, TSize >::m_Lock, and CCache< TKey, TValue, THandler, TLock, TSize >::x_UpdateElement().

template<class TKey, class TValue, class THandler, class TLock, class TSize>
CCache< TKey, TValue, THandler, TLock, TSize >::TValueType CCache< TKey, TValue, THandler, TLock, TSize >::operator[] const TKeyType key  )  [inherited]
 

Get cache element by the key.

If the key is not cached yet, the handler's CreateValue() will be called to create one and the new element will be stored in the cache.

See also:
Get()

Definition at line 572 of file ncbi_cache.hpp.

References map_checker< Container >::end(), map_checker< Container >::find(), CCache< TKey, TValue, THandler, TLock, TSize >::m_CacheMap, CCache< TKey, TValue, THandler, TLock, TSize >::m_Lock, and CCache< TKey, TValue, THandler, TLock, TSize >::x_UpdateElement().

template<class TKey, class TValue, class THandler, class TLock, class TSize>
bool CCache< TKey, TValue, THandler, TLock, TSize >::Remove const TKeyType key  )  [inherited]
 

Remove element from cache. Do nothing if the key is not cached.

Definition at line 630 of file ncbi_cache.hpp.

References map_checker< Container >::end(), map_checker< Container >::find(), CCache< TKey, TValue, THandler, TLock, TSize >::m_CacheMap, and CCache< TKey, TValue, THandler, TLock, TSize >::m_Lock.

template<class TKey, class TValue, class THandler, class TLock, class TSize>
void CCache< TKey, TValue, THandler, TLock, TSize >::SetCapacity TSizeType  new_capacity  )  [inherited]
 

Set new capacity of the cache.

The number of elements in the cache may be reduced to match the new capacity.

Parameters:
new_capacity new cache capacity, must be > 0.

Definition at line 645 of file ncbi_cache.hpp.

References CCache< TKey, TValue, THandler, TLock, TSize >::GetSize(), CCache< TKey, TValue, THandler, TLock, TSize >::m_Capacity, CCache< TKey, TValue, THandler, TLock, TSize >::m_Lock, NCBI_THROW, and CCache< TKey, TValue, THandler, TLock, TSize >::x_EraseLast().

template<class TKey, class TValue, class THandler, class TLock, class TSize>
void CCache< TKey, TValue, THandler, TLock, TSize >::SetSize TSizeType  new_size  )  [inherited]
 

Truncate the cache leaving at most new_size elements.

Does not affect cache capacity. If new_size is zero all elements will be removed.

Definition at line 660 of file ncbi_cache.hpp.

References CCache< TKey, TValue, THandler, TLock, TSize >::GetSize(), CCache< TKey, TValue, THandler, TLock, TSize >::m_Lock, and CCache< TKey, TValue, THandler, TLock, TSize >::x_EraseLast().

template<class TKey, class TValue, class THandler, class TLock, class TSize>
void CCache< TKey, TValue, THandler, TLock, TSize >::x_EraseElement TCacheSet_I set_iter,
TCacheMap_I map_iter
[private, inherited]
 

Definition at line 349 of file ncbi_cache.hpp.

References _ASSERT, map_checker< Container >::end(), set< Key, Compare >::end(), set< Key, Compare >::erase(), map_checker< Container >::erase(), CCache< TKey, TValue, THandler, TLock, TSize >::m_CacheMap, CCache< TKey, TValue, THandler, TLock, TSize >::m_CacheSet, and CCache< TKey, TValue, THandler, TLock, TSize >::m_Handler.

Referenced by CCache< TKey, TValue, THandler, TLock, TSize >::Add(), and CCache< TKey, TValue, THandler, TLock, TSize >::x_EraseLast().

template<class TKey, class TValue, class THandler, class TLock, class TSize>
void CCache< TKey, TValue, THandler, TLock, TSize >::x_EraseLast void   )  [private, inherited]
 

Definition at line 365 of file ncbi_cache.hpp.

References _ASSERT, set< Key, Compare >::begin(), set< Key, Compare >::empty(), map_checker< Container >::find(), CCache< TKey, TValue, THandler, TLock, TSize >::m_CacheMap, CCache< TKey, TValue, THandler, TLock, TSize >::m_CacheSet, and CCache< TKey, TValue, THandler, TLock, TSize >::x_EraseElement().

Referenced by CCache< TKey, TValue, THandler, TLock, TSize >::SetCapacity(), CCache< TKey, TValue, THandler, TLock, TSize >::SetSize(), and CCache< TKey, TValue, THandler, TLock, TSize >::~CCache().

template<class TKey, class TValue, class THandler, class TLock, class TSize>
CCache< TKey, TValue, THandler, TLock, TSize >::TOrder CCache< TKey, TValue, THandler, TLock, TSize >::x_GetNextCounter void   )  [private, inherited]
 

Definition at line 419 of file ncbi_cache.hpp.

References CCache< TKey, TValue, THandler, TLock, TSize >::m_Counter, and CCache< TKey, TValue, THandler, TLock, TSize >::x_PackElementIndex().

Referenced by CCache< TKey, TValue, THandler, TLock, TSize >::x_InsertElement(), and CCache< TKey, TValue, THandler, TLock, TSize >::x_UpdateElement().

template<class TKey, class TValue, class THandler, class TLock, class TSize>
CCache< TKey, TValue, THandler, TLock, TSize >::TCacheElement * CCache< TKey, TValue, THandler, TLock, TSize >::x_InsertElement const TKeyType key,
TWeight  weight
[private, inherited]
 

Definition at line 376 of file ncbi_cache.hpp.

References set< Key, Compare >::insert(), CCache< TKey, TValue, THandler, TLock, TSize >::m_CacheSet, NCBI_THROW, CCache< TKey, TValue, THandler, TLock, TSize >::x_GetBaseWeight(), CCache< TKey, TValue, THandler, TLock, TSize >::x_GetNextCounter(), and CCache< TKey, TValue, THandler, TLock, TSize >::x_PackElementIndex().

template<class TKey, class TValue, class THandler, class TLock, class TSize>
void CCache< TKey, TValue, THandler, TLock, TSize >::x_PackElementIndex void   )  [private, inherited]
 

Definition at line 429 of file ncbi_cache.hpp.

References set< Key, Compare >::begin(), set< Key, Compare >::empty(), ITERATE, CCache< TKey, TValue, THandler, TLock, TSize >::m_CacheSet, CCache< TKey, TValue, THandler, TLock, TSize >::m_Counter, SCacheElement< TKey, TSize >::m_Order, SCacheElement< TKey, TSize >::m_Weight, and NCBI_THROW.

Referenced by CCache< TKey, TValue, THandler, TLock, TSize >::x_GetNextCounter(), CCache< TKey, TValue, THandler, TLock, TSize >::x_InsertElement(), and CCache< TKey, TValue, THandler, TLock, TSize >::x_UpdateElement().

template<class TKey, class TValue, class THandler, class TLock, class TSize>
void CCache< TKey, TValue, THandler, TLock, TSize >::x_UpdateElement TCacheElement elem  )  [private, inherited]
 

Definition at line 400 of file ncbi_cache.hpp.

References _ASSERT, set< Key, Compare >::end(), set< Key, Compare >::erase(), set< Key, Compare >::find(), set< Key, Compare >::insert(), CCache< TKey, TValue, THandler, TLock, TSize >::m_CacheSet, SCacheElement< TKey, TSize >::m_Order, SCacheElement< TKey, TSize >::m_Weight, CCache< TKey, TValue, THandler, TLock, TSize >::x_GetNextCounter(), and CCache< TKey, TValue, THandler, TLock, TSize >::x_PackElementIndex().

Referenced by CCache< TKey, TValue, THandler, TLock, TSize >::Get(), and CCache< TKey, TValue, THandler, TLock, TSize >::operator[]().

template<class TKey, class TValue, class THandler, class TLock, class TSize>
CCache< TKey, TValue, THandler, TLock, TSize >::~CCache void   )  [inherited]
 

Definition at line 337 of file ncbi_cache.hpp.

References set< Key, Compare >::empty(), CCache< TKey, TValue, THandler, TLock, TSize >::m_CacheSet, CCache< TKey, TValue, THandler, TLock, TSize >::m_Lock, and CCache< TKey, TValue, THandler, TLock, TSize >::x_EraseLast().


Generated on Wed Dec 9 08:14:11 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Wed Dec 09 08:20:18 2009 by modify_doxy.py rev. 173732