extended caching framework with configurable thread safeness

This commit is contained in:
Peter Schojer
2008-09-30 06:49:22 +00:00
parent 358797c89e
commit b5c357599c
10 changed files with 66 additions and 33 deletions

View File

@@ -56,15 +56,15 @@
namespace Poco {
template <class TKey, class TValue, class TStrategy>
template <class TKey, class TValue, class TStrategy, class TMutex = FastMutex, class TEventMutex = FastMutex>
class AbstractCache
/// An AbstractCache is the interface of all caches.
{
public:
FIFOEvent<const KeyValueArgs<TKey, TValue > > Add;
FIFOEvent<const TKey> Remove;
FIFOEvent<const TKey> Get;
FIFOEvent<const EventArgs> Clear;
FIFOEvent<const KeyValueArgs<TKey, TValue >, TEventMutex > Add;
FIFOEvent<const TKey, TEventMutex> Remove;
FIFOEvent<const TKey, TEventMutex> Get;
FIFOEvent<const EventArgs, TEventMutex> Clear;
typedef std::map<TKey, SharedPtr<TValue > > DataHolder;
typedef typename DataHolder::iterator Iterator;
@@ -90,7 +90,7 @@ public:
/// Adds the key value pair to the cache.
/// If for the key already an entry exists, it will be overwritten.
{
FastMutex::ScopedLock lock(_mutex);
typename TMutex::ScopedLock lock(_mutex);
doAdd(key, val);
}
@@ -98,7 +98,7 @@ public:
/// 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.
{
FastMutex::ScopedLock lock(_mutex);
typename TMutex::ScopedLock lock(_mutex);
doAdd(key, val);
}
@@ -106,7 +106,7 @@ public:
/// Removes an entry from the cache. If the entry is not found,
/// the remove is ignored.
{
FastMutex::ScopedLock lock(_mutex);
typename TMutex::ScopedLock lock(_mutex);
Iterator it = _data.find(key);
doRemove(it);
}
@@ -114,7 +114,7 @@ public:
bool has(const TKey& key) const
/// Returns true if the cache contains a value for the key.
{
FastMutex::ScopedLock lock(_mutex);
typename TMutex::ScopedLock lock(_mutex);
return doHas(key);
}
@@ -123,21 +123,21 @@ public:
/// even when cache replacement removes the element.
/// If for the key no value exists, an empty SharedPtr is returned.
{
FastMutex::ScopedLock lock(_mutex);
typename TMutex::ScopedLock lock(_mutex);
return doGet (key);
}
void clear()
/// Removes all elements from the cache.
{
FastMutex::ScopedLock lock(_mutex);
typename TMutex::ScopedLock lock(_mutex);
doClear();
}
std::size_t size()
/// Returns the number of cached elements
{
FastMutex::ScopedLock lock(_mutex);
typename TMutex::ScopedLock lock(_mutex);
doReplace();
return _data.size();
}
@@ -149,14 +149,14 @@ public:
/// 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.
{
FastMutex::ScopedLock lock(_mutex);
typename TMutex::ScopedLock lock(_mutex);
doReplace();
}
std::set<TKey> getAllKeys()
/// Returns a copy of all keys stored in the cache
{
FastMutex::ScopedLock lock(_mutex);
typename TMutex::ScopedLock lock(_mutex);
doReplace();
ConstIterator it = _data.begin();
ConstIterator itEnd = _data.end();
@@ -301,7 +301,7 @@ protected:
TStrategy _strategy;
mutable DataHolder _data;
mutable FastMutex _mutex;
mutable TMutex _mutex;
private:
AbstractCache(const AbstractCache& aCache);

View File

@@ -47,8 +47,13 @@
namespace Poco {
template <class TKey, class TValue>
class AccessExpireCache: public AbstractCache<TKey, TValue, AccessExpireStrategy<TKey, TValue> >
template <
class TKey,
class TValue,
class TMutex = FastMutex,
class TEventMutex = FastMutex
>
class AccessExpireCache: public AbstractCache<TKey, TValue, AccessExpireStrategy<TKey, TValue>, TMutex, TEventMutex >
/// An AccessExpireCache caches entries for a fixed time period (per default 10 minutes).
/// Entries expire when they are not accessed with get() during this time period. Each access resets
/// the start time for expiration.

View File

@@ -51,9 +51,11 @@ namespace Poco {
template <
class TKey,
class TValue
class TValue,
class TMutex = FastMutex,
class TEventMutex = FastMutex
>
class AccessExpireLRUCache: public AbstractCache<TKey, TValue, StrategyCollection<TKey, TValue> >
class AccessExpireLRUCache: public AbstractCache<TKey, TValue, StrategyCollection<TKey, TValue>, TMutex, TEventMutex>
/// An AccessExpireLRUCache combines LRU caching and time based expire caching.
/// It cache entries for a fixed time period (per default 10 minutes)
/// but also limits the size of the cache (per default: 1024).

View File

@@ -47,8 +47,13 @@
namespace Poco {
template <class TKey, class TValue>
class ExpireCache: public AbstractCache<TKey, TValue, ExpireStrategy<TKey, TValue> >
template <
class TKey,
class TValue,
class TMutex = FastMutex,
class TEventMutex = FastMutex
>
class ExpireCache: public AbstractCache<TKey, TValue, ExpireStrategy<TKey, TValue>, TMutex, TEventMutex >
/// An ExpireCache caches entries for a fixed time period (per default 10 minutes).
/// Entries expire independently of the access pattern, i.e. after a constant time.
/// If you require your objects to expire after they were not accessed for a given time

View File

@@ -51,9 +51,11 @@ namespace Poco {
template <
class TKey,
class TValue
class TValue,
class TMutex = FastMutex,
class TEventMutex = FastMutex
>
class ExpireLRUCache: public AbstractCache<TKey, TValue, StrategyCollection<TKey, TValue> >
class ExpireLRUCache: public AbstractCache<TKey, TValue, StrategyCollection<TKey, TValue>, TMutex, TEventMutex >
/// An ExpireLRUCache combines LRU caching and time based expire caching.
/// It cache entries for a fixed time period (per default 10 minutes)
/// but also limits the size of the cache (per default: 1024).

View File

@@ -47,8 +47,13 @@
namespace Poco {
template <class TKey, class TValue>
class LRUCache: public AbstractCache<TKey, TValue, LRUStrategy<TKey, TValue> >
template <
class TKey,
class TValue,
class TMutex = FastMutex,
class TEventMutex = FastMutex
>
class LRUCache: public AbstractCache<TKey, TValue, LRUStrategy<TKey, TValue>, TMutex, TEventMutex >
/// An LRUCache implements Least Recently Used caching. The default size for a cache is 1024 entries
{
public:

View File

@@ -47,8 +47,13 @@
namespace Poco {
template <class TKey, class TValue>
class UniqueAccessExpireCache: public AbstractCache<TKey, TValue, UniqueAccessExpireStrategy<TKey, TValue> >
template <
class TKey,
class TValue,
class TMutex = FastMutex,
class TEventMutex = FastMutex
>
class UniqueAccessExpireCache: public AbstractCache<TKey, TValue, UniqueAccessExpireStrategy<TKey, TValue>, TMutex, TEventMutex >
/// 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.

View File

@@ -51,9 +51,11 @@ namespace Poco {
template <
class TKey,
class TValue
class TValue,
class TMutex = FastMutex,
class TEventMutex = FastMutex
>
class UniqueAccessExpireLRUCache: public AbstractCache<TKey, TValue, StrategyCollection<TKey, TValue> >
class UniqueAccessExpireLRUCache: public AbstractCache<TKey, TValue, StrategyCollection<TKey, TValue>, TMutex, TEventMutex >
/// A UniqueAccessExpireLRUCache combines LRU caching and time based per entry expire caching.
/// One can define for each cache entry a seperate timepoint
/// but also limit the size of the cache (per default: 1024).

View File

@@ -47,8 +47,13 @@
namespace Poco {
template <class TKey, class TValue>
class UniqueExpireCache: public AbstractCache<TKey, TValue, UniqueExpireStrategy<TKey, TValue> >
template <
class TKey,
class TValue,
class TMutex = FastMutex,
class TEventMutex = FastMutex
>
class UniqueExpireCache: public AbstractCache<TKey, TValue, UniqueExpireStrategy<TKey, TValue>, TMutex, TEventMutex >
/// An UniqueExpireCache caches entries for a given time amount. In contrast
/// to ExpireCache which only allows to set a per cache expiration value, it allows to define
/// expiration per CacheEntry.

View File

@@ -51,9 +51,11 @@ namespace Poco {
template <
class TKey,
class TValue
class TValue,
class TMutex = FastMutex,
class TEventMutex = FastMutex
>
class UniqueExpireLRUCache: public AbstractCache<TKey, TValue, StrategyCollection<TKey, TValue> >
class UniqueExpireLRUCache: public AbstractCache<TKey, TValue, StrategyCollection<TKey, TValue>, TMutex, TEventMutex >
/// A UniqueExpireLRUCache combines LRU caching and time based per entry expire caching.
/// One can define for each cache entry a seperate timepoint
/// but also limit the size of the cache (per default: 1024).