Skip to content

Request: adding an update_with #77

Closed

Description

@nicoxxl

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.

I can send a PR if it's a good idea.

Activity

  1. arthurprs commented on Aug 16, 2025

    @arthurprs
    Owner

    These may be good additions.

    Maybe you could clarify how they could look like?

  2. nicoxxl commented on Aug 17, 2025

    @nicoxxl
    Author

    That is the part where I'm not sure.

    Should it be with a function:

    /// Take a function and update the value in place. It true is returned the operation won’t affect the “hotness” of the entry.
    pub fn update_with<F>(&self, key: Key, updater: F) 
    where
      F: Fn(&mut Val) -> 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.

  3. arthurprs commented on Aug 17, 2025

    @arthurprs
    Owner

    I assume you're using the sync cache? 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 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.

    enum EntryAction {
        Return,
        Remove,
        ReplaceWithGuard,
    }
    
    enum EntryResult<K, V, ..> {
        Value<V>
        Guard<K, V, ...>,
        Timeout,
    }
    
    /// Gets an entry in the cache for in-place/atomic manipulation.
    pub fn entry_mut(&self, key: Key, timeout: Option<Duration>, action: FnOnce(&mut Val) -> EntryAction) -> EntryResult<K, V>;
    
    pub async fn entry_mut_async(&self, key: Key, callback: FnOnce(&mut Val) -> 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.
    pub fn entry(&self, key: Key, timeout: Option<Duration>, action: FnOnce(&Val) -> EntryAction) -> EntryResult<K, V>;
    
    pub async fn entry_async(&self, key: Key, callback: FnOnce(&Val) -> bool) -> Result<V, Guard<K, V>>;
  4. added
    enhancement
    on Oct 10, 2025
  5. added 10 commits that reference this issue on Mar 15, 2026
    f741f39
    c249def
    957482a
    d0e1fa6
    55e7df7
    89b8666
    e8cdb87
    3a08c52
    d6f2c29
    59dfc5e
  6. added a commit that references this issue on Mar 18, 2026
    0a5d380
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions