#lru-cache #thread-safe #weight #size #object

multicache

An LRU cache where each object has a given weight

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

#128 in Caching

Download history 577/week @ 2023-12-06 590/week @ 2023-12-13 372/week @ 2023-12-20 471/week @ 2023-12-27 427/week @ 2024-01-03 595/week @ 2024-01-10 542/week @ 2024-01-17 423/week @ 2024-01-24 598/week @ 2024-01-31 514/week @ 2024-02-07 1361/week @ 2024-02-14 1771/week @ 2024-02-21 747/week @ 2024-02-28 578/week @ 2024-03-06 585/week @ 2024-03-13 683/week @ 2024-03-20

2,919 downloads per month
Used in 12 crates (2 directly)

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

~49KB