Comments (4)
Ahh, thanks, my mistake
from moka.
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.
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.
Closing this issue. Please reopen if needed.
from moka.
Related Issues (20)
- Cache with max capacity of zero should not cache anything
- Proposal: support insert key with expire time? HOT 16
- CI: Disable external CI services on temporary merge-queue branches?
- Statistics for metrics HOT 3
- `get_with` deadlock HOT 13
- `.blocking().invalidate(key)` doesn't trigger eviction listener HOT 1
- RUSTSEC-2020-0168: mach is unmaintained HOT 2
- Can the eviction strategy be configured? HOT 8
- Upgrade the cache policy from TinyLFU to W-TinyLFU
- Bump the MSRV to 1.60
- optionally_try_get_with in moka::future::Cache HOT 1
- feature request: remove method HOT 2
- Use `std::thread::available_parallelism()` instead of `num_cpus` dependency
- Eviction listener not always called. HOT 2
- Cache housekeeper can panic (seen after 50M+ insertions) HOT 4
- CI: Miri test causes a complie error with Rust nightly-2023-05-27 HOT 2
- Implement Serialize trait for debugging webserver? HOT 2
- Data race found by `miri test` HOT 9
- CI: Enable Miri tests on `moka::cht::*` modules
- Memory corruption observed when using Moka v0.9.6 HOT 15
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moka.