UT_LimitedCache Class Reference

A memory limited cache. More...

#include <UT_LimitedCache.h>

Inheritance diagram for UT_LimitedCache:

UT_Cache

List of all members.

Classes

class  Lookup
class  ut_KeyCompare
class  ut_StorageTable

Public Member Functions

 UT_LimitedCache (const char *cache_name, int64 size_in_mb=32)
 ~UT_LimitedCache ()
int64 entries () const
 Find out the number of entries being stored in the cache.
int64 lru_entries () const
 Find out the number of entries being stored in the cache.
void clearCache ()
 Clear all elements out of the cache.
Lookup findItem (const UT_LimitedCacheKey &key)
Lookup addItem (const UT_LimitedCacheKey &key, UT_LimitedCacheItem *item)
void deallocateItem (const UT_LimitedCacheKey &key)
void dumpCache () const
 Debug and dump the cache -- this may not work properly in threaded code.
Lookup operator() (const UT_LimitedCacheKey &key)
Lookup operator[] (const UT_LimitedCacheKey &key)
virtual const char * utGetCacheName () const
virtual int64 utGetCurrentSize ()
virtual bool utHasMaxSize () const
virtual int64 utGetMaxSize ()
virtual void utSetMaxSize (int64 bytes)
virtual int64 utReduceCacheSizeBy (int64 amount)

Protected Types

typedef UT_ConcurrentHashMap
< UT_LimitedCacheKey
*, UT_LimitedCacheItem
*, ut_KeyCompare
ut_StorageMap_t

Friends

class Lookup
typedef
ut_StorageMap_t::const_iterator 
unsafe_traverser
unsafe_traverser unsafe_begin () const
unsafe_traverser unsafe_end () const


Detailed Description

A memory limited cache.

The memory limited cache allows you to put objects into the cache. The cache will automatically throw objects away based on memory usage. You can put as many objects into the cache as you want, but don't count on always being able to find them in the cache since they may get thrown out.

Example:

   class Item : public UT_LimitedCacheItem {
   public:
              Item() : UT_LimitedCacheItem() {}
     virtual ~Item();
     Data    myData;         // Data in item
   };
   class Key : public UT_LimitedCacheKey {
     Key(const char *str)    { myString.harden(str); }
     Key(const Key &key)     { myString.harden(key.myString); }
     
     virtual UT_LimitedCacheKey      *duplicate() const
                                         { return new Key(*this); }
     virtual unsigned        getHash() const
                             {
                                 return myString.hash();
                             }
     virtual bool            isEqual(const UT_LimitedCacheKey &src) const
                             {
                                 const Key &k = static_cast<Key &>(src);
                                 return k.myString == myString;
                             }
     virtual int64           getTotalMemory() const
                             {
                                 return myString.getMemoryUsage() }
                                         sizeof(Item) +
                                         sizeof(*this);
                             }
   };

   static bool
   testLookup(const UT_LimitedCache &cache, const char *str)
   {
     UT_LimitedCache::Lookup         found;

     found = cache[Key(str)];
     if (found.isValid())
         processItem(found.getItem());
   }
    
   testCache(const UT_StringArray &strings)
   {
     UT_LimitedCache cache("Test-Cache", 8); // An 8 MB cache
     for (int i = 0; i < strings.entries(); i++)
         cache.addItem(Key(strings(i)), new Item());
     testLookup("foo");
     testLookup("bar");
   }

Definition at line 133 of file UT_LimitedCache.h.


Member Typedef Documentation

typedef ut_StorageMap_t::const_iterator UT_LimitedCache::unsafe_traverser

Thread unsafe traversal of the cache contents

Definition at line 306 of file UT_LimitedCache.h.

Definition at line 292 of file UT_LimitedCache.h.


Constructor & Destructor Documentation

UT_LimitedCache::UT_LimitedCache ( const char *  cache_name,
int64  size_in_mb = 32 
)

UT_LimitedCache::~UT_LimitedCache (  ) 


Member Function Documentation

Lookup UT_LimitedCache::addItem ( const UT_LimitedCacheKey key,
UT_LimitedCacheItem item 
)

Add an item to the cache. Due to concurrency, the lookup info returned may not contain the object added. The item passed in may be deleted by this method and should not be used once passed in.

Warning:
The item passed in may be deleted. The value in the Lookup should be used.

void UT_LimitedCache::clearCache (  ) 

Clear all elements out of the cache.

void UT_LimitedCache::deallocateItem ( const UT_LimitedCacheKey key  ) 

Forcibly delete an item from the cache. This will delete the item from the LRU cache and the table.

void UT_LimitedCache::dumpCache (  )  const

Debug and dump the cache -- this may not work properly in threaded code.

int64 UT_LimitedCache::entries ( void   )  const [inline]

Find out the number of entries being stored in the cache.

Definition at line 231 of file UT_LimitedCache.h.

Lookup UT_LimitedCache::findItem ( const UT_LimitedCacheKey key  ) 

Find an item in the cache. If there's nothing found, the lookup info returned will be empty.

int64 UT_LimitedCache::lru_entries (  )  const [inline]

Find out the number of entries being stored in the cache.

Definition at line 234 of file UT_LimitedCache.h.

Lookup UT_LimitedCache::operator() ( const UT_LimitedCacheKey key  )  [inline]

Convenience array operators to lookup objects based on the key

Definition at line 224 of file UT_LimitedCache.h.

Lookup UT_LimitedCache::operator[] ( const UT_LimitedCacheKey key  )  [inline]

Convenience array operators to lookup objects based on the key

Definition at line 226 of file UT_LimitedCache.h.

unsafe_traverser UT_LimitedCache::unsafe_begin (  )  const [inline]

Thread unsafe traversal of the cache contents

Definition at line 307 of file UT_LimitedCache.h.

unsafe_traverser UT_LimitedCache::unsafe_end (  )  const [inline]

Thread unsafe traversal of the cache contents

Definition at line 308 of file UT_LimitedCache.h.

virtual const char* UT_LimitedCache::utGetCacheName (  )  const [inline, virtual]

API defined on UT_Cache

Implements UT_Cache.

Definition at line 258 of file UT_LimitedCache.h.

virtual int64 UT_LimitedCache::utGetCurrentSize (  )  [inline, virtual]

API defined on UT_Cache

Implements UT_Cache.

Definition at line 260 of file UT_LimitedCache.h.

virtual int64 UT_LimitedCache::utGetMaxSize (  )  [inline, virtual]

API defined on UT_Cache

Reimplemented from UT_Cache.

Definition at line 264 of file UT_LimitedCache.h.

virtual bool UT_LimitedCache::utHasMaxSize (  )  const [inline, virtual]

API defined on UT_Cache

Reimplemented from UT_Cache.

Definition at line 262 of file UT_LimitedCache.h.

virtual int64 UT_LimitedCache::utReduceCacheSizeBy ( int64  amount  )  [inline, virtual]

API defined on UT_Cache

Implements UT_Cache.

Definition at line 268 of file UT_LimitedCache.h.

virtual void UT_LimitedCache::utSetMaxSize ( int64  bytes  )  [inline, virtual]

API defined on UT_Cache

Reimplemented from UT_Cache.

Definition at line 266 of file UT_LimitedCache.h.


Friends And Related Function Documentation

friend class Lookup [friend]

Definition at line 321 of file UT_LimitedCache.h.


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

Generated on Mon Jan 28 00:30:11 2013 for HDK by  doxygen 1.5.9