Skip to content

Manishearth/cached

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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::{Cached, SizedCache}; // trait must be in scope


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);
    {
        let cache = SLOW.lock().unwrap();
        println!("hits: {:?}", cache.cache_hits());
        println!("misses: {:?}", cache.cache_misses());
        // make sure the cache-lock is dropped
    }
}

About

Simple rust caching

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%