#cache #memory #local #in-memory #key-value #memory-cache #kept

memory-cache-rs

为 Rust 简单的本地内存缓存

4 个版本

0.2.0 2020 年 2 月 5 日
0.1.2 2020 年 1 月 31 日
0.1.1 2020 年 1 月 29 日
0.1.0 2020 年 1 月 28 日

内存管理 中排名第 269

Download history 47/week @ 2023-12-11 15/week @ 2023-12-18 13/week @ 2023-12-25 15/week @ 2024-01-01 107/week @ 2024-01-08 15/week @ 2024-01-15 8/week @ 2024-01-22 45/week @ 2024-01-29 73/week @ 2024-02-05 46/week @ 2024-02-12 79/week @ 2024-02-19 19/week @ 2024-02-26 31/week @ 2024-03-04 17/week @ 2024-03-11 32/week @ 2024-03-18 13/week @ 2024-03-25

每月下载量 94
2 crates 中使用

MIT 许可证

18KB
203

memory-cache-rs

Rust 提供简单的本地内存缓存。

  1. 示例
  2. 缓存
  3. 重大变更
  4. 许可证

示例

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));

缓存

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);

重大变更

0.2.0:

构造函数

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

重命名方法

看起来像 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

}

参数/结果类型变更

MemoryCache<A, B> {

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

}

许可证

MIT

依赖

~49KB