A key:value store/cache library written in Go generics. LRU, LFU, FIFO, MRU, Clock support.
go-generics-cache is an in-memory key:value store/cache that is suitable for applications running on a single machine. This in-memory cache uses Go Generics which is introduced in 1.18.
map[string]interface{}
Go 1.18 or later.
$ go get github.com/Code-Hex/go-generics-cache
See also examples or go playground
package main
import (
"context"
"fmt"
"time"
cache "github.com/Code-Hex/go-generics-cache"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// use simple cache algorithm without options.
c := cache.NewContext[string, int](ctx)
c.Set("a", 1)
gota, aok := c.Get("a")
gotb, bok := c.Get("b")
fmt.Println(gota, aok) // 1 true
fmt.Println(gotb, bok) // 0 false
// Create a cache for Number constraint. key as string, value as int.
nc := cache.NewNumber[string, int]()
nc.Set("age", 26, cache.WithExpiration(time.Hour))
incremented := nc.Increment("age", 1)
fmt.Println(incremented) // 27
decremented := nc.Decrement("age", 1)
fmt.Println(decremented) // 26
}