#lru-cache #lru #cache

concurrent_lru

并发 LRU 缓存

2 个不稳定版本

0.2.0 2021 年 2 月 14 日
0.1.0 2021 年 2 月 14 日

#553并发

Download history 1301/week @ 2024-03-29 1308/week @ 2024-04-05 867/week @ 2024-04-12 1028/week @ 2024-04-19 1239/week @ 2024-04-26 949/week @ 2024-05-03 782/week @ 2024-05-10 1061/week @ 2024-05-17 1425/week @ 2024-05-24 1545/week @ 2024-05-31 1283/week @ 2024-06-07 1270/week @ 2024-06-14 1099/week @ 2024-06-21 1782/week @ 2024-06-28 1421/week @ 2024-07-05 840/week @ 2024-07-12

5,386 每月下载量
用于 4 个 Crates (2 个直接使用)

MIT 许可证

35KB
739

并发 LRU

crates.io Badge docs.rs Badge License Badge

并发 LRU 缓存的实现。它被设计用来存储重量级资源,例如文件描述符、磁盘页。该实现深受LevelDB 中的 LRU 缓存的影响。

目前有两个实现,unshardedsharded

  • unsharded 是一个由一个大锁保护的链式哈希表。
  • sharded 通过键将 unsharded 分片,在竞争条件下提供更好的性能。

示例

use concurrent_lru::sharded::LruCache;
use std::{fs, io};

fn read(_f: &fs::File) -> io::Result<()> {
    // Maybe some positioned read...
    Ok(())
}

fn main() -> io::Result<()> {
    let cache = LruCache::<String, fs::File>::new(10);

    let foo_handle = cache.get_or_try_init("foo".to_string(), 1, |name| {
        fs::OpenOptions::new().read(true).open(name)
    })?;
    read(foo_handle.value())?;
    drop(foo_handle); // Unpin foo file.

    // Foo is in the cache.
    assert!(cache.get("foo".to_string()).is_some());

    // Evict foo manually.
    cache.prune();
    assert!(cache.get("foo".to_string()).is_none());

    Ok(())
}

贡献

欢迎贡献!请fork 库,将更改推送到您的 fork,并发送一个pull request。除非在 pull request 中明确说明,否则所有贡献都在 MIT 许可证下共享。

性能

待办事项

依赖项

~50KB