The library adds a thin layer on top of lru\expirable cache.
Cache name | Constructor | Defaults | Description |
---|---|---|---|
LruCache | lcw.NewLruCache | keys=1000 | LRU cache with limits |
ExpirableCache | lcw.NewExpirableCache | keys=1000, ttl=5m | TTL cache with limits |
RedisCache | lcw.NewRedisCache | ttl=5m | Redis cache with limits |
Nop | lcw.NewNopCache | Do-nothing cache |
Main features:
ExpirableCache
and RedisCache
)RedisCache
)go get -u github.com/go-pkgz/lcw/v2
package main
import (
"github.com/go-pkgz/lcw/v2"
)
func main() {
o := lcw.NewOpts[int]()
cache, err := lcw.NewLruCache(o.MaxKeys(500), o.MaxCacheSize(65536), o.MaxValSize(200), o.MaxKeySize(32))
if err != nil {
panic("failed to create cache")
}
defer cache.Close()
val, err := cache.Get("key123", func() (int, error) {
res, err := getDataFromSomeSource(params) // returns int
return res, err
})
if err != nil {
panic("failed to get data")
}
s := val // cached value
}
Cache can be created with URIs:
mem://lru?max_key_size=10&max_val_size=1024&max_keys=50&max_cache_size=64000
- creates LRU cache with given limitsmem://expirable?ttl=30s&max_key_size=10&max_val_size=1024&max_keys=50&max_cache_size=64000
- create expirable cacheredis://10.0.0.1:1234?db=16&password=qwerty&network=tcp4&dial_timeout=1s&read_timeout=5s&write_timeout=3s
- create
redis cachenop://
- create Nop cacheScache
provides a wrapper on top of all implementations of LoadingCache
with a number of special features:
[]byte
Flush
method for scoped/tagged invalidation of multiple records in a given partitionExampleLoadingCache_Mutability
illustrates that.lcw.Sizer
interface.lcw
.