template class Poco::UniqueAccessExpireCache

Overview

An UniqueAccessExpireCache caches entries for a given time span. More…

#include <UniqueAccessExpireCache.h>

template <
    class TKey,
    class TValue,
    class TMutex = FastMutex,
    class TEventMutex = FastMutex
    >
class UniqueAccessExpireCache: public Poco::AbstractCache
{
};

Inherited Members

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;

    // 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();

Detailed Documentation

An UniqueAccessExpireCache caches entries for a given time span.

In contrast to ExpireCache which only allows to set a per cache expiration value, it allows to define expiration per CacheEntry. Each TValue object must thus offer the following method:

const Poco::Timespan& getTimeout() const;

which returns the relative timespan for how long the entry should be valid without being accessed! The absolute expire timepoint is calculated as now() + getTimeout(). Accessing an object will update this absolute expire timepoint. You can use the Poco::AccessExpirationDecorator to add the getExpiration method to values that do not have a getExpiration function.

Be careful when using an UniqueAccessExpireCache. A cache is often used like cache.has(x) followed by cache.get x). Note that it could happen that the “has” call works, then the current execution thread gets descheduled, time passes, the entry gets invalid, thus leading to an empty SharedPtr being returned when “get” is invoked.