# `cleep` is a Clocksweep Cache implementation in Rust
[](https://github.com/mirrajabi/cleep/actions)
[](https://crates.io/crates/cleep)
[](https://docs.rs/cleep)
Use it to keep your hot juicy records in memory for fast access.
## Usage
```bash
cargo add cleep
```
### Single Threaded example
```rust
use cleep::ClockCache;
let mut cache = ClockCache::<usize, usize, _, 4>::new(|key| fetcher(key));
let v = cache.get(&123);
```
### Thread safe example
```rust
use std::sync::Arc;
use std::thread;
use cleep::ConcurrentClockCache;
const SHARD_CAP: usize = 64;
const SHARDS: usize = 16;
let cache = Arc::new(ConcurrentClockCache::<usize, usize, _, SHARD_CAP, SHARDS>::new(
mock_fetcher,
));
let mut handles = vec![];
let n_threads = thread::available_parallelism().unwrap().get();
for t in 0..n_threads {
let cache_ref = Arc::clone(&cache);
handles.push(thread::spawn(move || {
for i in 0..100_000 {
let key = (i + t * 10) % 500;
let val = cache_ref.get(&key);
assert_eq!(val, mock_fetcher(&key));
}
}));
}
for handle in handles {
handle.join().unwrap();
}
```
### Limitations
The cache capacity in single-threaded implementation and the shard size and shard capacity in the concurrent version must
be a power of two. This is so that we can avoid branching and modulo operations as much as possible on the hot paths and
rely on bitwise operations.
### License
This project is dual-licensed under the MIT and Apache-2.0 licenses. You can choose either license for your use.