https://github.com/floatdrop/lru
Thread safe GoLang LRU cache
https://github.com/floatdrop/lru
cache go golang lru lru-cache
Last synced: 7 months ago
JSON representation
Thread safe GoLang LRU cache
- Host: GitHub
- URL: https://github.com/floatdrop/lru
- Owner: floatdrop
- License: mit
- Created: 2022-03-25T07:21:54.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-04-01T14:07:16.000Z (over 3 years ago)
- Last Synced: 2025-03-14T01:31:42.517Z (7 months ago)
- Topics: cache, go, golang, lru, lru-cache
- Language: Go
- Homepage:
- Size: 42 KB
- Stars: 20
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# lru
[](https://pkg.go.dev/github.com/floatdrop/lru)
[](https://github.com/floatdrop/lru/actions/workflows/ci.yml)

[](https://goreportcard.com/report/github.com/floatdrop/lru)Thread safe GoLang LRU cache.
## Example
```go
import (
"fmt""github.com/floatdrop/lru"
)func main() {
cache := lru.New[string, int](256)cache.Set("Hello", 5)
if e := cache.Get("Hello"); e != nil {
fmt.Println(*e)
// Output: 5
}
}
```## TTL
You can wrap values into `Expiring[T any]` struct to release memory on timer (or manually in `Valid` method).
Example implementation
```go
import (
"fmt"
"time""github.com/floatdrop/lru"
)type Expiring[T any] struct {
value *T
}func (E *Expiring[T]) Valid() *T {
if E == nil {
return nil
}return E.value
}func WithTTL[T any](value T, ttl time.Duration) Expiring[T] {
e := Expiring[T]{
value: &value,
}time.AfterFunc(ttl, func() {
e.value = nil // Release memory
})return e
}func main() {
l := lru.New[string, Expiring[string]](256)l.Set("Hello", WithTTL("Bye", time.Hour))
if e := l.Get("Hello").Valid(); e != nil {
fmt.Println(*e)
}
}
```**Note:** Althou this short implementation frees memory after ttl duration, it will not erase entry for key in cache. It can be a problem, if you do not check nillnes after getting element from cache and call `Set` afterwards.
## Benchmarks
```
floatdrop/lru:
BenchmarkLRU_Rand-8 8802915 131.7 ns/op 24 B/op 1 allocs/op
BenchmarkLRU_Freq-8 9392769 127.8 ns/op 24 B/op 1 allocs/ophashicorp/golang-lru:
BenchmarkLRU_Rand-8 5992782 195.8 ns/op 76 B/op 3 allocs/op
BenchmarkLRU_Freq-8 6355358 186.1 ns/op 71 B/op 3 allocs/opjellydator/ttlcache:
BenchmarkLRU_Rand-8 4447654 253.5 ns/op 144 B/op 2 allocs/op
BenchmarkLRU_Freq-8 4837938 240.9 ns/op 137 B/op 2 allocs/op
```