Git Product home page Git Product logo

Comments (7)

arthurprs avatar arthurprs commented on May 27, 2024

Yeah, allowing the cache to be overweight might have large implications.

If we ignore that part, I could probably add support for get_or_insert to return a Option<&mut V> kind of thing w/o extra lookups.

from quick-cache.

nagisa avatar nagisa commented on May 27, 2024

Even just insert returning a Result<&mut V, V> would work for pretty much everything in my case. Basically either V is in the cache and I can refer to it that way, or it was not inserted for any reason, in which case I can still grab a reference of the value and do something with it without duplicating the effort to regenerate it.

But that would require not calling on_evict if the item hasn't actually been inserted into the cache.

Thanks for the quick response.

from quick-cache.

arthurprs avatar arthurprs commented on May 27, 2024

I tried to add APIs that would satisfy your use case, but it's elusive to them to co-exists with non-guaranteed admission and the lifecycle hooks. It might take another round of refactors to get it exactly right.

Can you check if

pub fn get_mut_or_insert_with<'a, Q, E>(
or one of the new apis improve your use case?

from quick-cache.

nagisa avatar nagisa commented on May 27, 2024

I think I could implement what I need with get_mut_or_guard. get_mut_or_insert_with, as far as I understand would require me to duplicate the logic acting on the value if I wanted to do something with it regardless of whether the value was able to make it into the cache or not. Something like this perhaps:

let already_used = false;
value = cache.get_mut_or_insert_with(key, || {
    let mut value = generate();
    already_used = true;
    use(&mut value);
    value
});
if !already_used {
    use(&mut value);
}

Not too terrible, though in this particular application it would perhaps be better if get_mut_or_insert_with either invoked the callback XOR returned a reference, not potentially both. That would allow dropping the already_used type of flag.

from quick-cache.

nagisa avatar nagisa commented on May 27, 2024

Thanks for responding so quickly and spending your time on implementing this! Much appreciated!

from quick-cache.

arthurprs avatar arthurprs commented on May 27, 2024

I merged #23 on master now, I'll keep the issue open until I make a crates.io release with it.

from quick-cache.

arthurprs avatar arthurprs commented on May 27, 2024

0.5 has now been published

from quick-cache.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.