From 80251de6aef80e179c6cabb661c89ef91e1262e1 Mon Sep 17 00:00:00 2001 From: Julia Qiu Date: Sun, 6 Jun 2021 13:59:14 -0700 Subject: [PATCH] implement golru adapter --- go.mod | 1 + go.sum | 2 ++ golru/golru.go | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 golru/golru.go diff --git a/go.mod b/go.mod index a8f92c1..bea87ca 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/cenkalti/backoff/v4 v4.1.0 github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-redis/redis/v8 v8.10.0 // indirect + github.com/manucorporat/golru v0.0.0-20140606170941-59079c2a3565 // indirect github.com/stretchr/testify v1.7.0 // indirect github.com/thoas/go-funk v0.8.0 gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index cecb7c8..6e90ea7 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/manucorporat/golru v0.0.0-20140606170941-59079c2a3565 h1:ijt1m3vhbXQGZzFwjLie0WDearhtX+Fn0VIN/yAsr0o= +github.com/manucorporat/golru v0.0.0-20140606170941-59079c2a3565/go.mod h1:fVS5OR0DKAGXdkzgjOvCcqXnxO0GzeppLckxIFfOCl8= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= diff --git a/golru/golru.go b/golru/golru.go new file mode 100644 index 0000000..d78ed36 --- /dev/null +++ b/golru/golru.go @@ -0,0 +1,93 @@ +package golru + +import ( + "context" + "encoding/json" + "time" + + "github.com/juliaqiuxy/wfcache" + "github.com/manucorporat/golru" +) + +type GoLRUStorage struct { + golru *golru.Cache + ttl time.Duration +} + +func Create(capacity int, ttl time.Duration) wfcache.StorageMaker { + return CreateWithConfig(capacity, golru.DefaultLRUSamples, ttl) +} + +func CreateWithConfig(capacity int, samples int, ttl time.Duration) wfcache.StorageMaker { + return func() (wfcache.Storage, error) { + golru := golru.New(capacity, golru.DefaultLRUSamples) + + s := &GoLRUStorage{ + golru: golru, + ttl: ttl, + } + + return s, nil + } +} + +func (s *GoLRUStorage) TimeToLive() time.Duration { + return s.ttl +} + +func (s *GoLRUStorage) Get(ctx context.Context, key string) *wfcache.CacheItem { + result := s.golru.Get(key) + + cacheItem := wfcache.CacheItem{} + err := json.Unmarshal(result, &cacheItem) + if err != nil { + return nil + } + + return &cacheItem +} + +func (s *GoLRUStorage) BatchGet(ctx context.Context, keys []string) (results []*wfcache.CacheItem) { + for _, key := range keys { + m := s.Get(ctx, key) + + if m != nil { + results = append(results, m) + } + } + + return results +} + +func (s *GoLRUStorage) Set(ctx context.Context, key string, data []byte) error { + item, err := json.Marshal(wfcache.CacheItem{ + Key: key, + Value: data, + ExpiresAt: time.Now().UTC().Add(s.ttl).Unix(), + }) + + if err != nil { + return err + } + + s.golru.Set(key, item) + + return nil +} + +func (s *GoLRUStorage) BatchSet(ctx context.Context, pairs map[string][]byte) error { + for key, data := range pairs { + err := s.Set(ctx, key, data) + if err != nil { + return err + } + } + + return nil +} + +func (s *GoLRUStorage) Del(ctx context.Context, key string) error { + s.golru.Del(key) + + return nil +}