added update message to caching

This commit is contained in:
Peter Schojer
2008-11-27 08:36:36 +00:00
parent 645bd01ec9
commit 41b157725b
2 changed files with 75 additions and 4 deletions

View File

@@ -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<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;
@@ -94,14 +95,37 @@ public:
doAdd(key, val);
}
void add(const TKey& key, SharedPtr<TValue > 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<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, 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<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 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<TStrategy, const KeyValueArgs<TKey, TValue> >(&_strategy, &TStrategy::onAdd);
Update += Delegate<TStrategy, const KeyValueArgs<TKey, TValue> >(&_strategy, &TStrategy::onUpdate);
Remove += Delegate<TStrategy, const TKey>(&_strategy, &TStrategy::onRemove);
Get += Delegate<TStrategy, const TKey>(&_strategy, &TStrategy::onGet);
Clear += Delegate<TStrategy, const EventArgs>(&_strategy, &TStrategy::onClear);
@@ -186,6 +211,7 @@ protected:
/// Reverts event registration.
{
Add -= Delegate<TStrategy, const KeyValueArgs<TKey, TValue> >(&_strategy, &TStrategy::onAdd );
Update -= Delegate<TStrategy, const KeyValueArgs<TKey, TValue> >(&_strategy, &TStrategy::onUpdate);
Remove -= Delegate<TStrategy, const TKey>(&_strategy, &TStrategy::onRemove);
Get -= Delegate<TStrategy, const TKey>(&_strategy, &TStrategy::onGet);
Clear -= Delegate<TStrategy, const EventArgs>(&_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<TKey, TValue> args(key, val);
Iterator it = _data.find(key);
if (it == _data.end())
{
Add.notify(this, args);
_data.insert(std::make_pair(key, SharedPtr<TValue>(new TValue(val))));
}
else
{
Update.notify(this, args);
}
doReplace();
}
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.
{
KeyValueArgs<TKey, TValue> args(key, *val);
Iterator it = _data.find(key);
if (it == _data.end())
{
Add.notify(this, args);
_data.insert(std::make_pair(key, SharedPtr<TValue>(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.

View File

@@ -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
@@ -61,9 +61,16 @@ public:
{
}
virtual void onUpdate(const void* pSender, const KeyValueArgs <TKey, TValue>& args)
/// Updates an existing entry.
{
onRemove(pSender,args.key());
onAdd(pSender, args);
}
virtual void onAdd(const void* pSender, const KeyValueArgs <TKey, TValue>& 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