Skip to content

In-memory cache implementation with commonly used LRU, W-LFU and S3-FIFO as the eviction policy

License

Notifications You must be signed in to change notification settings

Jeevananthan-23/ziglang-caches

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ziglang-caches

This is a modern cache implementation, inspired by the following papers, provides high efficiency.

This offers state-of-the-art efficiency and scalability compared to other LRU-based cache algorithms.

Basic usage

[!LRU_Cache] Least recents used cache eviction policy for cache your data in-memory for fast access.

const std = @import("std");
const lru = @import("lru");

const cache = lru.LruCache(.locking, u8, []const u8);

pub fn main() !void {

    // Create a cache backed by DRAM
    var lrucache = try cache.init(std.heap.page_allocator, 4);
    defer lrucache.deinit();

    // Add an object to the cache
    try lrucache.insert(1, "one");
    try lrucache.insert(2, "two");
    try lrucache.insert(3, "three");
    try lrucache.insert(4, "four");

    // Most recently used cache
    std.debug.print("mru: {s} \n", .{lrucache.mru().?.value});

    // least recently used cache
    std.debug.print("lru: {s} \n", .{lrucache.lru().?.value});

    // remove from cache
    _ = lrucache.remove(1);

    // Check if an object is in the cache O/P: false
    std.debug.print("key: 1 exists: {} \n", .{lrucache.contains(1)});
}

🚀 Usage

  1. Add ziglang-caches as a dependency in your build.zig.zon.

    build.zig.zon example
    .{
        .name = "<name_of_your_package>",
        .version = "<version_of_your_package>",
        .dependencies = .{
            .caches = .{
                .url = "https://github.com/jeevananthan-23/ziglang-caches/archive/<git_tag_or_commit_hash>.tar.gz",
                .hash = "<package_hash>",
            },
        },
    }

    Set <package_hash> to 12200000000000000000000000000000000000000000000000000000000000000000, and Zig will provide the correct found value in an error message.

  2. Add lrucache as a module in your build.zig.

    build.zig example
    const lrucache = b.dependency("caches", .{});
    exe.addModule("lrucache", lrucache.module("lrucache"));

About

In-memory cache implementation with commonly used LRU, W-LFU and S3-FIFO as the eviction policy

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages