diff --git a/Foundation/include/Poco/AbstractCache.h b/Foundation/include/Poco/AbstractCache.h index 38cf31ba7..0f2e21fc2 100644 --- a/Foundation/include/Poco/AbstractCache.h +++ b/Foundation/include/Poco/AbstractCache.h @@ -1,7 +1,7 @@ // // AbstractCache.h // -// $Id: //poco/svn/Foundation/include/Poco/AbstractCache.h#2 $ +// $Id: //poco/Main/Foundation/include/Poco/AbstractCache.h#16 $ // // Library: Foundation // Package: Cache @@ -62,6 +62,7 @@ class AbstractCache { public: FIFOEvent, TEventMutex > Add; + FIFOEvent, TEventMutex > Update; FIFOEvent Remove; FIFOEvent Get; FIFOEvent Clear; @@ -94,14 +95,37 @@ public: doAdd(key, val); } - void add(const TKey& key, SharedPtr val) + 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 + { + typename TMutex::ScopedLock lock(_mutex); + doUpdate(key, val); + } + + void add(const TKey& key, SharedPtr 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 { typename TMutex::ScopedLock lock(_mutex); doAdd(key, val); } + void update(const TKey& key, SharedPtr 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 + { + typename TMutex::ScopedLock lock(_mutex); + doUpdate(key, val); + } + void remove(const TKey& key) /// Removes an entry from the cache. If the entry is not found, /// the remove is ignored. @@ -175,6 +199,7 @@ protected: /// Sets up event registration. { Add += Delegate >(&_strategy, &TStrategy::onAdd); + Update += Delegate >(&_strategy, &TStrategy::onUpdate); Remove += Delegate(&_strategy, &TStrategy::onRemove); Get += Delegate(&_strategy, &TStrategy::onGet); Clear += Delegate(&_strategy, &TStrategy::onClear); @@ -186,6 +211,7 @@ protected: /// Reverts event registration. { Add -= Delegate >(&_strategy, &TStrategy::onAdd ); + Update -= Delegate >(&_strategy, &TStrategy::onUpdate); Remove -= Delegate(&_strategy, &TStrategy::onRemove); Get -= Delegate(&_strategy, &TStrategy::onGet); Clear -= Delegate(&_strategy, &TStrategy::onClear); @@ -221,6 +247,44 @@ protected: doReplace(); } + 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. + { + KeyValueArgs args(key, val); + Iterator it = _data.find(key); + if (it == _data.end()) + { + Add.notify(this, args); + _data.insert(std::make_pair(key, SharedPtr(new TValue(val)))); + } + else + { + Update.notify(this, args); + } + + doReplace(); + } + + void doUpdate(const TKey& key, SharedPtr& val) + /// Adds the key value pair to the cache. + /// If for the key already an entry exists, it will be overwritten. + { + KeyValueArgs args(key, *val); + Iterator it = _data.find(key); + if (it == _data.end()) + { + Add.notify(this, args); + _data.insert(std::make_pair(key, SharedPtr(new TValue(val)))); + } + else + { + Update.notify(this, args); + } + + doReplace(); + } + void doRemove(Iterator it) /// Removes an entry from the cache. If the entry is not found /// the remove is ignored. diff --git a/Foundation/include/Poco/AbstractStrategy.h b/Foundation/include/Poco/AbstractStrategy.h index a163c6d95..3ecf4d7c9 100644 --- a/Foundation/include/Poco/AbstractStrategy.h +++ b/Foundation/include/Poco/AbstractStrategy.h @@ -1,7 +1,7 @@ // // AbstractStrategy.h // -// $Id: //poco/svn/Foundation/include/Poco/AbstractStrategy.h#2 $ +// $Id: //poco/Main/Foundation/include/Poco/AbstractStrategy.h#4 $ // // Library: Foundation // Package: Cache @@ -60,10 +60,17 @@ public: virtual ~AbstractStrategy() { } + + virtual void onUpdate(const void* pSender, const KeyValueArgs & args) + /// Updates an existing entry. + { + onRemove(pSender,args.key()); + onAdd(pSender, args); + } virtual void onAdd(const void* pSender, const KeyValueArgs & key) = 0; /// Adds the key to the strategy. - /// If for the key already an entry exists, an excpetion will be thrown. + /// If for the key already an entry exists, an exception will be thrown. virtual void onRemove(const void* pSender, const TKey& key) = 0; /// Removes an entry from the strategy. If the entry is not found