#lru-cache #lru

concurrent_lru

A concurrent LRU cache

2 unstable releases

0.2.0 Feb 14, 2021
0.1.0 Feb 14, 2021

#453 in Concurrency

Download history 2028/week @ 2024-07-25 1434/week @ 2024-08-01 1280/week @ 2024-08-08 1663/week @ 2024-08-15 1987/week @ 2024-08-22 2169/week @ 2024-08-29 1494/week @ 2024-09-05 1509/week @ 2024-09-12 1414/week @ 2024-09-19 1548/week @ 2024-09-26 1695/week @ 2024-10-03 2226/week @ 2024-10-10 854/week @ 2024-10-17 301/week @ 2024-10-24 440/week @ 2024-10-31 310/week @ 2024-11-07

2,234 downloads per month
Used in 6 crates (2 directly)

MIT license

35KB
739 lines

Concurrent LRU

crates.io Badge docs.rs Badge License Badge

An implementation of a concurrent LRU cache. It is designed to hold heavyweight resources, e.g. file descriptors, disk pages. The implementation is heavily influenced by the LRU cache in LevelDB.

Currently there are two implementations, unsharded and sharded.

  • unsharded is a linked hashmap protected by a big lock.
  • sharded shards unsharded by key, providing better performance under contention.

Example

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(())
}

Contribution

Contributions are welcome! Please fork the library, push changes to your fork, and send a pull request. All contributions are shared under an MIT license unless explicitly stated otherwise in the pull request.

Performance

TODO

Dependencies

~47KB