template class Poco::AbstractCache

Overview

An AbstractCache is the interface of all caches. More…

#include <AbstractCache.h>

template <
    class TKey,
    class TValue,
    class TStrategy,
    class TMutex = FastMutex,
    class TEventMutex = FastMutex
    >
class AbstractCache
{
public:
    // typedefs

    typedef std::map<TKey, SharedPtr<TValue>> DataHolder;
    typedef DataHolder::iterator Iterator;
    typedef DataHolder::const_iterator ConstIterator;
    typedef std::set<TKey> KeySet;

    // fields

    FIFOEvent<const KeyValueArgs<TKey, TValue>, TEventMutex> Add;
    FIFOEvent<const KeyValueArgs<TKey, TValue>, TEventMutex> Update;
    FIFOEvent<const TKey, TEventMutex> Remove;
    FIFOEvent<const TKey, TEventMutex> Get;
    FIFOEvent<const EventArgs, TEventMutex> Clear;

    // construction

    AbstractCache();
    AbstractCache(const TStrategy& strat);

    // methods

    void
    add(
        const TKey& key,
        const TValue& val
        );

    void
    update(
        const TKey& key,
        const TValue& val
        );

    void
    add(
        const TKey& key,
        SharedPtr<TValue> val
        );

    void
    update(
        const TKey& key,
        SharedPtr<TValue> val
        );

    void
    remove(const TKey& key);

    bool
    has(const TKey& key) const;

    SharedPtr<TValue>
    get(const TKey& key);

    void
    clear();

    std::size_t
    size();

    void
    forceReplace();

    std::set<TKey>
    getAllKeys();

protected:
    // fields

    FIFOEvent<ValidArgs<TKey>> IsValid;
    FIFOEvent<KeySet> Replace;
    TStrategy _strategy;
    DataHolder _data;
    TMutex _mutex;

    // methods

    void
    initialize();

    void
    uninitialize();

    void
    doAdd(
        const TKey& key,
        const TValue& val
        );

    void
    doAdd(
        const TKey& key,
        SharedPtr<TValue>& val
        );

    void
    doUpdate(
        const TKey& key,
        const TValue& val
        );

    void
    doUpdate(
        const TKey& key,
        SharedPtr<TValue>& val
        );

    void
    doRemove(Iterator it);

    bool
    doHas(const TKey& key) const;

    SharedPtr<TValue>
    doGet(const TKey& key);

    void
    doClear();

    void
    doReplace();
};

// direct descendants

template <
    class TKey,
    class TValue,
    class TMutex = FastMutex,
    class TEventMutex = FastMutex
    >
class AccessExpireCache;

template <
    class TKey,
    class TValue,
    class TMutex = FastMutex,
    class TEventMutex = FastMutex
    >
class AccessExpireLRUCache;

template <
    class TKey,
    class TValue,
    class TMutex = FastMutex,
    class TEventMutex = FastMutex
    >
class ExpireCache;

template <
    class TKey,
    class TValue,
    class TMutex = FastMutex,
    class TEventMutex = FastMutex
    >
class ExpireLRUCache;

template <
    class TKey,
    class TValue,
    class TMutex = FastMutex,
    class TEventMutex = FastMutex
    >
class LRUCache;

template <
    class TKey,
    class TValue,
    class TMutex = FastMutex,
    class TEventMutex = FastMutex
    >
class UniqueAccessExpireCache;

template <
    class TKey,
    class TValue,
    class TMutex = FastMutex,
    class TEventMutex = FastMutex
    >
class UniqueAccessExpireLRUCache;

template <
    class TKey,
    class TValue,
    class TMutex = FastMutex,
    class TEventMutex = FastMutex
    >
class UniqueExpireCache;

template <
    class TKey,
    class TValue,
    class TMutex = FastMutex,
    class TEventMutex = FastMutex
    >
class UniqueExpireLRUCache;

Detailed Documentation

An AbstractCache is the interface of all caches.

Methods

void
add(
    const TKey& key,
    const TValue& val
    )

Adds the key value pair to the cache.

If for the key already an entry exists, it will be overwritten.

void
update(
    const TKey& key,
    const TValue& val
    )

Adds the key value pair to the cache.

Note that adding a NULL SharedPtr will fail! If for the key already an entry exists, it will be overwritten. The difference to add is that no remove or add events are thrown in this case, just a simply silent update is performed If the key doesnot exist the behavior is equal to add, ie. an add event is thrown

void
add(
    const TKey& key,
    SharedPtr<TValue> val
    )

Adds the key value pair to the cache.

Note that adding a NULL SharedPtr will fail! If for the key already an entry exists, it will be overwritten, ie. first a remove event is thrown, then a add event

void
update(
    const TKey& key,
    SharedPtr<TValue> val
    )

Adds the key value pair to the cache.

Note that adding a NULL SharedPtr will fail! If for the key already an entry exists, it will be overwritten. The difference to add is that no remove or add events are thrown in this case, just an Update is thrown If the key doesnot exist the behavior is equal to add, ie. an add event is thrown

void
remove(const TKey& key)

Removes an entry from the cache.

If the entry is not found, the remove is ignored.

bool
has(const TKey& key) const

Returns true if the cache contains a value for the key.

SharedPtr<TValue>
get(const TKey& key)

Returns a SharedPtr of the value.

The SharedPointer will remain valid even when cache replacement removes the element. If for the key no value exists, an empty SharedPtr is returned.

void
clear()

Removes all elements from the cache.

std::size_t
size()

Returns the number of cached elements.

void
forceReplace()

Forces cache replacement.

Note that Poco ‘s cache strategy use for efficiency reason no background thread which periodically triggers cache replacement. Cache Replacement is only started when the cache is modified from outside, i.e. add is called, or when a user tries to access an cache element via get. In some cases, i.e. expire based caching where for a long time no access to the cache happens, it might be desirable to be able to trigger cache replacement manually.

std::set<TKey>
getAllKeys()

Returns a copy of all keys stored in the cache.

void
initialize()

Sets up event registration.

void
uninitialize()

Reverts event registration.

void
doAdd(
    const TKey& key,
    const TValue& val
    )

Adds the key value pair to the cache.

If for the key already an entry exists, it will be overwritten.

void
doAdd(
    const TKey& key,
    SharedPtr<TValue>& val
    )

Adds the key value pair to the cache.

If for the key already an entry exists, it will be overwritten.

void
doUpdate(
    const TKey& key,
    const TValue& val
    )

Adds the key value pair to the cache.

If for the key already an entry exists, it will be overwritten.

void
doUpdate(
    const TKey& key,
    SharedPtr<TValue>& val
    )

Adds the key value pair to the cache.

If for the key already an entry exists, it will be overwritten.

void
doRemove(Iterator it)

Removes an entry from the cache.

If the entry is not found the remove is ignored.

bool
doHas(const TKey& key) const

Returns true if the cache contains a value for the key.

SharedPtr<TValue>
doGet(const TKey& key)

Returns a SharedPtr of the cache entry, returns 0 if for the key no value was found.