HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_CappedCache Class Reference

#include <UT_CappedCache.h>

+ Inheritance diagram for UT_CappedCache:

Public Member Functions

 UT_CappedCache (const char *name, int64 size_in_mb=32)
 
 ~UT_CappedCache ()
 
void clear ()
 Clear the cache. More...
 
exint entries () const
 Number of entries in the cache. More...
 
exint getListEntries () const
 
UT_CappedItemHandle findItem (const UT_CappedKey &key)
 Find an item in the cache. More...
 
UT_CappedItemHandle addItem (const UT_CappedKey &key, const UT_CappedItemHandle &item)
 
void deleteItem (const UT_CappedKey &key)
 Remove an item from the cache. More...
 
template<typename T >
void threadSafeTraversal (T &task)
 
virtual const char * utGetCacheName () const
 
virtual bool utHasMaxSize () const
 
virtual int64 utGetCurrentSize () const
 
virtual int64 utGetMaxSize () const
 
virtual void utSetMaxSize (int64 bytes)
 
virtual int64 utReduceCacheSizeBy (int64 bytes)
 
- Public Member Functions inherited from UT_Cache
 UT_Cache ()
 
virtual ~UT_Cache ()
 
void utClearCache ()
 
virtual bool utHasMinSize () const
 
virtual int64 utGetMinSize () const
 
virtual void utSetMinSize (int64)
 
virtual bool utUpdateCacheInfo ()
 

Additional Inherited Members

- Static Public Member Functions inherited from UT_Cache
static const UT_ValArray
< UT_Cache * > & 
utGetCacheList ()
 get the list of caches More...
 
static void setCacheAddRemoveCB (void(*callback)(void *), void *data)
 callback to be called when a cache is added or deleted. More...
 
static int64 utClearSpaceFromCaches (int64 amount)
 this method attempts to free up 'amount' bytes from all the caches. More...
 

Detailed Description

A thread safe memory limited cache

This class stores a map of UT_CappedItem objects which are queried by UT_CappedKey objects. In addition, the cache is an LRU, memory limited cache. The cache tracks memory used by each UT_CappedItem to keep memory usage limited to the given threshold.

Note
This class conforms to the UT_Cache interface.

Definition at line 67 of file UT_CappedCache.h.

Constructor & Destructor Documentation

UT_CappedCache::UT_CappedCache ( const char *  name,
int64  size_in_mb = 32 
)
UT_CappedCache::~UT_CappedCache ( )

Member Function Documentation

UT_CappedItemHandle UT_CappedCache::addItem ( const UT_CappedKey key,
const UT_CappedItemHandle item 
)

Add an item to the cache. The returned item may not be the item passed as an argument. This may happen if two threads attempt to add the same item at the same time (i.e. the keys are equivalent, but the items may be different).

void UT_CappedCache::clear ( )

Clear the cache.

void UT_CappedCache::deleteItem ( const UT_CappedKey key)

Remove an item from the cache.

exint UT_CappedCache::entries ( void  ) const
inline

Number of entries in the cache.

Definition at line 77 of file UT_CappedCache.h.

UT_CappedItemHandle UT_CappedCache::findItem ( const UT_CappedKey key)

Find an item in the cache.

exint UT_CappedCache::getListEntries ( ) const
inline

Return the number of entries in the list. This should match the entries in the map.

Definition at line 81 of file UT_CappedCache.h.

template<typename T >
void UT_CappedCache::threadSafeTraversal ( T task)
inline

Thread safe iteration over all items in the cache. This method takes a templated class that has a () operator to process entries from the traversal. The () operator should return a bool, true if the iteration should continue, or false if the traversal can stop. For example:

class MemoryCounter
{
public:
MemoryCounter() : myMemory(0) {}
bool operator()(const UT_CappedKeyHandle &key,
const UT_CappedItemHandle &handle)
{
myMemory += item->getMemoryUsage();
return true;
}
int64 getMemoryUsage() const { return myMemory; }
int64 myMemory;
};
static int64 computeStoredMemory(UT_CappedCache &cache)
{
MemoryCounter task;
cache.threadSafeTraversal(task);
return task.getMemory();
}
Note
It is possible to call some methods which alter the map. However, other methods will result in a corrupt iteration.
  • deleteItem
    It is safe to call deleteItem on the current item. However, if deleteItem is called, and your task takes const-references to the keys/items, these may become invalid.
  • findItem addItem
    This may have a side effect of re-ordering the list, meaning iteration will be corrupted.

Definition at line 141 of file UT_CappedCache.h.

virtual const char* UT_CappedCache::utGetCacheName ( ) const
inlinevirtual

API from UT_Cache

Implements UT_Cache.

Definition at line 98 of file UT_CappedCache.h.

virtual int64 UT_CappedCache::utGetCurrentSize ( ) const
inlinevirtual

API from UT_Cache

Implements UT_Cache.

Definition at line 100 of file UT_CappedCache.h.

virtual int64 UT_CappedCache::utGetMaxSize ( ) const
inlinevirtual

API from UT_Cache

Reimplemented from UT_Cache.

Definition at line 101 of file UT_CappedCache.h.

virtual bool UT_CappedCache::utHasMaxSize ( ) const
inlinevirtual

API from UT_Cache

Reimplemented from UT_Cache.

Definition at line 99 of file UT_CappedCache.h.

virtual int64 UT_CappedCache::utReduceCacheSizeBy ( int64  bytes)
virtual

API from UT_Cache

Implements UT_Cache.

virtual void UT_CappedCache::utSetMaxSize ( int64  bytes)
virtual

API from UT_Cache

Reimplemented from UT_Cache.


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