# Poco

# template < class TKey, class TValue, class TStrategy, class TMutex = FastMutex, class TEventMutex = FastMutex >

# class AbstractCache

Library: Foundation
Package: Cache
Header: Poco/AbstractCache.h

## Description

An AbstractCache is the interface of all caches.

## Member Summary

Member Functions: add, clear, doAdd, doClear, doGet, doHas, doRemove, doReplace, doUpdate, forEach, forceReplace, get, getAllKeys, has, initialize, operator =, remove, size, uninitialize, update

## Types Aliases

### ConstIterator

using ConstIterator = typename DataHolder::const_iterator;

### DataHolder

using DataHolder = std::map < TKey, SharedPtr < TValue >>;

### Iterator

using Iterator = typename DataHolder::iterator;

### KeySet

using KeySet = std::set < TKey >;

## Constructors

### AbstractCache

AbstractCache();

### AbstractCache

AbstractCache(
const TStrategy & strat
);

### AbstractCache

AbstractCache(
const AbstractCache & aCache
) = delete;

## Destructor

### ~AbstractCache

virtual ~AbstractCache();

## Member Functions

### add

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.

### add

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

Adds the key value pair to the cache. Note that adding a nullptr 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

### clear

void clear();

Removes all elements from the cache.

### forEach

template < typename Fn > void forEach(
Fn && fn
) const;

Iterates over all key-value pairs in the cache, using a functor or lambda expression.

The given functor must take the key and value as parameters. Note that the value is passed as the actual value (or reference), not a Poco::SharedPtr.

### forceReplace

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.

### get

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.

### getAllKeys

std::set < TKey > getAllKeys();

Returns a copy of all keys stored in the cache

### has

bool has(
const TKey & key
) const;

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

### operator =

AbstractCache & operator = (
const AbstractCache & aCache
) = delete;

### remove

void remove(
const TKey & key
);

Removes an entry from the cache. If the entry is not found, the remove is ignored.

### size

std::size_t size();

Returns the number of cached elements

### update

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

Adds the key value pair to the cache. Note that adding a nullptr 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 does not exist the behavior is equal to add, ie. an add event is thrown

### update

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

Adds the key value pair to the cache. Note that adding a nullptr 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 does not exist the behavior is equal to add, ie. an add event is thrown

### doAdd

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.

### doAdd

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.

### doClear

void doClear();

### doGet

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

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

### doHas

bool doHas(
const TKey & key
) const;

Returns true if the cache contains a value for the key

### doRemove

void doRemove(
Iterator it
);

Removes an entry from the cache. If the entry is not found the remove is ignored.

### doReplace

void doReplace();

### doUpdate

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.

### doUpdate

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.

### initialize

void initialize();

Sets up event registration.

### uninitialize

void uninitialize();

Reverts event registration.

## Variables

### Add

BasicEvent < const KeyValueArgs < TKey, TValue >, TEventMutex > Add;

### Clear

BasicEvent < const EventArgs, TEventMutex > Clear;

### Get

BasicEvent < const TKey, TEventMutex > Get;

### Remove

BasicEvent < const TKey, TEventMutex > Remove;

### Update

BasicEvent < const KeyValueArgs < TKey, TValue >, TEventMutex > Update;

### IsValid

mutable BasicEvent < ValidArgs < TKey >> IsValid;

### Replace

mutable BasicEvent < KeySet > Replace;

### _data

mutable DataHolder _data;

### _mutex

mutable TMutex _mutex;

### _strategy

TStrategy _strategy;