https://github.com/arindas/generational-cache
Generational Arena based cache impls. in 100% safe, [no_std] compatible Rust.
https://github.com/arindas/generational-cache
cache generational-arena lru-cache no-std
Last synced: 10 months ago
JSON representation
Generational Arena based cache impls. in 100% safe, [no_std] compatible Rust.
- Host: GitHub
- URL: https://github.com/arindas/generational-cache
- Owner: arindas
- License: mit
- Created: 2023-06-07T15:58:27.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-08T06:21:25.000Z (over 2 years ago)
- Last Synced: 2024-08-11T10:08:02.340Z (almost 2 years ago)
- Topics: cache, generational-arena, lru-cache, no-std
- Language: Rust
- Homepage: https://docs.rs/generational-cache/
- Size: 1.46 MB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
generational-cache
Generational Arena based cache impls. in 100% safe, [no_std] compatible Rust.
## Usage
`generational-cache` is a library crate. You may include it in your `Cargo.toml` as follows:
```toml
[dependencies]
generational-cache = "0.2.2"
```
Refer to latest git [API Documentation](https://arindas.github.io/generational-cache/docs/generational_cache/)
or [Crate Documentation](https://docs.rs/generational-cache) for more details.
### Examples
1. LRU Cache (`generational_cache::cache::LRUCache`)
A generational arena based LRU cache implementation.
```rust
#[no_std]
use generational_cache::prelude::*;
const CAPACITY: usize = 3;
// users can choose between different map and vector implementations
let mut cache = LRUCache::<_, i32, u64, AllocBTreeMap<_, _>>::with_backing_vector(Array::<_, CAPACITY>::new());
cache.insert(-1, 1).unwrap();
cache.insert(-2, 2).unwrap();
cache.insert(-3, 3).unwrap();
assert_eq!(cache.least_recent().unwrap(), (&-1, &1));
assert_eq!(cache.most_recent().unwrap(), (&-3, &3));
assert_eq!(cache.insert(-4, 4).unwrap(), Eviction::Block { key: -1, value: 1});
assert_eq!(cache.least_recent().unwrap(), (&-2, &2));
assert_eq!(cache.most_recent().unwrap(), (&-4, &4));
assert_eq!(cache.insert(-2, 42).unwrap(), Eviction::Value(2));
assert_eq!(cache.least_recent().unwrap(), (&-3, &3));
assert_eq!(cache.most_recent().unwrap(), (&-2, &42));
assert_eq!(cache.remove(&-42).unwrap(), Lookup::Miss);
assert_eq!(cache.query(&-42).unwrap(), Lookup::Miss);
assert_eq!(cache.query(&-3).unwrap(), Lookup::Hit(&3));
assert_eq!(cache.least_recent().unwrap(), (&-4, &4));
assert_eq!(cache.most_recent().unwrap(), (&-3, &3));
assert_eq!(cache.remove(&-2).unwrap(), Lookup::Hit(42));
assert_eq!(cache.query(&-2).unwrap(), Lookup::Miss);
// zero capacity LRUCache is unusable
let mut cache = LRUCache::<_, i32, u64, AllocBTreeMap<_, _>>::with_backing_vector(Array::<_, 0_usize>::new());
match cache.insert(0, 0) {
Err(LRUCacheError::ListUnderflow) => {}
_ => unreachable!("Wrong error on list underflow."),
};
```
(… we plan on adding more cache implementations in the future).
## License
This repository is licensed under the MIT License. See
[License](https://raw.githubusercontent.com/arindas/generational-cache/main/LICENSE)
for more details.