bytealex / cache-loader-async-rs Goto Github PK
View Code? Open in Web Editor NEWThread Safe Cache with async loader functions based on tokio-rs
License: Apache License 2.0
Thread Safe Cache with async loader functions based on tokio-rs
License: Apache License 2.0
Consider using thiserror
or similar to support usage of generic rust error-handling libraries.
Hi,
I was looking at this crate, evaluating it for my use.
While examining the code of TtlCacheBacking
I've noticed that it's using SystemTime
in order to determine the age of a cached entry.
My question is, why SystemTime
instead of Instance
? Was this due to a problem which you've encountered?
The reason for my questions is a caveat with SystemTime
which each call to SystemTime::now()
translates to a system call (at least on Linux) - a relatively slow operation.
Under heavy load, the affect of this can be significant.
Instant
, on the other hand, does not suffer from the same problem.
Thanks for providing this library!
I couldn't see how to feedback on my experience, but I just wanted to convey that using the library has been very pleasant. Here's a code snippet of how I set up my cache to read secrets from Hashicorp's Vault:
let cache: TtlCache = LoadingCache::with_meta_loader(
TtlCacheBacking::with_backing(
*unauthorized_timeout,
LruCacheBacking::new(max_secrets_cached),
),
move |secret_path| {
let task_state = Arc::clone(&state);
async move {
let result = task_state
.client
.get(
task_state
.server
.join(&format!(
"{}v1/secret/data/{}",
task_state.server, secret_path
))
.unwrap(),
)
.bearer_auth(&task_state.client_token)
.send()
.await;
if let Ok(response) = result {
let secret_reply = if response.status().is_success() {
response.json::<GetSecretReply>().await.ok()
} else {
None
};
let lease_duration = secret_reply.as_ref().map(|sr| {
let mut lease_duration = None;
if let Some(ttl_field) = task_state.ttl_field.as_ref() {
if let Some(ttl) = sr.data.data.get(ttl_field) {
if let Ok(ttl_duration) = ttl.parse::<humantime::Duration>() {
lease_duration = Some(ttl_duration.into());
}
}
}
lease_duration.unwrap_or_else(|| Duration::from_secs(sr.lease_duration))
});
Ok(secret_reply).with_meta(lease_duration.map(TtlMeta::from))
} else {
Err(Error::Unavailable)
}
}
},
);
Feel free to use this as a more (incomplete) complex example if that helps. Thanks once again!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.