https://github.com/fitzgen/associative-cache
A generic, fixed-size, associative cache
https://github.com/fitzgen/associative-cache
Last synced: about 1 year ago
JSON representation
A generic, fixed-size, associative cache
- Host: GitHub
- URL: https://github.com/fitzgen/associative-cache
- Owner: fitzgen
- Created: 2019-09-18T22:35:57.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2024-11-08T18:35:57.000Z (over 1 year ago)
- Last Synced: 2025-04-04T03:13:21.527Z (over 1 year ago)
- Language: Rust
- Homepage: https://docs.rs/associative-cache
- Size: 66.4 KB
- Stars: 40
- Watchers: 3
- Forks: 5
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `associative_cache`
**A generic, fixed-size, associative cache data structure mapping `K` keys to
`V` values.**
[](https://docs.rs/associative-cache/)
[](https://crates.io/crates/associative-cache)
[](https://crates.io/crates/associative-cache)
[](https://github.com/fitzgen/associative-cache/actions/workflows/rust.yml)
## 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
```rust
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`s.
let cache = AssociativeCache::<
*mut usize,
Vec,
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,
Capacity128,
HashEightWay,
LruReplacement,
>::default();
```