Skip to content

nymphbox/slabcache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Slabcache

Slabcache is a simple LRU cache with slab allocation. It wraps slab-alloc with LRU eviction policy and maintains some simple statistics for bare-bones observability.

Slab allocators allocate memory in fixed size chunks ahead of time. Thus this library might be useful if you need to cache a fixed number of objects of the same type without incurring allocations at runtime, i.e. in performance critical scenarios where allocation has too much overhead. The metadata storage is pre-allocated as well, and thus all allocations happens at cache initialization. If the cache is ever at capacity, it will evict the LRU element.

Features

  • Least Recently Used (LRU) cache eviction policy.
  • Slab allocation for efficient memory usage.
  • Statistics tracking for cache hits, misses, and current size.

Installation

Add this to your Cargo.toml:

[dependencies]
slabcache = "0.1.0"

Usage

use slabcache::Cache;

fn main() {
    let mut cache = Cache::new(2);

    cache.insert("key1", "value1");
    cache.insert("key2", "value2");

    assert_eq!(cache.get("key1"), Some(&"value1"));
    assert_eq!(cache.get("key2"), Some(&"value2"));
    assert_eq!(cache.get("key3"), None);

    cache.insert("key3", "value3");

    // "key1" should be evicted because the cache capacity is 2
    assert_eq!(cache.get("key1"), None);
    assert_eq!(cache.get("key2"), Some(&"value2"));
    assert_eq!(cache.get("key3"), Some(&"value3"));
}

About

a simple in-memory LRU cache with a slab allocator

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages