Skip to content

Commit

Permalink
0.51.3 expiring sized cache improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jaemk committed May 8, 2024
1 parent f30ad47 commit f1cfbc1
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 191 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
## Changed
## Removed

## [0.51.3]
## Added
- `ExpiringSizedCache`: Allow specifying explicit TTL when inserting
## Changed
- Refactor `ExpiringSizedCache` internals to not require tombstones
- `ExpiringSizedCache` keys must impl `Ord`
- `ExpiringSizedCache` `remove` and `insert` updated to return only unexpired values
## Removed

## [0.51.2]
## Added
- Add `get_borrowed` methods to `ExpiringSizedCache` to support cache retrieval using `&str` / `&[T]`
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
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"
Expand Down Expand Up @@ -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"]
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ CACHED_BASIC_EXAMPLES = async_std \
basic_proc_macro \
kitchen_sink \
kitchen_sink_proc_macro \
tokio
tokio \
expiring_sized_cache
# Same as `CACHED_BASIC_EXAMPLES`, but these examples require the `docker/redis`
# goal
CACHED_REDIS_EXAMPLES = redis \
Expand Down
54 changes: 54 additions & 0 deletions examples/expiring_sized_cache.rs
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();
}
}
Loading

0 comments on commit f1cfbc1

Please sign in to comment.