Skip to content

ozedd-ee/lfu-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LFU Cache

A high-performance, thread-safe LFU (Least Frequently Used) cache written in Go.
Supports O(1) time Get and Set operations, TTL-based expiration, automatic cleanup, and customizable eviction callbacks.

🚀 Features

  • O(1) Get/Set operations
  • LFU eviction policy (Least Frequently Used)
  • Global TTL expiration per entry
  • Automatic cleanup loop for stale keys
  • Eviction callback hook for logging or custom actions
  • Thread-safe

📦 Installation

go get github.com/ozedd-ee/lfu

Usage:

package main

import (
	"fmt"
	"time"
	"lfu"
)

func main() {
	cache := lfu.New[string, int](
		3,                    // capacity
		5*time.Minute,        // TTL
		1*time.Minute,        // cleanup interval
		func(k string, v int) { // customizable eviction callback
			fmt.Printf("Evicted: %s = %d\n", k, v)
		},
	)

	cache.Set("a", 100)
	cache.Set("b", 200)

	if val, ok := cache.Get("a"); ok {
		fmt.Println("a =", val)
	}
}

🧪 Running Tests

go test -v 

Run benchmarks

go test -bench=. -benchmem

API

func New[K comparable, V any](
	capacity int,
	ttl time.Duration,
	cleanupInterval time.Duration,
	onEvict EvictionCallback[K, V],
) *LFUCache[K, V]

About

LFU (Least Frequently Used) cache written in Go with O(1) get/set operations

Topics

Resources

License

Stars

Watchers

Forks

Languages