Skip to content

Commit

Permalink
implement golru adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaqiuxy committed Jun 6, 2021
1 parent 2add68b commit 80251de
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
93 changes: 93 additions & 0 deletions golru/golru.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 80251de

Please sign in to comment.