55 lines
963 B
Markdown
55 lines
963 B
Markdown
# LRU
|
|
|
|
Thread-safe LRU cache implementation with O(1) ops.
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
go get git.sharkk.net/Go/LRU
|
|
```
|
|
|
|
## Usage
|
|
|
|
```go
|
|
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](LICENSE.md)
|