Change backend for in-memory cache to patrickmn/go-cache
This commit is contained in:
parent
43871c6141
commit
3366a71ac1
35
cache/inmemory_cache.go
vendored
35
cache/inmemory_cache.go
vendored
@ -3,54 +3,45 @@ package cache
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/VictoriaMetrics/fastcache"
|
||||
"github.com/fxamacker/cbor/v2"
|
||||
"github.com/patrickmn/go-cache"
|
||||
)
|
||||
|
||||
const (
|
||||
// DefaultInMemoryCacheCapacity is maximal in-memory cache size in bytes
|
||||
DefaultInMemoryCacheCapacity = 32000000
|
||||
DefaultCacheExpiration = 5 * time.Minute
|
||||
DefaultGCInterval = 10 * time.Minute
|
||||
)
|
||||
|
||||
type InMemoryCache struct {
|
||||
cache *fastcache.Cache
|
||||
cache *cache.Cache
|
||||
}
|
||||
|
||||
func NewInMemoryCache() *InMemoryCache {
|
||||
return &InMemoryCache{
|
||||
cache: fastcache.New(DefaultInMemoryCacheCapacity),
|
||||
cache: cache.New(DefaultCacheExpiration, DefaultGCInterval),
|
||||
}
|
||||
}
|
||||
|
||||
func (imc *InMemoryCache) Store(key string, value interface{}) error {
|
||||
mRes, err := cbor.Marshal(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
imc.cache.SetBig([]byte(key), mRes)
|
||||
imc.cache.Set(key, value, cache.NoExpiration)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (imc *InMemoryCache) StoreWithTTL(key string, value interface{}, ttl time.Duration) error {
|
||||
return imc.Store(key, value) // fastcache doesn't support ttl for values
|
||||
imc.cache.Set(key, value, ttl)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (imc *InMemoryCache) Get(key string, v interface{}) error {
|
||||
data := make([]byte, 0)
|
||||
imc.cache.GetBig(data, []byte(key))
|
||||
if len(data) == 0 {
|
||||
func (imc *InMemoryCache) Get(key string, value interface{}) error {
|
||||
v, exists := imc.cache.Get(key)
|
||||
if !exists {
|
||||
return ErrNilValue
|
||||
}
|
||||
err := cbor.Unmarshal(data, v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
value = v
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (imc *InMemoryCache) Delete(key string) {
|
||||
imc.cache.Del([]byte(key))
|
||||
imc.cache.Delete(key)
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -5,7 +5,6 @@ go 1.14
|
||||
require (
|
||||
github.com/Secured-Finance/go-libp2p-pex v1.1.0
|
||||
github.com/Secured-Finance/golang-set v1.8.0
|
||||
github.com/VictoriaMetrics/fastcache v1.5.7
|
||||
github.com/aristanetworks/goarista v0.0.0-20210308203447-b196d8410f1d // indirect
|
||||
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect
|
||||
github.com/cespare/cp v1.1.1 // indirect
|
||||
@ -46,6 +45,7 @@ require (
|
||||
github.com/multiformats/go-multiaddr v0.3.1
|
||||
github.com/multiformats/go-multihash v0.0.15 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.4 // indirect
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||
github.com/raulk/clock v1.1.0
|
||||
github.com/rjeczalik/notify v0.9.2 // indirect
|
||||
github.com/rs/cors v1.7.0 // indirect
|
||||
|
2
go.sum
2
go.sum
@ -1469,6 +1469,8 @@ github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh
|
||||
github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
|
||||
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
|
||||
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||
github.com/pborman/uuid v0.0.0-20170112150404-1b00554d8222/go.mod h1:VyrYX9gd7irzKovcSS6BIIEwPRkP2Wm2m9ufcdFSJ34=
|
||||
github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
|
||||
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
|
||||
|
Loading…
Reference in New Issue
Block a user