BDE 4.14.0 Production release
|
#include <bdlcc_cache.h>
Public Types | |
typedef bsl::shared_ptr< VALUE > | ValuePtrType |
Shared pointer type pointing to value type. | |
typedef bsl::function< void(const ValuePtrType &)> | PostEvictionCallback |
Type of function to call after an item has been evicted from the cache. | |
typedef bsl::pair< KEY, ValuePtrType > | KVType |
Value type of a bulk insert entry. | |
Public Member Functions | |
Cache (bslma::Allocator *basicAllocator=0) | |
Cache (CacheEvictionPolicy::Enum evictionPolicy, bsl::size_t lowWatermark, bsl::size_t highWatermark, bslma::Allocator *basicAllocator=0) | |
Cache (CacheEvictionPolicy::Enum evictionPolicy, bsl::size_t lowWatermark, bsl::size_t highWatermark, const HASH &hashFunction, const EQUAL &equalFunction, bslma::Allocator *basicAllocator=0) | |
~Cache ()=default | |
Destroy this object. | |
void | clear () |
int | erase (const KEY &key) |
template<class INPUT_ITERATOR > | |
int | eraseBulk (INPUT_ITERATOR begin, INPUT_ITERATOR end) |
int | eraseBulk (const bsl::vector< KEY > &keys) |
void | insert (const KEY &key, const VALUE &value) |
void | insert (const KEY &key, bslmf::MovableRef< VALUE > value) |
void | insert (bslmf::MovableRef< KEY > key, const VALUE &value) |
void | insert (bslmf::MovableRef< KEY > key, bslmf::MovableRef< VALUE > value) |
void | insert (const KEY &key, const ValuePtrType &valuePtr) |
void | insert (bslmf::MovableRef< KEY > key, const ValuePtrType &valuePtr) |
template<class INPUT_ITERATOR > | |
int | insertBulk (INPUT_ITERATOR begin, INPUT_ITERATOR end) |
int | insertBulk (const bsl::vector< KVType > &data) |
int | insertBulk (bslmf::MovableRef< bsl::vector< KVType > > data) |
int | popFront () |
void | setPostEvictionCallback (const PostEvictionCallback &postEvictionCallback) |
int | tryGetValue (bsl::shared_ptr< VALUE > *value, const KEY &key, bool modifyEvictionQueue=true) |
EQUAL | equalFunction () const |
CacheEvictionPolicy::Enum | evictionPolicy () const |
Return the eviction policy used by this cache. | |
HASH | hashFunction () const |
bsl::size_t | highWatermark () const |
bsl::size_t | lowWatermark () const |
bsl::size_t | size () const |
Return the current size of this cache. | |
template<class VISITOR > | |
void | visit (VISITOR &visitor) const |
Friends | |
class | Cache_TestUtil< KEY, VALUE, HASH, EQUAL > |
This class represents a simple in-process key-value store supporting a variety of eviction policies.
See bdlcc_cache
typedef bsl::pair<KEY, ValuePtrType> bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::KVType |
typedef bsl::function<void(const ValuePtrType&)> bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::PostEvictionCallback |
typedef bsl::shared_ptr<VALUE> bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::ValuePtrType |
|
explicit |
Create an empty LRU cache having no size limit. Optionally specify a basicAllocator
used to supply memory. If basicAllocator
is 0, the currently installed default allocator is used.
bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::Cache | ( | CacheEvictionPolicy::Enum | evictionPolicy, |
bsl::size_t | lowWatermark, | ||
bsl::size_t | highWatermark, | ||
bslma::Allocator * | basicAllocator = 0 |
||
) |
Create an empty cache using the specified evictionPolicy
and the specified lowWatermark
and highWatermark
. Optionally specify the basicAllocator
used to supply memory. If basicAllocator
is 0, the currently installed default allocator is used. The behavior is undefined unless lowWatermark <= highWatermark
, 1 <= lowWatermark
, and 1 <= highWatermark
.
bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::Cache | ( | CacheEvictionPolicy::Enum | evictionPolicy, |
bsl::size_t | lowWatermark, | ||
bsl::size_t | highWatermark, | ||
const HASH & | hashFunction, | ||
const EQUAL & | equalFunction, | ||
bslma::Allocator * | basicAllocator = 0 |
||
) |
Create an empty cache using the specified evictionPolicy
, lowWatermark
, and highWatermark
. The specified hashFunction
is used to generate the hash values for a given key, and the specified equalFunction
is used to determine whether two keys have the same value. Optionally specify the basicAllocator
used to supply memory. If basicAllocator
is 0, the currently installed default allocator is used. The behavior is undefined unless lowWatermark <= highWatermark
, 1 <= lowWatermark
, and 1 <= highWatermark
.
|
default |
void bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::clear | ( | ) |
Remove all items from this cache. Do not invoke the post-eviction callback.
|
inline |
Return (a copy of) the key-equality functor used by this cache that returns true
if two KEY
objects have the same value, and false
otherwise.
int bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::erase | ( | const KEY & | key | ) |
Remove the item having the specified key
from this cache. Invoke the post-eviction callback for the removed item. Return 0 on success and 1 if key
does not exist.
|
inline |
Remove the items having the specified keys
from this cache. Invoke the post-eviction callback for each removed item. Return the number of items successfully removed.
int bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::eraseBulk | ( | INPUT_ITERATOR | begin, |
INPUT_ITERATOR | end | ||
) |
Remove the items having the keys in the specified range [ begin, end )
, from this cache. Invoke the post-eviction callback for each removed item. Return the number of items successfully removed.
|
inline |
|
inline |
Return (a copy of) the unary hash functor used by this cache to generate a hash value (of type std::size_t
) for a KEY
object.
|
inline |
Return the high watermark of this cache, which is the size at which eviction of existing items begins.
void bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::insert | ( | bslmf::MovableRef< KEY > | key, |
bslmf::MovableRef< VALUE > | value | ||
) |
Move the specified key
and its associated value
into this cache. If key
already exists, then its value will be replaced with value
. Note that all the methods that take moved objects provide the basic
but not the strong
exception guarantee – throws may occur after the objects are moved out of; the cache will not be modified, but key
or value
may be changed. Also note that key
must be copyable, even if it is moved.
void bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::insert | ( | bslmf::MovableRef< KEY > | key, |
const VALUE & | value | ||
) |
void bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::insert | ( | bslmf::MovableRef< KEY > | key, |
const ValuePtrType & | valuePtr | ||
) |
Insert the specified key
and its associated valuePtr
into this cache. If key
already exists, then its value will be replaced with value
. Note that the method with key
moved provides the basic
but not the strong
exception guarantee – if a throw occurs, the cache will not be modified, but key
may be changed. Also note that key
must be copyable, even if it is moved.
void bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::insert | ( | const KEY & | key, |
bslmf::MovableRef< VALUE > | value | ||
) |
|
inline |
|
inline |
int bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::insertBulk | ( | bslmf::MovableRef< bsl::vector< KVType > > | data | ) |
Insert the specified data
(composed of Key-Value pairs) into this cache. If a key already exists, then its value will be replaced with the value. Return the number of items successfully inserted. If an exception occurs during this action, we provide only the basic guarantee - both this cache and data
will be in some valid but unspecified state.
|
inline |
Insert the specified data
(composed of Key-Value pairs) into this cache. If a key already exists, then its value will be replaced with the value. Return the number of items successfully inserted.
int bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::insertBulk | ( | INPUT_ITERATOR | begin, |
INPUT_ITERATOR | end | ||
) |
Insert the specified range of Key-Value pairs specified by [ begin, end )
into this cache. If a key already exists, then its value will be replaced with the value. Return the number of items successfully inserted.
|
inline |
Return the low watermark of this cache, which is the size at which eviction of existing items ends.
|
inline |
Remove the item at the front of the eviction queue. Invoke the post-eviction callback for the removed item. Return 0 on success, and 1 if this cache is empty.
void bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::setPostEvictionCallback | ( | const PostEvictionCallback & | postEvictionCallback | ) |
Set the post-eviction callback to the specified postEvictionCallback
. The post-eviction callback is invoked for each item evicted or removed from this cache.
|
inline |
int bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::tryGetValue | ( | bsl::shared_ptr< VALUE > * | value, |
const KEY & | key, | ||
bool | modifyEvictionQueue = true |
||
) |
Load, into the specified value
, the value associated with the specified key
in this cache. If the optionally specified modifyEvictionQueue
is true
and the eviction policy is LRU, then move the cached item to the back of the eviction queue. Return 0 on success, and 1 if key
does not exist in this cache. Note that a write lock is acquired only if this queue is modified.
void bdlcc::Cache< KEY, VALUE, HASH, EQUAL >::visit | ( | VISITOR & | visitor | ) | const |
Call the specified visitor
for every item stored in this cache in the order of the eviction queue until visitor
returns false
. The VISITOR
type must be a callable object that can be invoked in the same way as the function bool (const KEY&, const VALUE&)
|
friend |