Skip to content

Commit

Permalink
Merge pull request #10 from moka-rs/async-sleep
Browse files Browse the repository at this point in the history
Switch the sleep call in the async cache driver back to an async timer
  • Loading branch information
tatsuya6502 authored May 6, 2023
2 parents c1c6c07 + d7571eb commit 792f71e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ pub(crate) fn sleep_thread_for_insertion(config: &Config) {
}
}

pub(crate) async fn sleep_task_for_insertion(config: &Config) {
if let Some(delay) = config.insertion_delay {
async_io::Timer::after(delay).await;
}
}

const HASH_SEED_KEY: u64 = 982922761776577566;

#[derive(Clone, Default)]
Expand Down
14 changes: 7 additions & 7 deletions src/cache/moka_driver/async_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<I> MokaAsyncCache<I> {

async fn insert(&self, key: usize, req_id: usize) {
let value = cache::make_value(&self.config, key, req_id);
cache::sleep_thread_for_insertion(&self.config);
cache::sleep_task_for_insertion(&self.config).await;
self.cache.insert(key, value).await;
}
}
Expand Down Expand Up @@ -281,7 +281,7 @@ impl GetWith {
async fn get_with(&self, key: usize, req_id: usize, is_inserted: Arc<AtomicBool>) {
self.cache
.get_with(key, async {
cache::sleep_thread_for_insertion(&self.config);
cache::sleep_task_for_insertion(&self.config).await;
is_inserted.store(true, Ordering::Release);
cache::make_value(&self.config, key, req_id)
})
Expand All @@ -299,7 +299,7 @@ impl GetWith {
InitClosureType::GetOrTryInsertWithError1 => self
.cache
.try_get_with(key, async {
cache::sleep_thread_for_insertion(&self.config);
cache::sleep_task_for_insertion(&self.config).await;
is_inserted.store(true, Ordering::Release);
Ok(cache::make_value(&self.config, key, req_id)) as Result<_, InitClosureError1>
})
Expand All @@ -308,7 +308,7 @@ impl GetWith {
InitClosureType::GetOrTyyInsertWithError2 => self
.cache
.try_get_with(key, async {
cache::sleep_thread_for_insertion(&self.config);
cache::sleep_task_for_insertion(&self.config).await;
is_inserted.store(true, Ordering::Release);
Ok(cache::make_value(&self.config, key, req_id)) as Result<_, InitClosureError2>
})
Expand Down Expand Up @@ -368,7 +368,7 @@ mod entry_api {
self.cache
.entry(key)
.or_insert_with(async {
cache::sleep_thread_for_insertion(&self.config);
cache::sleep_task_for_insertion(&self.config).await;
cache::make_value(&self.config, key, req_id)
})
.await
Expand All @@ -386,7 +386,7 @@ mod entry_api {
.cache
.entry(key)
.or_try_insert_with(async {
cache::sleep_thread_for_insertion(&self.config);
cache::sleep_task_for_insertion(&self.config).await;
Ok(cache::make_value(&self.config, key, req_id))
as Result<_, InitClosureError1>
})
Expand All @@ -397,7 +397,7 @@ mod entry_api {
.cache
.entry(key)
.or_try_insert_with(async {
cache::sleep_thread_for_insertion(&self.config);
cache::sleep_task_for_insertion(&self.config).await;
Ok(cache::make_value(&self.config, key, req_id))
as Result<_, InitClosureError2>
})
Expand Down

0 comments on commit 792f71e

Please sign in to comment.