#lru-cache #cache #associative #lru #key-value #data-structures #direct-mapped

associative-cache

A generic N-way associative cache with fixed-size capacity and random or least recently used (LRU) replacement

3 stable releases

2.0.0 May 11, 2023
1.0.1 Sep 18, 2019

#44 in Caching

Download history 3502/week @ 2024-03-14 3658/week @ 2024-03-21 2806/week @ 2024-03-28 3508/week @ 2024-04-04 3836/week @ 2024-04-11 4426/week @ 2024-04-18 4202/week @ 2024-04-25 4411/week @ 2024-05-02 15283/week @ 2024-05-09 3333/week @ 2024-05-16 2825/week @ 2024-05-23 2253/week @ 2024-05-30 5169/week @ 2024-06-06 9849/week @ 2024-06-13 4975/week @ 2024-06-20 3328/week @ 2024-06-27

23,763 downloads per month
Used in 55 crates (3 directly)

MIT/Apache

67KB
1K SLoC

associative_cache

A generic, fixed-size, associative cache data structure mapping K keys to V values.

Capacity

The cache has a constant, fixed-size capacity which is controlled by the C type parameter and the Capacity trait. The memory for the cache entries is eagerly allocated once and never resized.

Associativity

The cache can be configured as direct-mapped, two-way associative, four-way associative, etc... via the I type parameter and Indices trait.

Replacement Policy

The cache can be configured to replace the least recently used (LRU) entry, or a random entry via the R type parameter and the Replacement trait.

Examples

use associative_cache::*;

// A two-way associative cache with random replacement mapping
// `String`s to `usize`s.
let cache = AssociativeCache::<
    String,
    usize,
    Capacity512,
    HashTwoWay,
    RandomReplacement
>::default();

// A four-way associative cache with random replacement mapping
// `*mut usize`s to `Vec<u8>`s.
let cache = AssociativeCache::<
    *mut usize,
    Vec<u8>,
    Capacity32,
    PointerFourWay,
    RandomReplacement
>::default();

// An eight-way associative, least recently used (LRU) cache mapping
// `std::path::PathBuf`s to `std::fs::File`s.
let cache = AssociativeCache::<
    std::path::PathBuf,
    WithLruTimestamp<std::fs::File>,
    Capacity128,
    HashEightWay,
    LruReplacement,
>::default();

Dependencies

~72KB