HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 () override
 
 UT_CappedCache (const UT_CappedCache &)=delete
 
UT_CappedCacheoperator= (const UT_CappedCache &)=delete
 
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)
 
const char * utGetCacheName () const override
 
bool utHasMaxSize () const override
 
int64 utGetCurrentSize () const override
 
int64 utGetMaxSize () const override
 
void utSetMaxSize (int64 bytes) override
 
int64 utReduceCacheSizeBy (int64 bytes) override
 
- Public Member Functions inherited from UT_Cache
 UT_Cache ()
 
virtual ~UT_Cache ()
 
 UT_Cache (const UT_Cache &)=delete
 
UT_Cacheoperator= (const UT_Cache &)=delete
 
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 75 of file UT_CappedCache.h.

Constructor & Destructor Documentation

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

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

Number of entries in the cache.

Definition at line 88 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 92 of file UT_CappedCache.h.

UT_CappedCache& UT_CappedCache::operator= ( const UT_CappedCache )
delete
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,
{
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 154 of file UT_CappedCache.h.

const char* UT_CappedCache::utGetCacheName ( ) const
inlineoverridevirtual

API from UT_Cache

Implements UT_Cache.

Definition at line 109 of file UT_CappedCache.h.

int64 UT_CappedCache::utGetCurrentSize ( ) const
inlineoverridevirtual

API from UT_Cache

Implements UT_Cache.

Definition at line 111 of file UT_CappedCache.h.

int64 UT_CappedCache::utGetMaxSize ( ) const
inlineoverridevirtual

API from UT_Cache

Reimplemented from UT_Cache.

Definition at line 113 of file UT_CappedCache.h.

bool UT_CappedCache::utHasMaxSize ( ) const
inlineoverridevirtual

API from UT_Cache

Reimplemented from UT_Cache.

Definition at line 110 of file UT_CappedCache.h.

int64 UT_CappedCache::utReduceCacheSizeBy ( int64  bytes)
overridevirtual

API from UT_Cache

Implements UT_Cache.

void UT_CappedCache::utSetMaxSize ( int64  bytes)
overridevirtual

API from UT_Cache

Reimplemented from UT_Cache.


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