9 个版本
0.1.8 | 2024 年 4 月 6 日 |
---|---|
0.1.7 | 2024 年 4 月 1 日 |
0.1.3 | 2024 年 3 月 31 日 |
#122 in 缓存
9KB
153 代码行
如何使用该库的简单示例。
Dashmap cache 是线程安全的,并且可以通过 Arc 在线程之间共享,就像 Dashmap 一样。
use dashmap_cache::DashmapCache;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug)]
struct Repository {}
#[derive(Clone, Serialize, Deserialize, Hash, PartialEq, Eq, Debug)]
pub struct SomeStruct {
pub some_field: String,
pub some_id: u64,
}
#[derive(Clone, Serialize, Deserialize, Hash, PartialEq, Eq, Debug)]
pub struct SomeResult {
pub some_result_field: String,
pub some_result_int: u64,
}
impl Repository {
pub fn c(&self, arg: &SomeStruct) -> SomeResult {
// Emulates a database call
println!("call C with {:?}", arg);
SomeResult {
some_result_field: arg.some_field.clone(),
some_result_int: arg.some_id,
}
}
}
fn main() {
let repo = Repository {};
let dmc: DashmapCache = DashmapCache::new();
let invalidation_keys = vec!["some-key".to_string(), "some-other".to_string()];
let const_arg = SomeStruct {
some_field: "test".to_owned(),
some_id: 1,
};
// Fills cache with a value
println!(
"{}",
dmc.cached(
&invalidation_keys,
|some_struct| repo.c(some_struct),
&const_arg
)
.unwrap()
.some_result_field
);
// Identical call gets memoized;
println!(
"{}",
dmc.cached(
&invalidation_keys,
|some_struct| repo.c(some_struct),
&const_arg
)
.unwrap()
.some_result_field
);
// Until you invalidate one of the associated key
dmc.invalidate("some-key");
println!(
"{}",
dmc.cached(
&invalidation_keys,
|some_struct| repo.c(some_struct),
&const_arg
)
.unwrap()
.some_result_field
);
/*
> cargo run
call C with SomeStruct { some_field: "test", some_id: 1 }
test
test
call C with SomeStruct { some_field: "test", some_id: 1 }
test
*/
}
依赖关系
~1.6–8.5MB
~69K SLoC