From b5c357599c6ecb9c5cc8adba4be78fe620c2f988 Mon Sep 17 00:00:00 2001 From: Peter Schojer Date: Tue, 30 Sep 2008 06:49:22 +0000 Subject: [PATCH] extended caching framework with configurable thread safeness --- Foundation/include/Poco/AbstractCache.h | 30 +++++++++---------- Foundation/include/Poco/AccessExpireCache.h | 9 ++++-- .../include/Poco/AccessExpireLRUCache.h | 6 ++-- Foundation/include/Poco/ExpireCache.h | 9 ++++-- Foundation/include/Poco/ExpireLRUCache.h | 6 ++-- Foundation/include/Poco/LRUCache.h | 9 ++++-- .../include/Poco/UniqueAccessExpireCache.h | 9 ++++-- .../include/Poco/UniqueAccessExpireLRUCache.h | 6 ++-- Foundation/include/Poco/UniqueExpireCache.h | 9 ++++-- .../include/Poco/UniqueExpireLRUCache.h | 6 ++-- 10 files changed, 66 insertions(+), 33 deletions(-) diff --git a/Foundation/include/Poco/AbstractCache.h b/Foundation/include/Poco/AbstractCache.h index 1d77548ff..38cf31ba7 100644 --- a/Foundation/include/Poco/AbstractCache.h +++ b/Foundation/include/Poco/AbstractCache.h @@ -56,15 +56,15 @@ namespace Poco { -template +template class AbstractCache /// An AbstractCache is the interface of all caches. { public: - FIFOEvent > Add; - FIFOEvent Remove; - FIFOEvent Get; - FIFOEvent Clear; + FIFOEvent, TEventMutex > Add; + FIFOEvent Remove; + FIFOEvent Get; + FIFOEvent Clear; typedef std::map > 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 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); diff --git a/Foundation/include/Poco/AccessExpireCache.h b/Foundation/include/Poco/AccessExpireCache.h index dee5bdf37..252d9c31a 100644 --- a/Foundation/include/Poco/AccessExpireCache.h +++ b/Foundation/include/Poco/AccessExpireCache.h @@ -47,8 +47,13 @@ namespace Poco { -template -class AccessExpireCache: public AbstractCache > +template < + class TKey, + class TValue, + class TMutex = FastMutex, + class TEventMutex = FastMutex +> +class AccessExpireCache: public AbstractCache, 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. diff --git a/Foundation/include/Poco/AccessExpireLRUCache.h b/Foundation/include/Poco/AccessExpireLRUCache.h index 39f138c54..45625e321 100644 --- a/Foundation/include/Poco/AccessExpireLRUCache.h +++ b/Foundation/include/Poco/AccessExpireLRUCache.h @@ -51,9 +51,11 @@ namespace Poco { template < class TKey, - class TValue + class TValue, + class TMutex = FastMutex, + class TEventMutex = FastMutex > -class AccessExpireLRUCache: public AbstractCache > +class AccessExpireLRUCache: public AbstractCache, 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). diff --git a/Foundation/include/Poco/ExpireCache.h b/Foundation/include/Poco/ExpireCache.h index 9e5e7cb08..09dff4683 100644 --- a/Foundation/include/Poco/ExpireCache.h +++ b/Foundation/include/Poco/ExpireCache.h @@ -47,8 +47,13 @@ namespace Poco { -template -class ExpireCache: public AbstractCache > +template < + class TKey, + class TValue, + class TMutex = FastMutex, + class TEventMutex = FastMutex +> +class ExpireCache: public AbstractCache, 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 diff --git a/Foundation/include/Poco/ExpireLRUCache.h b/Foundation/include/Poco/ExpireLRUCache.h index 0717be9c4..42407d2ff 100644 --- a/Foundation/include/Poco/ExpireLRUCache.h +++ b/Foundation/include/Poco/ExpireLRUCache.h @@ -51,9 +51,11 @@ namespace Poco { template < class TKey, - class TValue + class TValue, + class TMutex = FastMutex, + class TEventMutex = FastMutex > -class ExpireLRUCache: public AbstractCache > +class ExpireLRUCache: public AbstractCache, 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). diff --git a/Foundation/include/Poco/LRUCache.h b/Foundation/include/Poco/LRUCache.h index 1cc1ce07b..be2937c2a 100644 --- a/Foundation/include/Poco/LRUCache.h +++ b/Foundation/include/Poco/LRUCache.h @@ -47,8 +47,13 @@ namespace Poco { -template -class LRUCache: public AbstractCache > +template < + class TKey, + class TValue, + class TMutex = FastMutex, + class TEventMutex = FastMutex +> +class LRUCache: public AbstractCache, TMutex, TEventMutex > /// An LRUCache implements Least Recently Used caching. The default size for a cache is 1024 entries { public: diff --git a/Foundation/include/Poco/UniqueAccessExpireCache.h b/Foundation/include/Poco/UniqueAccessExpireCache.h index 2fe45a38b..f8fe9b4df 100644 --- a/Foundation/include/Poco/UniqueAccessExpireCache.h +++ b/Foundation/include/Poco/UniqueAccessExpireCache.h @@ -47,8 +47,13 @@ namespace Poco { -template -class UniqueAccessExpireCache: public AbstractCache > +template < + class TKey, + class TValue, + class TMutex = FastMutex, + class TEventMutex = FastMutex +> +class UniqueAccessExpireCache: public AbstractCache, 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. diff --git a/Foundation/include/Poco/UniqueAccessExpireLRUCache.h b/Foundation/include/Poco/UniqueAccessExpireLRUCache.h index fb298d226..e34795f77 100644 --- a/Foundation/include/Poco/UniqueAccessExpireLRUCache.h +++ b/Foundation/include/Poco/UniqueAccessExpireLRUCache.h @@ -51,9 +51,11 @@ namespace Poco { template < class TKey, - class TValue + class TValue, + class TMutex = FastMutex, + class TEventMutex = FastMutex > -class UniqueAccessExpireLRUCache: public AbstractCache > +class UniqueAccessExpireLRUCache: public AbstractCache, 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). diff --git a/Foundation/include/Poco/UniqueExpireCache.h b/Foundation/include/Poco/UniqueExpireCache.h index 15e86d1d0..23ec10dfa 100644 --- a/Foundation/include/Poco/UniqueExpireCache.h +++ b/Foundation/include/Poco/UniqueExpireCache.h @@ -47,8 +47,13 @@ namespace Poco { -template -class UniqueExpireCache: public AbstractCache > +template < + class TKey, + class TValue, + class TMutex = FastMutex, + class TEventMutex = FastMutex +> +class UniqueExpireCache: public AbstractCache, 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. diff --git a/Foundation/include/Poco/UniqueExpireLRUCache.h b/Foundation/include/Poco/UniqueExpireLRUCache.h index 01f2ce365..7911bfd0a 100644 --- a/Foundation/include/Poco/UniqueExpireLRUCache.h +++ b/Foundation/include/Poco/UniqueExpireLRUCache.h @@ -51,9 +51,11 @@ namespace Poco { template < class TKey, - class TValue + class TValue, + class TMutex = FastMutex, + class TEventMutex = FastMutex > -class UniqueExpireLRUCache: public AbstractCache > +class UniqueExpireLRUCache: public AbstractCache, 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).