GithubHelp home page GithubHelp logo

bytealex / cache-loader-async-rs Goto Github PK

View Code? Open in Web Editor NEW
15.0 2.0 2.0 51 KB

Thread Safe Cache with async loader functions based on tokio-rs

License: Apache License 2.0

Rust 100.00%
rust rust-library rust-libraries cache tokio-rs hacktoberfest

cache-loader-async-rs's People

Contributors

bytealex avatar carols10cents avatar uwemaurer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cache-loader-async-rs's Issues

[question] TtlCacheBacking - SystemTime or Instant?

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.

Feedback!

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.