Crate gs_rust_cache

Crate gs_rust_cache 

Source
Expand description

Thread safe cache developed using lru crate as its core. Supports LRU, positive and negative TTLs and miss handler function. It is intended to be used using the function retrieve_or_compute, which will return the value if it is in the cache, or compute it using the miss_handler function if it is not.

§Example

extern crate gs_rust_cache;
use gs_rust_cache::Cache;
use std::any::Any;
use std::time::Duration;

fn miss_handler(key: &i32, data: &mut i32, adhoc_code: &mut u8, _: &[&dyn Any]) -> bool {
   // Your Code Here
   *data = 123;
   *adhoc_code = 200;
   true
}

fn main() {
    let mut cache = Cache::new(
        3,
        miss_handler,
        Duration::from_millis(200),          
        Duration::from_millis(100),          
    );

    let key =  456;
    let (value, adhoc_code, is_hit) = cache.retrieve_or_compute(&key); // first one is calculated
    let (value_1, adhoc_code_1, is_hit_1) = cache.retrieve_or_compute(&key); // afterwards it is retrieved

    assert_eq!(value, value_1);
    assert_eq!(adhoc_code, adhoc_code_1);
    assert!(is_hit); // is_hit is false because the value was computed
    assert!(is_hit_1); // is_hit_1 is true because the value was retrieved from the cache

}

Structs§

Cache

Type Aliases§

MissHandler