2 个不稳定版本
0.2.0 | 2021 年 2 月 14 日 |
---|---|
0.1.0 | 2021 年 2 月 14 日 |
#553 在 并发
5,386 每月下载量
用于 4 个 Crates (2 个直接使用)
35KB
739 行
并发 LRU
并发 LRU 缓存的实现。它被设计用来存储重量级资源,例如文件描述符、磁盘页。该实现深受LevelDB 中的 LRU 缓存的影响。
目前有两个实现,unsharded
和 sharded
。
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