#expiration #delay #cache #async #type

simple-async-cache-rs

A fast asynchronous caching crate with expiration delay and custom types

9 releases

0.3.3 Oct 11, 2021
0.3.2 Oct 11, 2021
0.2.0 Jul 8, 2021
0.1.3 Jul 8, 2021
0.1.2 Jun 30, 2021

#187 in Caching

MIT license

6KB
93 lines

simple-async-cache-rs

A fast asynchronous caching crate with expiration delay and custom types.

Why would you use this crate ?

  • You can configure expiration delay
  • It supports almost any types / structs
  • It's really simple and written in pure Rust
  • It prevents dogpile effect
  • Its only dependency is tokio.
  • It's thread-safe and fast

Basic example:

use simple_async_cache_rs::AsyncCacheStore;
use std::time::Duration;
use tokio::time::sleep;


#[tokio::main]
async fn main() {
    // Create an AsyncCacheStore using implicit typing with an expiration delay of 10 seconds.
    let mut store = AsyncCacheStore::new(10);

    // If you want to explicitly define types you can do the following:
    // let mut store: Arc<AsyncCacheStore<String, String>> = AsyncCacheStore::new(60);

    let cache = store.get("key_1".to_string()).await;
    let mut result = cache.lock().await;
    match &mut *result {
        Some(_d) => {
            // You can  get here the cached value for key_1 if it is already available.
        }
        None => {
            // There is no existing entry for key_1, you can do any task to get the value.
            // The AsyncCacheStore prevents dogpile effect by itself.
            *result = Some("This is the first value for key_1.");
        }
    }

    // The value for key_1 is still cached.
    assert_eq!(
        *store.get("key_1".to_string()).await.lock().await,
        Some("This is the first value for key_1.")
    );

    // We sleep for 15 seconds, the value for key_1 is expired.
    sleep(Duration::from_secs(15)).await;

    assert_eq!(
        *store.get("key_1".to_string()).await.lock().await,
        None
    );
}

Dependencies

~2–3MB
~46K SLoC