#cache #memory

memory-cache-rs

Simple local in-memory cache for Rust

4 releases

0.2.0 Feb 5, 2020
0.1.2 Jan 31, 2020
0.1.1 Jan 29, 2020
0.1.0 Jan 28, 2020

#105 in Memory management

Download history 79/week @ 2021-07-06 56/week @ 2021-07-13 94/week @ 2021-07-20 97/week @ 2021-07-27 93/week @ 2021-08-03 84/week @ 2021-08-10 61/week @ 2021-08-17 45/week @ 2021-08-24 49/week @ 2021-08-31 97/week @ 2021-09-07 86/week @ 2021-09-14 64/week @ 2021-09-21 99/week @ 2021-09-28 88/week @ 2021-10-05 96/week @ 2021-10-12 145/week @ 2021-10-19

227 downloads per month
Used in ncmapi

MIT license

17KB
203 lines

memory-cache-rs

Simple local in-memory cache for Rust.

  1. Example
  2. Memoization
  3. Breaking Changes
  4. Licence

Example

use std::time::Duration;
use memory_cache::MemoryCache;

let mut cache = MemoryCache::new();

let key: &'static str = "key";
let value: &'static str = "Hello, World!";

// `None` - if the value must be kept forever.
let lifetime = Some(Duration::from_secs(30));

cache.insert(key, value, lifetime);

assert_eq!(cache.get(&key), Some(&value));

Memoization

use once_cell::sync::Lazy;
use std::sync::Mutex;
use memory_cache::{MemoryCache, cached};

cached! {
    fn factorial(x: u128) -> u128 = {
        if x <= 1 {
            1
        } else {
            x * factorial(x - 1)
        }
    }
}

assert_eq!(factorial(21), 51090942171709440000);

Breaking Changes:

0.2.0:

Constructors:

- MemoryCache::new(full_scan_frequency: Duration) -> Self
+ MemoryCache::new() -> Self
+ MemoryCache::with_full_scan(full_scan_frequency: Duration) -> Self

Renamed Methods:

To look like a HashMap.

MemoryCache<A, B> {

-   fn has_key(&self, key: &A) -> bool
+   fn contains_key(&self, key: &A) -> bool

-   fn set(&mut self, key: A, value: B, duration: Option<Duration>) -> Option<B>
+   fn insert(&mut self, key: A, value: B, lifetime: Option<Duration>) -> Option<B>

-   fn get_or_set<F>(&mut self, key: A, factory: F, duration: Option<Duration>) -> &B
+   fn get_or_insert<F>(&mut self, key: A, factory: F, lifetime: Option<Duration>) -> &B

}

Changed types of parameters/results:

MemoryCache<A, B> {

-   fn get_full_scan_frequency(&self) -> &Duration
+   fn get_full_scan_frequency(&self) -> Option<Duration>

}

Licence

MIT

Dependencies

~53KB

ob:^