#caches #associative #lru #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

#31 in Caching

Download history 2905/week @ 2023-11-20 3058/week @ 2023-11-27 2401/week @ 2023-12-04 3154/week @ 2023-12-11 2830/week @ 2023-12-18 1549/week @ 2023-12-25 1971/week @ 2024-01-01 2334/week @ 2024-01-08 3981/week @ 2024-01-15 2191/week @ 2024-01-22 3812/week @ 2024-01-29 2526/week @ 2024-02-05 3245/week @ 2024-02-12 3359/week @ 2024-02-19 2536/week @ 2024-02-26 2417/week @ 2024-03-04

11,790 downloads per month
Used in 57 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

~74KB