-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0.51.3 expiring sized cache improvements
- Loading branch information
Showing
5 changed files
with
250 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "cached" | ||
version = "0.51.2" | ||
version = "0.51.3" | ||
authors = ["James Kominick <[email protected]>"] | ||
description = "Generic cache implementations and simplified function memoization" | ||
repository = "https://github.com/jaemk/cached" | ||
|
@@ -135,3 +135,7 @@ required-features = ["async", "proc_macro"] | |
[[example]] | ||
name = "async_std" | ||
required-features = ["async", "proc_macro"] | ||
|
||
[[example]] | ||
name = "expiring_sized_cache" | ||
required-features = ["async_tokio_rt_multi_thread"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use cached::stores::ExpiringSizedCache; | ||
use instant::Instant; | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
use tokio::sync::RwLock; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let mut cache = ExpiringSizedCache::new(20_000); | ||
cache.size_limit(100); | ||
|
||
let cache = Arc::new(RwLock::new(cache)); | ||
|
||
let write_cache = cache.clone(); | ||
let write_handle = tokio::spawn(async move { | ||
for _ in 0..10 { | ||
{ | ||
let mut cache = write_cache.write().await; | ||
cache | ||
.insert("A".to_string(), "A".to_string()) | ||
.expect("write failure"); | ||
println!("[expiring_sized] wrote to cache"); | ||
} | ||
tokio::time::sleep(Duration::from_millis(500)).await; | ||
} | ||
}); | ||
|
||
let mut read_handles = vec![]; | ||
for i in 0..5 { | ||
let reader = i + 1; | ||
let read_cache = cache.clone(); | ||
let read_handle = tokio::spawn(async move { | ||
tokio::time::sleep(Duration::from_millis(100)).await; | ||
let start = Instant::now(); | ||
let mut count = 0; | ||
while Instant::now().duration_since(start) < Duration::from_millis(5_000) { | ||
let cache = read_cache.read().await; | ||
assert_eq!(cache.get_borrowed("A"), Some(&"A".to_string())); | ||
count += 1; | ||
if count % 1_000_000 == 0 { | ||
println!("[expiring_sized] read 1M times in reader {}", reader); | ||
} | ||
} | ||
}); | ||
read_handles.push(read_handle); | ||
} | ||
|
||
write_handle.await.expect("error in write loop"); | ||
for (i, h) in read_handles.into_iter().enumerate() { | ||
h.await | ||
.map_err(|e| format!("error in read handle {}: {:?}", i + 1, e)) | ||
.unwrap(); | ||
} | ||
} |
Oops, something went wrong.