gcache is a high-performance Go language implementation of a cache library. It is easy to use and understand
- LRU elimination algorithm
- Use singleflight to avoid cache pinging
- Support custom load method
- Support cache change listener
Map shard
go get -u github.com/wang1309/gcache
init cache object
import (
"sync"
"time"
"golang.org/x/sync/singleflight"
"github.com/wang1309/gcache"
)
const maxItems = 100
func main() {
// create cache object
c := cache.NewCache(loader, maxItems)
}
func loader(key string) interface{} {
// get data by key
return "value"
}
Get
value := c.Get("key")
Put
c.Put("key", "value")
goos: darwin goarch: arm64
BenchmarkCache_GetParallel/Get-8 3000000 314.4 ns/op
BenchmarkCache_Put-8 3000000 190.4 ns/op