Giter Club home page Giter Club logo

Comments (5)

Dessix avatar Dessix commented on July 4, 2024 1

Futures- thanks ^^

from moka.

tatsuya6502 avatar tatsuya6502 commented on July 4, 2024 1

Finished developing and #20 has been merged into the master branch. I am going to release Moka v0.5.0 with this feature in a day or so.

from moka.

tatsuya6502 avatar tatsuya6502 commented on July 4, 2024 1

Published Moka v0.5.0 to crates.io.

from moka.

tatsuya6502 avatar tatsuya6502 commented on July 4, 2024

Thanks for your suggestion. I think this would be a good feature to add. I will work on it once I finish working on another feature (#12).

Which Moka Cache implementation are you going to use? moka::sync::Cache or moka::future::Cache?

from moka.

tatsuya6502 avatar tatsuya6502 commented on July 4, 2024

Hi @Dessix

I created a topic branch and added the following methods to the future cache:

async fn get_or_insert_with(&self, key: K, init: impl Future<Output = V>) -> V
  • This method takes a key and a future to initialize the value.
  • If the key does not exist in the cache and no other get_or_insert_with or get_or_try_insert_with method is already running for the key, it awaits for the future, inserts the value returned from the future to the cache, and returns the value to the caller.
  • If other method is already running (the future is being awaited), it waits for the other method to complete and returns the value.
async fn get_or_try_insert_with<F>(&self, key: K, init: F) -> Result<V, Arc<Box<dyn Error>>>
where
    F: Future<Output = Result<V, Box<dyn Error>>>
  • This method is similar to get_or_insert_with but the future resolves into Result<V, Box<dyn std::error::Error>> instead of V.
  • A value is inserted to the cache only when the future resolves into Ok(V).

Please check the unit tests out as they will show you how to use them (test1 and test2). Let me know what you think.

from moka.

Related Issues (20)

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.