diff --git a/bigcache/bigcache.go b/bigcache/bigcache.go index 77933a6..3c356d2 100644 --- a/bigcache/bigcache.go +++ b/bigcache/bigcache.go @@ -1,44 +1,107 @@ package bigcache -// TODO(juliaqiuxy) Implement using https://github.com/allegro/bigcache - import ( "context" + "encoding/json" "time" + "github.com/allegro/bigcache/v3" "github.com/juliaqiuxy/wfcache" ) -type BigCacheStorage struct{} +type BigCacheStorage struct { + bigCache *bigcache.BigCache + ttl time.Duration +} func Create(ttl time.Duration) wfcache.StorageMaker { + return CreateWithConfig(bigcache.DefaultConfig(ttl)) +} + +func CreateWithConfig(conf bigcache.Config) wfcache.StorageMaker { return func() (wfcache.Storage, error) { - s := &BigCacheStorage{} + bigCache, err := bigcache.NewBigCache(conf) + + if err != nil { + return nil, err + } + + s := &BigCacheStorage{ + bigCache: bigCache, + ttl: conf.LifeWindow, + } return s, nil } } func (s *BigCacheStorage) TimeToLive() time.Duration { - return 0 + return s.ttl } func (s *BigCacheStorage) Get(ctx context.Context, key string) *wfcache.CacheItem { - panic("bigcache: unimplemented") + result, err := s.bigCache.Get(key) + if err != nil { + return nil + } + + cacheItem := wfcache.CacheItem{} + err = json.Unmarshal(result, &cacheItem) + if err != nil { + return nil + } + + return &cacheItem } func (s *BigCacheStorage) BatchGet(ctx context.Context, keys []string) (results []*wfcache.CacheItem) { - panic("bigcache: unimplemented") + for _, key := range keys { + m := s.Get(ctx, key) + + if m != nil { + results = append(results, m) + } + } + + return results } func (s *BigCacheStorage) Set(ctx context.Context, key string, data []byte) error { - panic("bigcache: unimplemented") + item, err := json.Marshal(wfcache.CacheItem{ + Key: key, + Value: data, + ExpiresAt: time.Now().UTC().Add(s.ttl).Unix(), + }) + + if err != nil { + return err + } + + err = s.bigCache.Set(key, item) + if err != nil { + return err + } + + return nil } func (s *BigCacheStorage) BatchSet(ctx context.Context, pairs map[string][]byte) error { - panic("bigcache: unimplemented") + for key, data := range pairs { + err := s.Set(ctx, key, data) + if err != nil { + return err + } + } + + return nil } func (s *BigCacheStorage) Del(ctx context.Context, key string) error { - panic("bigcache: unimplemented") + err := s.bigCache.Delete(key) + + if err != nil { + return err + } + + return nil } diff --git a/go.mod b/go.mod index 2201252..799167a 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/juliaqiuxy/wfcache go 1.16 require ( + github.com/allegro/bigcache/v3 v3.0.0 // indirect github.com/aws/aws-sdk-go v1.38.51 github.com/cenkalti/backoff/v4 v4.1.0 github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index 7014676..39d4ac9 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/allegro/bigcache/v3 v3.0.0 h1:5Hxq+GTy8gHEeQccCZZDCfZRTydUfErdUf0iVDcMAFg= +github.com/allegro/bigcache/v3 v3.0.0/go.mod h1:t5TAJn1B9qvf/VlJrSM1r6NlFAYoFDubYUsCuIO9nUQ= github.com/aws/aws-sdk-go v1.38.51 h1:aKQmbVbwOCuQSd8+fm/MR3bq0QOsu9Q7S+/QEND36oQ= github.com/aws/aws-sdk-go v1.38.51/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= github.com/cenkalti/backoff/v4 v4.1.0 h1:c8LkOFQTzuO0WBM/ae5HdGQuZPfPxp7lqBRwQRm4fSc=