55 releases

Uses old Rust 2015

0.12.5 Oct 7, 2024
0.12.4 Jul 31, 2024
0.12.3 Feb 24, 2024
0.12.1 Nov 22, 2023
0.1.1 Dec 31, 2016

#1 in Caching

Download history 1052578/week @ 2024-08-03 1124481/week @ 2024-08-10 1133809/week @ 2024-08-17 1147729/week @ 2024-08-24 1056949/week @ 2024-08-31 1129304/week @ 2024-09-07 994951/week @ 2024-09-14 1102345/week @ 2024-09-21 1130435/week @ 2024-09-28 1198726/week @ 2024-10-05 1171075/week @ 2024-10-12 1175309/week @ 2024-10-19 1109614/week @ 2024-10-26 989444/week @ 2024-11-02 1029817/week @ 2024-11-09 887640/week @ 2024-11-16

4,221,183 downloads per month
Used in 3,121 crates (377 directly)

MIT license

90KB
1.5K SLoC

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

Dependencies

~1MB
~12K SLoC