2025-05-24 09:33:04 -05:00
2025-05-24 09:22:28 -05:00
2025-05-24 09:29:51 -05:00
2025-05-24 09:33:04 -05:00
2025-05-24 09:29:51 -05:00
2025-05-24 09:29:51 -05:00
2025-05-24 09:33:04 -05:00

LRU

Thread-safe LRU cache implementation with O(1) ops.

Installation

go get git.sharkk.net/Go/LRU

Usage

import "git.sharkk.net/Go/LRU"

// Create cache with capacity 100
cache := lru.NewLRUCache(100)

// Add items
cache.Put("key", "value")
cache.Put(123, struct{Name string}{"example"})

// Get items
value, exists := cache.Get("key")
if exists {
    fmt.Println(value) // "value"
}

// Check size
fmt.Println(cache.Len()) // 2

// Clear cache
cache.Clear()

Features

  • O(1) operations for Get and Put
  • Thread-safe using sync.RWMutex
  • Generic - accepts any key/value types
  • Automatic eviction when capacity exceeded
  • Zero allocations for cache hits

Performance

cpu: 13th Gen Intel(R) Core(TM) i7-1370P
BenchmarkPut-20    26019799    45.93 ns/op    13 B/op    1 allocs/op
BenchmarkGet-20    34455165    35.16 ns/op     0 B/op    0 allocs/op

License

Sharkk Open License

Description
Simple, fast, efficient LRU cache.
Readme 28 KiB
2025-05-24 09:34:22 -05:00
Languages
Go 100%