lru

Crates.iolru
lib.rslru
version0.12.5
sourcesrc
created_at2016-12-31 13:49:16.05674
updated_at2024-10-07 13:56:39.162648
descriptionA LRU cache implementation
homepagehttps://github.com/jeromefroe/lru-rs
repositoryhttps://github.com/jeromefroe/lru-rs.git
max_upload_size
id7873
size100,261
Jerome Froelich (jeromefroe)

documentation

https://docs.rs/lru/

README

LRU Cache

Build Badge Codecov Badge crates.io Badge docs.rs Badge License Badge

Documentation

An implementation of a LRU cache. The cache supports put, get, get_mut and pop operations, all of which are O(1). This crate was heavily influenced by the LRU Cache implementation in an earlier version of Rust's std::collections crate.

The MSRV for this crate is 1.64.0.

Example

Below is a simple example of how to instantiate and use a LRU cache.

extern crate lru;

use lru::LruCache;
use std::num::NonZeroUsize;

fn main() {
    let mut cache = LruCache::new(NonZeroUsize::new(2).unwrap());
    cache.put("apple", 3);
    cache.put("banana", 2);

    assert_eq!(*cache.get(&"apple").unwrap(), 3);
    assert_eq!(*cache.get(&"banana").unwrap(), 2);
    assert!(cache.get(&"pear").is_none());

    assert_eq!(cache.put("banana", 4), Some(2));
    assert_eq!(cache.put("pear", 5), None);

    assert_eq!(*cache.get(&"pear").unwrap(), 5);
    assert_eq!(*cache.get(&"banana").unwrap(), 4);
    assert!(cache.get(&"apple").is_none());

    {
        let v = cache.get_mut(&"banana").unwrap();
        *v = 6;
    }

    assert_eq!(*cache.get(&"banana").unwrap(), 6);
}
Commit count: 364

cargo fmt