You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to add an update_with or replace_with method to the cache.
The use case would be to be able to update atomically an entry with a closure/Fn type (and with the soft argument, don't affect "hotness").
It can be useful in cases where a cache is needed for reading but also can receive updates.
/// Take a function and update the value in place. It true is returned the operation won’t affect the “hotness” of the entry.pubfnupdate_with<F>(&self,key:Key,updater:F)whereF:Fn(&mutVal) -> bool
my_cache.update(player, |p| {
p.score += score_won;true});
On the other side, it might be better to follow the ".entry()" already used in the std::collections::HashMap which should allow for a better API.
Long story short, the current sync cache APIs do not allow for atomic in-place updates. This request is also similar to #73
Ideally, we'd solve your use case, #73, and possibly others, without adding too many new APIs.
Something like this might be achievable, but the soft variant is still missing, and it may create a Guard (for possible insertion) that the caller may or may not want.
enumEntryAction{Return,Remove,ReplaceWithGuard,}enumEntryResult<K,V, ..>{Value<V>
Guard<K,V, ...>,Timeout,}/// Gets an entry in the cache for in-place/atomic manipulation.pubfnentry_mut(&self,key:Key,timeout:Option<Duration>,action:FnOnce(&mutVal) -> EntryAction) -> EntryResult<K,V>;pubasyncfnentry_mut_async(&self,key:Key,callback:FnOnce(&mutVal) -> bool) -> Result<V,Guard<K,V>>;/// Relaxed version of `entry_mut` in case mutable references aren't required./// This has the benefit of allowing values to be returned with `EntryAction::Value` without/// acquiring an exclusive lock internally.pubfnentry(&self,key:Key,timeout:Option<Duration>,action:FnOnce(&Val) -> EntryAction) -> EntryResult<K,V>;pubasyncfnentry_async(&self,key:Key,callback:FnOnce(&Val) -> bool) -> Result<V,Guard<K,V>>;
Activity
arthurprs commentedon Aug 16, 2025
These may be good additions.
Maybe you could clarify how they could look like?
nicoxxl commentedon Aug 17, 2025
That is the part where I'm not sure.
Should it be with a function:
On the other side, it might be better to follow the ".entry()" already used in the
std::collections::HashMapwhich should allow for a better API.arthurprs commentedon Aug 17, 2025
I assume you're using the
synccache? To make sure.There's https://docs.rs/quick_cache/latest/quick_cache/sync/struct.Cache.html#method.get_value_or_guard (and others), but they were built to avoid holding a big lock while the caller(s) are working on producing the value or waiting for it to be produced by another caller.
Long story short, the current
synccache APIs do not allow for atomic in-place updates. This request is also similar to #73Ideally, we'd solve your use case, #73, and possibly others, without adding too many new APIs.
Something like this might be achievable, but the soft variant is still missing, and it may create a Guard (for possible insertion) that the caller may or may not want.
Add entry/entry_async API for atomic cache access
Add entry/entry_async API for atomic cache access
Add entry/entry_async API for atomic cache access
Add entry/entry_async API for atomic cache access
Add entry/entry_async API for atomic cache access
Add entry/entry_async API for atomic cache access
Add entry/entry_async API for atomic cache access
Add entry/entry_async API for atomic cache access
Add entry/entry_async API for atomic cache access
Add entry/entry_async API for atomic cache access
Add entry/entry_async API for atomic cache access (arthurprs#109)