https://github.com/electricbubble/lru
thread safe LRU cache (Go generics)
https://github.com/electricbubble/lru
generics go golang
Last synced: about 1 year ago
JSON representation
thread safe LRU cache (Go generics)
- Host: GitHub
- URL: https://github.com/electricbubble/lru
- Owner: electricbubble
- License: mit
- Created: 2022-03-28T14:23:56.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-03-28T14:31:15.000Z (about 4 years ago)
- Last Synced: 2025-01-22T09:46:14.505Z (over 1 year ago)
- Topics: generics, go, golang
- Language: Go
- Homepage:
- Size: 13.7 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Golang-LRU
fixed-size thread safe LRU cache (Go generics).
It is based on [golang/groupcache](https://github.com/golang/groupcache)
`&&` [hashicorp/golang-lru](https://github.com/hashicorp/golang-lru).
## Install
```shell script
go get github.com/electricbubble/lru
```
## Example
```go
package main
import (
"fmt"
"github.com/electricbubble/lru"
)
func main() {
maxEntries := 64
cache := lru.New[int, any](maxEntries)
for i := 0; i < maxEntries; i++ {
cache.Add(i, nil)
}
if cache.Len() != maxEntries {
panic(fmt.Sprintf("bad len: %v", cache.Len()))
}
}
```
## Benchmark
```shell
go test -bench='Benchmark.+afeLru_Add' . -benchmem
```
```text
goos: darwin
goarch: amd64
pkg: github.com/electricbubble/lru
cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
BenchmarkUnsafeLru_Add-8 2793051 430.8 ns/op 81 B/op 3 allocs/op
BenchmarkSafeLru_Add-8 1059967 1181 ns/op 262 B/op 5 allocs/op
```
## Thanks
| |About|
|---|---|
|[golang/groupcache](https://github.com/golang/groupcache)|groupcache is a caching and cache-filling library, intended as a replacement for memcached in many cases.|
|[hashicorp/golang-lru](https://github.com/hashicorp/golang-lru)|Golang LRU cache|
Thank you [JetBrains](https://www.jetbrains.com/?from=gwda) for providing free open source licenses