https://github.com/arindas/generational-lru
A generational arena based LRU Cache implementation in 100% safe rust.
https://github.com/arindas/generational-lru
cache generational-arena lru-cache
Last synced: 10 months ago
JSON representation
A generational arena based LRU Cache implementation in 100% safe rust.
- Host: GitHub
- URL: https://github.com/arindas/generational-lru
- Owner: arindas
- License: mit
- Created: 2022-06-11T06:48:58.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-06-27T06:33:38.000Z (almost 4 years ago)
- Last Synced: 2024-10-31T11:39:19.859Z (over 1 year ago)
- Topics: cache, generational-arena, lru-cache
- Language: Rust
- Homepage: https://docs.rs/generational-lru/
- Size: 2.64 MB
- Stars: 40
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# generational-lru
[](https://github.com/arindas/generational-lru/actions/workflows/ci.yml)
[](https://codecov.io/gh/arindas/generational-lru)
[](https://github.com/arindas/generational-lru/actions/workflows/rustdoc.yml)
[](https://crates.io/crates/generational-lru)
Crate providing a 100% safe, generational arena based LRU cache implementation.
```rust
use generational_lru::lrucache::{LRUCache, CacheError};
let capacity = 5;
let mut lru_cache = LRUCache::::with_capacity(capacity);
assert_eq!(lru_cache.query(&0), Err(CacheError::CacheMiss));
for ele in 0..capacity {
let x = ele as i32;
assert!(lru_cache.insert(x, x).is_ok());
}
for ele in 0..capacity {
let x = ele as i32;
assert_eq!(lru_cache.query(&x), Ok(&x));
}
let x = capacity as i32;
assert!(lru_cache.insert(x, x).is_ok());
assert_eq!(lru_cache.query(&x), Ok(&x));
assert_eq!(lru_cache.query(&0), Err(CacheError::CacheMiss));
let x = capacity as i32 / 2;
assert_eq!(lru_cache.remove(&x), Ok(x));
assert_eq!(lru_cache.query(&x), Err(CacheError::CacheMiss));
assert_eq!(lru_cache.remove(&x), Err(CacheError::CacheMiss));
// zero capacity LRUCache is unusable
let mut lru_cache = LRUCache::::with_capacity(0);
assert!(matches!(
lru_cache.insert(0, 0),
Err(CacheError::CacheBroken(_))
));
```
Refer to [API documentation](https://arindas.github.io/generational-lru/generational_lru) for more details.
## Usage
This is a library crate. You may include it in your `Cargo.toml` as follows:
```toml
[dependencies]
generational-lru = "0.1"
```
## License
This repository is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.