12 releases

0.6.1 Feb 1, 2020
0.6.0 Aug 26, 2019
0.5.0 Aug 17, 2018
0.4.1 Aug 18, 2017
0.1.1 Jan 30, 2017

#44 in Caching

Download history 80/week @ 2020-05-30 11/week @ 2020-06-06 24/week @ 2020-06-13 23/week @ 2020-06-20 15/week @ 2020-06-27 31/week @ 2020-07-04 36/week @ 2020-07-11 11/week @ 2020-07-18 10/week @ 2020-07-25 3/week @ 2020-08-01 64/week @ 2020-08-08 32/week @ 2020-08-15 21/week @ 2020-08-22 29/week @ 2020-08-29 70/week @ 2020-09-05 9/week @ 2020-09-12

173 downloads per month
Used in 2 crates

LGPL-3.0-only

9KB
151 lines

A cache that will keep track of the total size of the elements put in and evict based on that value. The cache is fully thread safe and returns Arc references.

Usage

extern crate multicache;
use multicache::MultiCache;
use std::sync::Arc;

fn main() {
  let cache = MultiCache::new(200);

  cache.put(0, 0, 100);
  cache.put(1, 1, 100);
  cache.put(2, 2, 100);

  assert_eq!(cache.get(0), None);
  assert_eq!(cache.get(1), Some(Arc::new(1)));
  assert_eq!(cache.get(2), Some(Arc::new(2)));
}

Doing a get bumps the value to be the last to be evicted:

extern crate multicache;
use multicache::MultiCache;
use std::sync::Arc;

fn main() {
  let cache = MultiCache::new(200);

  cache.put(0, 0, 100);
  cache.put(1, 1, 100);
  cache.get(0);
  cache.put(2, 2, 100);

  assert_eq!(cache.get(0), Some(Arc::new(0)));
  assert_eq!(cache.get(1), None);
  assert_eq!(cache.get(2), Some(Arc::new(2)));
}

lib.rs:

A cache that will keep track of the total size of the elements put in and evict based on that value. The cache is fully thread safe and returns Arc references.

Example

 extern crate multicache;
 use multicache::MultiCache;
 use std::sync::Arc;

 fn main() {
   let cache = MultiCache::new(200);

   cache.put(0, 0, 100);
   cache.put(1, 1, 100);
   cache.put(2, 2, 100);

   assert_eq!(cache.get(&0), None);
   assert_eq!(cache.get(&1), Some(Arc::new(1)));
   assert_eq!(cache.get(&2), Some(Arc::new(2)));
 }

Doing a get bumps the value to be the last to be evicted:

 extern crate multicache;
 use multicache::MultiCache;
 use std::sync::Arc;

 fn main() {
   let cache = MultiCache::new(200);

   cache.put(0, 0, 100);
   cache.put(1, 1, 100);
   cache.get(&0);
   cache.put(2, 2, 100);

   assert_eq!(cache.get(&0), Some(Arc::new(0)));
   assert_eq!(cache.get(&1), None);
   assert_eq!(cache.get(&2), Some(Arc::new(2)));
 }

Dependencies

~43KB