Skip to content

Commit

Permalink
implement BigCache
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaqiuxy committed Jun 5, 2021
1 parent c158328 commit b8db960
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
83 changes: 73 additions & 10 deletions bigcache/bigcache.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down

0 comments on commit b8db960

Please sign in to comment.