mirror of
https://github.com/XRPLF/rippled.git
synced 2026-04-29 15:37:57 +00:00
adding callback into TaggedCache
This commit is contained in:
@@ -161,6 +161,21 @@ public:
|
||||
fetch(key_type const& digest, Handler const& h);
|
||||
// End CachedSLEs functions.
|
||||
|
||||
/** Fetch or create an entry and execute a callback while holding the lock.
|
||||
|
||||
The entry for the given key is fetched from the cache or created if it
|
||||
doesn't exist. The callback is then invoked with a reference to the
|
||||
entry while the cache mutex is still held, allowing safe modification
|
||||
of the cached object.
|
||||
|
||||
@param key The key to fetch or create
|
||||
@param callback Function to call with the entry under lock.
|
||||
Signature: void(T&)
|
||||
*/
|
||||
template <class Callback>
|
||||
void
|
||||
fetch_and_modify(key_type const& key, Callback&& callback);
|
||||
|
||||
private:
|
||||
SharedPointerType
|
||||
initialFetch(key_type const& key, std::lock_guard<mutex_type> const& l);
|
||||
|
||||
@@ -586,6 +586,31 @@ TaggedCache<Key, T, IsKeyCache, SharedWeakUnionPointer, SharedPointerType, Hash,
|
||||
}
|
||||
// End CachedSLEs functions.
|
||||
|
||||
template <
|
||||
class Key,
|
||||
class T,
|
||||
bool IsKeyCache,
|
||||
class SharedWeakUnionPointer,
|
||||
class SharedPointerType,
|
||||
class Hash,
|
||||
class KeyEqual,
|
||||
class Mutex>
|
||||
template <class Callback>
|
||||
inline void
|
||||
TaggedCache<Key, T, IsKeyCache, SharedWeakUnionPointer, SharedPointerType, Hash, KeyEqual, Mutex>::
|
||||
fetch_and_modify(key_type const& key, Callback&& callback)
|
||||
{
|
||||
static_assert(
|
||||
!IsKeyCache, "fetch_and_modify is only supported for value caches, not key-only caches");
|
||||
|
||||
std::lock_guard lock(m_mutex);
|
||||
|
||||
auto entry = std::make_shared<T>();
|
||||
canonicalize(key, entry, []() { return false; });
|
||||
|
||||
callback(*entry);
|
||||
}
|
||||
|
||||
template <
|
||||
class Key,
|
||||
class T,
|
||||
|
||||
Reference in New Issue
Block a user