cached 0.3.1

Simple function caching/memoization

cached Build Status crates.io docs

simple rust caching macro

Easy to use caching inspired by python decorators.

Documentation

See examples for example of implementing a custom cache-store.

Usage

#[macro_use] extern crate cached;
// `cached!` macro requires the `lazy_static!` macro
#[macro_use] extern crate lazy_static;

use std::time::Duration;
use std::thread::sleep;

use cached::SizedCache;


cached!{ SLOW: SizedCache = SizedCache::with_capacity(50); >>
slow(n: u32) -> () = {
    if n == 0 { return; }
    sleep(Duration::new(1, 0));
    slow(n-1)
}}

pub fn main() {
    slow(10);
    slow(10);
    {
        use cached::Cached;  // must be in scope to access cache
        let cache = SLOW.lock().unwrap();
        println!("hits: {:?}", cache.cache_hits());
        println!("misses: {:?}", cache.cache_misses());
        // make sure the cache-lock is dropped
    }
}