Cortex  10.0.0-a4
Public Types | Public Member Functions | Friends | List of all members
IECore::LRUCache< Key, Value, Policy, GetterKey > Class Template Reference

#include <LRUCache.h>

Inherits noncopyable.

Public Types

typedef size_t Cost
 
typedef Key KeyType
 
typedef boost::function< Value(const GetterKey &key, Cost &cost)> GetterFunction
 
typedef boost::function< void(const Key &key, const Value &data)> RemovalCallback
 The optional RemovalCallback is called whenever an item is discarded from the cache.
 

Public Member Functions

 LRUCache (GetterFunction getter)
 
 LRUCache (GetterFunction getter, Cost maxCost)
 
 LRUCache (GetterFunction getter, RemovalCallback removalCallback, Cost maxCost)
 
Value get (const GetterKey &key)
 
bool set (const Key &key, const Value &value, Cost cost)
 
bool cached (const Key &key) const
 
bool erase (const Key &key)
 
void clear ()
 
void setMaxCost (Cost maxCost)
 
Cost getMaxCost () const
 Returns the maximum cost.
 
Cost currentCost () const
 Returns the current cost of all cached items.
 

Friends

class Policy< LRUCache >
 

Detailed Description

template<typename Key, typename Value, template< typename > class Policy = LRUCachePolicy::Parallel, typename GetterKey = Key>
class IECore::LRUCache< Key, Value, Policy, GetterKey >

A mapping from keys to values, where values are computed from keys using a user supplied function. Recently computed values are stored in the cache to accelerate subsequent lookups. Each value has a cost associated with it, and the cache has a maximum total cost above which it will remove the least recently accessed items.

The Value type must be default constructible, copy constructible and assignable. Note that Values are returned by value, and erased by assigning a default constructed value. In practice this means that a smart pointer is the best choice of Value.

The Policy determines the thread safety, eviction and performance characteristics of the cache. See the documentation for each individual policy in the LRUCachePolicy namespace.

The GetterKey may be used where the GetterFunction requires some auxiliary information

in addition to the Key. It must be implicitly castable to Key, and all GetterKeys which yield the same Key must also yield the same results from the GetterFunction.

Member Typedef Documentation

template<typename Key, typename Value, template< typename > class Policy = LRUCachePolicy::Parallel, typename GetterKey = Key>
typedef boost::function<Value ( const GetterKey &key, Cost &cost )> IECore::LRUCache< Key, Value, Policy, GetterKey >::GetterFunction

The GetterFunction is responsible for computing the value and cost for a cache entry when given the key. It should throw a descriptive exception if it can't get the data for any reason.

Member Function Documentation

template<typename Key, typename Value, template< typename > class Policy = LRUCachePolicy::Parallel, typename GetterKey = Key>
bool IECore::LRUCache< Key, Value, Policy, GetterKey >::cached ( const Key &  key) const

Returns true if the object is in the cache. Note that the return value may be invalidated immediately by operations performed by another thread.

template<typename Key, typename Value, template< typename > class Policy = LRUCachePolicy::Parallel, typename GetterKey = Key>
void IECore::LRUCache< Key, Value, Policy, GetterKey >::clear ( )

Erases all cached items. Note that when this returns, the cache may have been repopulated with items if other threads have called set() or get() concurrently.

template<typename Key, typename Value, template< typename > class Policy = LRUCachePolicy::Parallel, typename GetterKey = Key>
bool IECore::LRUCache< Key, Value, Policy, GetterKey >::erase ( const Key &  key)

Erases the item if it was cached. Returns true if it was cached and false if it wasn't cached and therefore wasn't removed.

template<typename Key, typename Value, template< typename > class Policy = LRUCachePolicy::Parallel, typename GetterKey = Key>
Value IECore::LRUCache< Key, Value, Policy, GetterKey >::get ( const GetterKey &  key)

Retrieves an item from the cache, computing it if necessary. The item is returned by value, as it may be removed from the cache at any time by operations on another thread, or may not even be stored in the cache if it exceeds the maximum cost. Throws if the item can not be computed.

template<typename Key, typename Value, template< typename > class Policy = LRUCachePolicy::Parallel, typename GetterKey = Key>
bool IECore::LRUCache< Key, Value, Policy, GetterKey >::set ( const Key &  key,
const Value &  value,
Cost  cost 
)

Adds an item to the cache directly, bypassing the GetterFunction. Returns true for success and false on failure - failure can occur if the cost exceeds the maximum cost for the cache. Note that even when true is returned, the item may be removed from the cache by a subsequent (or concurrent) operation.

template<typename Key, typename Value, template< typename > class Policy = LRUCachePolicy::Parallel, typename GetterKey = Key>
void IECore::LRUCache< Key, Value, Policy, GetterKey >::setMaxCost ( Cost  maxCost)

Sets the maximum cost of the items held in the cache, discarding any items if necessary to meet the new limit.


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