Crate timed_cache[][src]

Example

The following example is obviously overly simplistic, but gives an idea of how one might use this library. In a real-world application, one might have the generator function call an over-the-network service to get some session key.

   extern crate timed_cache;

   use timed_cache::TimedCache;

   use std::time::Duration;
   use std::sync::Mutex;

   struct TestService(usize);

   impl TestService {
       fn next(&mut self) -> usize {
           let n = self.0;
           self.0 += 1;
           n
       }
   }

   let time_to_keep = Duration::from_millis(1);
   let mut cache = TimedCache::<String, usize>::with_time_to_keep(time_to_keep);

   let service = Mutex::new(TestService(0));

   let generate_value = || service.lock().unwrap().next();

   (0..1000).for_each(|_| {
       // this generator method will
       let value = cache.get(&"value".to_owned(), generate_value);
       println!("{}", value);
   });

Structs

TimedCache

A collection which stores a value for a set amount of time.