cleep 0.1.0

Clocksweep Cache implementation in Rust
Documentation
  • Coverage
  • 25%
    7 out of 28 items documented0 out of 23 items with examples
  • Size
  • Source code size: 55.9 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 341.1 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • mirrajabi

cleep is a Clocksweep Cache implementation in Rust

Build Status Crates.io Documentation

Use it to keep your hot juicy records in memory for fast access.

Usage

cargo add cleep

Single Threaded example

use cleep::ClockCache;

let mut cache = ClockCache::<usize, usize, _, 4>::new(|key| fetcher(key));
let v = cache.get(&123);

Thread safe example

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.