Giter Club home page Giter Club logo

Comments (4)

dannygale avatar dannygale commented on June 29, 2024 1

Ahh, thanks, my mistake

from moka.

tatsuya6502 avatar tatsuya6502 commented on June 29, 2024

Thank you for reporting the issue. I am a bit busy this week, and I am hoping I can look after this in next week (the week of Dec 19, 2022). Sorry for the inconvenience.

from moka.

tatsuya6502 avatar tatsuya6502 commented on June 29, 2024

Ah, I believe this is the expected behavior. When futures::future::ok is called, its argument (the async function init) has been already resolved because of the await.

This code fragment will illustrate the behavior:

use futures::future;
use std::time::Duration;

async fn init(value: u32) -> u32 {
    println!("init triggered with {}", value);
    tokio::time::sleep(Duration::from_millis(1000)).await;
    println!("init ran to completion");
    value
}

#[tokio::main]
async fn main() {
    // We do not do `.await` on the  `_future`, but the init future
    // will be resolved. This is because `ok` expects a `u32` value
    // as the argument, rather than `impl Future<Output=u32>`.
    let _future = future::ok::<_, u32>(init(2).await);
    let _future = future::ok::<_, u32>(init(3).await);
}

Output:

init triggered with 2
init ran to completion
init triggered with 3
init ran to completion

You can fix the example by using async block instead of futures::future::ok:

use moka::future::Cache;
use std::convert::Infallible;
use std::hash::Hash;
use std::path::PathBuf;
use std::time::Duration;

async fn init<K: Hash + Eq + Send + Sync + Clone + 'static>(
    cache: Cache<K, u32>,
    key: &K,
    value: u32,
) -> u32 {
    if cache.contains_key(&key) {
        println!("cache contains key but init triggered anyway");
    }
    tokio::time::sleep(Duration::from_millis(1000)).await;
    println!("init ran to completion");
    value
}

fn check_result(result: u32) {
    println!("result: {:?}", &result);
    println!(
        "result was returned from {}",
        if result == 1 {
            "cache"
        } else {
            "init function"
        }
    );
}

#[tokio::main]
async fn main() {
    let cache = Cache::new(100);

    let key = PathBuf::from("asdf");

    cache.insert(key.clone(), 1).await;

    println!("--- checking try_get_with_by_ref ---");

    let result = cache
        .try_get_with_by_ref(&key, async {
            Ok(init(cache.clone(), &key, 2).await) as Result<u32, Infallible>
        })
        .await
        .unwrap();

    check_result(result);

    println!("--- checking try_get_with ---");

    let result = cache
        .try_get_with(key.clone(), async {
            Ok(init(cache.clone(), &key, 3).await) as Result<u32, Infallible>
        })
        .await
        .unwrap();

    check_result(result);

    println!("--- checking get_with ---");

    let result = cache
        .get_with(key.clone(), init(cache.clone(), &key, 4))
        .await;

    check_result(result);
}

Output:

--- checking try_get_with_by_ref ---
result: 1
result was returned from cache
--- checking try_get_with ---
result: 1
result was returned from cache
--- checking get_with ---
result: 1
result was returned from cache

from moka.

tatsuya6502 avatar tatsuya6502 commented on June 29, 2024

Closing this issue. Please reopen if needed.

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.