-
Hi, I'm trying to integrate Foyer in an app I'm building which involves fetching many audio files from a remote location and using them locally. I'm thinking of using Foyer to store these audio files on disk so they are available between sessions, and also keep some in memory for faster access. I'm having trouble to test disk persistence though, it seems I can't get Foyer to actually commit things to disk. Take this example, using Foyer use std::sync::Arc;
use foyer::{DirectFsDeviceOptions, Engine, HybridCache, HybridCacheBuilder};
#[tokio::main]
async fn main() {
let init_cache = || async {
let cache: HybridCache<String, String> = HybridCacheBuilder::new()
.with_flush_on_close(true)
.with_policy(foyer::HybridCachePolicy::WriteOnInsertion)
.memory(10 * 1024 * 1024)
.storage(Engine::Large)
.with_admission_picker(Arc::new(foyer::AdmitAllPicker::default()))
.with_device_options(
DirectFsDeviceOptions::new("./cache").with_capacity(1_000 * 1024 * 1024),
)
.with_flush(true)
.with_recover_mode(foyer::RecoverMode::Strict)
.build()
.await
.unwrap();
return cache;
};
let key = "mykey".to_string();
let value = "value".to_string();
{
let cache = init_cache().await;
let inserted = cache.insert(key.clone(), value.clone());
assert!(*inserted.value() == value);
let entry = cache.get(&key).await.unwrap().unwrap();
assert!(*entry == value);
cache.memory().evict_all();
cache.close().await.unwrap();
}
{
let cache = init_cache().await;
let entry = cache.get(&key).await.unwrap().unwrap(); // <--- fails here with a cache miss
assert!(*entry == value);
cache.close().await.unwrap();
}
} I'm expecting the last assert to work but it always returns |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8000 7 replies
-
Hi, @noomly . Thanks for reporting. While debugging, I printed the hash of the same key between the two runs.
The hash doesn't match each other. According to the document of ahash:
Each time the It is a bug from the foyer side, the default hash shouldn't return an unstable result. I'll fix it. As a temporary solution, please use a seed to fix the hasher when building the memory cache. e.g. .memory(10 * 1024 * 1024)
.with_hash_builder(ahash::RandomState::with_seed(0))
.storage(Engine::Large) |
Beta Was this translation helpful? Give feedback.
-
I've created an issue based on this discussion. FYI: #1011 |
Beta Was this translation helpful? Give feedback.
-
Thank you so much for the quick reply and for the fix! I'll be trying this tomorrow. |
Beta Was this translation helpful? Give feedback.
-
Hi @MrCroxx. Coming back to this, I've tried {
8000
let cache = init_cache().await;
let inserted = cache.insert(key.clone(), value.clone());
assert!(*inserted.value() == value);
let entry = cache.get(&key).await.unwrap().unwrap();
assert!(*entry == value);
cache.memory().evict_all();
cache.close().await.unwrap();
} and then run it again, it panics when we try to retrieve the data: let entry = cache.get(&key).await.unwrap().unwrap(); // <--- fails here with a cache miss Even if the directory |
Beta Was this translation helpful? Give feedback.
Hi, @noomly . Thanks for reporting.
While debugging, I printed the hash of the same key between the two runs.
The hash doesn't match each other.
According to the document of ahash: