Skip to content

Commit

Permalink
Merge pull request #155 from fmyxyz/master
Browse files Browse the repository at this point in the history
change style use tools gofumpt golangci-lint
  • Loading branch information
eko committed Jun 21, 2022
2 parents 230e140 + 9cc0e64 commit 8326c6c
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 57 deletions.
6 changes: 3 additions & 3 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ func (c *Cache[T]) GetType() string {
// the key if type is string or by computing a checksum of key structure
// if its type is other than string
func (c *Cache[T]) getCacheKey(key any) string {
switch key.(type) {
switch v := key.(type) {
case string:
return key.(string)
return v
case CacheKeyGenerator:
return key.(CacheKeyGenerator).GetCacheKey()
return v.GetCacheKey()
default:
return checksum(key)
}
Expand Down
1 change: 0 additions & 1 deletion cache/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ func TestCacheChecksum(t *testing.T) {
}

func TestChainSetWhenErrorInChain(t *testing.T) {

// Given
ctrl := gomock.NewController(t)
store1 := mocksCache.NewMockSetterCacheInterface[any](ctrl)
Expand Down
4 changes: 1 addition & 3 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ const (
namespaceCache = "cache"
)

var (
cacheCollector *prometheus.GaugeVec = initCacheCollector(namespaceCache)
)
var cacheCollector *prometheus.GaugeVec = initCacheCollector(namespaceCache)

// Prometheus represents the prometheus struct for collecting metrics
type Prometheus struct {
Expand Down
10 changes: 5 additions & 5 deletions store/bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@ func (s *BigcacheStore) Set(ctx context.Context, key any, value any, options ...

func (s *BigcacheStore) setTags(ctx context.Context, key any, tags []string) {
for _, tag := range tags {
var tagKey = fmt.Sprintf(BigcacheTagPattern, tag)
var cacheKeys = []string{}
tagKey := fmt.Sprintf(BigcacheTagPattern, tag)
cacheKeys := []string{}

if result, err := s.Get(ctx, tagKey); err == nil {
if bytes, ok := result.([]byte); ok {
cacheKeys = strings.Split(string(bytes), ",")
}
}

var alreadyInserted = false
alreadyInserted := false
for _, cacheKey := range cacheKeys {
if cacheKey == key.(string) {
alreadyInserted = true
Expand All @@ -120,13 +120,13 @@ func (s *BigcacheStore) Invalidate(ctx context.Context, options ...InvalidateOpt

if tags := opts.tags; len(tags) > 0 {
for _, tag := range tags {
var tagKey = fmt.Sprintf(BigcacheTagPattern, tag)
tagKey := fmt.Sprintf(BigcacheTagPattern, tag)
result, err := s.Get(ctx, tagKey)
if err != nil {
return nil
}

var cacheKeys = []string{}
cacheKeys := []string{}
if bytes, ok := result.([]byte); ok {
cacheKeys = strings.Split(string(bytes), ",")
}
Expand Down
1 change: 0 additions & 1 deletion store/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func TestNotFoundIs(t *testing.T) {

err := NotFoundWithCause(redis.Nil)
assert.True(t, errors.Is(err, NotFound{}))
assert.True(t, errors.Is(err, redis.Nil))
Expand Down
17 changes: 8 additions & 9 deletions store/freecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type FreecacheClientInterface interface {
Clear()
}

//FreecacheStore is a store for freecache
// FreecacheStore is a store for freecache
type FreecacheStore struct {
client FreecacheClientInterface
options *options
Expand Down Expand Up @@ -86,7 +86,7 @@ func (f *FreecacheStore) Set(ctx context.Context, key any, value any, options ..
// Using default options set during cache initialization
opts := applyOptionsWithDefault(f.options, options...)

//type check for value, as freecache only supports value of type []byte
// type check for value, as freecache only supports value of type []byte
switch v := value.(type) {
case []byte:
val = v
Expand All @@ -109,10 +109,10 @@ func (f *FreecacheStore) Set(ctx context.Context, key any, value any, options ..

func (f *FreecacheStore) setTags(ctx context.Context, key any, tags []string) {
for _, tag := range tags {
var tagKey = fmt.Sprintf(FreecacheTagPattern, tag)
var cacheKeys = f.getCacheKeysForTag(ctx, tagKey)
tagKey := fmt.Sprintf(FreecacheTagPattern, tag)
cacheKeys := f.getCacheKeysForTag(ctx, tagKey)

var alreadyInserted = false
alreadyInserted := false
for _, cacheKey := range cacheKeys {
if cacheKey == key.(string) {
alreadyInserted = true
Expand All @@ -129,7 +129,7 @@ func (f *FreecacheStore) setTags(ctx context.Context, key any, tags []string) {
}

func (f *FreecacheStore) getCacheKeysForTag(ctx context.Context, tagKey string) []string {
var cacheKeys = []string{}
cacheKeys := []string{}
if result, err := f.Get(ctx, tagKey); err == nil && result != nil {
if str, ok := result.([]byte); ok {
cacheKeys = strings.Split(string(str), ",")
Expand All @@ -147,7 +147,6 @@ func (f *FreecacheStore) Delete(_ context.Context, key any) error {
return fmt.Errorf("failed to delete key %v", key)
}
return errors.New("key type not supported by Freecache store")

}

// Invalidate invalidates some cache data in freecache for given options
Expand All @@ -156,8 +155,8 @@ func (f *FreecacheStore) Invalidate(ctx context.Context, options ...InvalidateOp

if tags := opts.tags; len(tags) > 0 {
for _, tag := range tags {
var tagKey = fmt.Sprintf(FreecacheTagPattern, tag)
var cacheKeys = f.getCacheKeysForTag(ctx, tagKey)
tagKey := fmt.Sprintf(FreecacheTagPattern, tag)
cacheKeys := f.getCacheKeysForTag(ctx, tagKey)

for _, cacheKey := range cacheKeys {
err := f.Delete(ctx, cacheKey)
Expand Down
1 change: 0 additions & 1 deletion store/freecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ func TestFreecacheSetInvalidSize(t *testing.T) {
s := NewFreecache(client, WithExpiration(6*time.Second))
err := s.Set(ctx, cacheKey, cacheValue, WithExpiration(6*time.Second))
assert.NotNil(t, err)

}

func TestFreecacheSetInvalidKey(t *testing.T) {
Expand Down
7 changes: 3 additions & 4 deletions store/go_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ func (s *GoCacheStore) GetWithTTL(_ context.Context, key any) (any, time.Duratio
if !exists {
return data, 0, NotFoundWithCause(errors.New("value not found in GoCache store"))
}
duration := t.Sub(time.Now())
duration := time.Until(t)
return data, duration, nil
}

// Set defines data in GoCache memoey cache for given key identifier
func (s *GoCacheStore) Set(ctx context.Context, key any, value any, options ...Option) error {

opts := applyOptions(options...)
if opts == nil {
opts = s.options
Expand All @@ -80,7 +79,7 @@ func (s *GoCacheStore) Set(ctx context.Context, key any, value any, options ...O

func (s *GoCacheStore) setTags(ctx context.Context, key any, tags []string) {
for _, tag := range tags {
var tagKey = fmt.Sprintf(GoCacheTagPattern, tag)
tagKey := fmt.Sprintf(GoCacheTagPattern, tag)
var cacheKeys map[string]struct{}

if result, err := s.Get(ctx, tagKey); err == nil {
Expand Down Expand Up @@ -120,7 +119,7 @@ func (s *GoCacheStore) Invalidate(ctx context.Context, options ...InvalidateOpti

if tags := opts.tags; len(tags) > 0 {
for _, tag := range tags {
var tagKey = fmt.Sprintf(GoCacheTagPattern, tag)
tagKey := fmt.Sprintf(GoCacheTagPattern, tag)
result, err := s.Get(ctx, tagKey)
if err != nil {
return nil
Expand Down
7 changes: 3 additions & 4 deletions store/go_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func TestGoCacheSetWithTags(t *testing.T) {
client := mocksStore.NewMockGoCacheClientInterface(ctrl)
client.EXPECT().Set(cacheKey, cacheValue, 0*time.Second)
client.EXPECT().Get("gocache_tag_tag1").Return(nil, true)
var cacheKeys = map[string]struct{}{"my-key": {}}
cacheKeys := map[string]struct{}{"my-key": {}}
client.EXPECT().Set("gocache_tag_tag1", cacheKeys, 720*time.Hour)

store := NewGoCache(client)
Expand All @@ -192,7 +192,7 @@ func TestGoCacheSetWithTagsWhenAlreadyInserted(t *testing.T) {
client := mocksStore.NewMockGoCacheClientInterface(ctrl)
client.EXPECT().Set(cacheKey, cacheValue, 0*time.Second)

var cacheKeys = map[string]struct{}{"my-key": {}, "a-second-key": {}}
cacheKeys := map[string]struct{}{"my-key": {}, "a-second-key": {}}
client.EXPECT().Get("gocache_tag_tag1").Return(cacheKeys, true)

store := NewGoCache(client)
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestGoCacheInvalidate(t *testing.T) {

ctx := context.Background()

var cacheKeys = map[string]struct{}{"a23fdf987h2svc23": {}, "jHG2372x38hf74": {}}
cacheKeys := map[string]struct{}{"a23fdf987h2svc23": {}, "jHG2372x38hf74": {}}

client := mocksStore.NewMockGoCacheClientInterface(ctrl)
client.EXPECT().Get("gocache_tag_tag1").Return(cacheKeys, true)
Expand Down Expand Up @@ -304,7 +304,6 @@ func TestGoCacheSetTagsConcurrency(t *testing.T) {

for i := 0; i < 200; i++ {
go func(i int) {

key := fmt.Sprintf("%d", i)

err := store.Set(
Expand Down
6 changes: 3 additions & 3 deletions store/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (s *MemcacheStore) setTags(ctx context.Context, key any, tags []string) {
for _, tag := range tags {
currentTag := tag
group.Go(func() error {
var tagKey = fmt.Sprintf(MemcacheTagPattern, currentTag)
tagKey := fmt.Sprintf(MemcacheTagPattern, currentTag)

var err error
for i := 0; i < 3; i++ {
Expand Down Expand Up @@ -167,13 +167,13 @@ func (s *MemcacheStore) Invalidate(ctx context.Context, options ...InvalidateOpt

if tags := opts.tags; len(tags) > 0 {
for _, tag := range tags {
var tagKey = fmt.Sprintf(MemcacheTagPattern, tag)
tagKey := fmt.Sprintf(MemcacheTagPattern, tag)
result, err := s.Get(ctx, tagKey)
if err != nil {
return nil
}

var cacheKeys = []string{}
cacheKeys := []string{}
if bytes, ok := result.([]byte); ok {
cacheKeys = strings.Split(string(bytes), ",")
}
Expand Down
30 changes: 14 additions & 16 deletions store/pegasus.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ const (
DefaultScanNum = 100
)

var (
// empty represent empty sort key, more info reference: https://github.com/XiaoMi/pegasus-go-client/blob/f3b6b08bc4c227982bb5b73106329435fda97a38/pegasus/table_connector.go#L83
empty = []byte("-")
)
// empty represent empty sort key, more info reference: https://github.com/XiaoMi/pegasus-go-client/blob/f3b6b08bc4c227982bb5b73106329435fda97a38/pegasus/table_connector.go#L83
var empty = []byte("-")

// OptionsPegasus is options of Pegasus
type OptionsPegasus struct {
Expand Down Expand Up @@ -61,10 +59,10 @@ func NewPegasus(ctx context.Context, options *OptionsPegasus) (*PegasusStore, er
MetaServers: options.MetaServers,
})
table, err := client.OpenTable(ctx, options.TableName)
defer table.Close()
if err != nil {
return nil, err
}
defer table.Close()

return &PegasusStore{
client: client,
Expand Down Expand Up @@ -130,10 +128,10 @@ func (p *PegasusStore) Close() error {
// Get returns data stored from a given key
func (p *PegasusStore) Get(ctx context.Context, key any) (any, error) {
table, err := p.client.OpenTable(ctx, p.options.TableName)
defer table.Close()
if err != nil {
return nil, err
}
defer table.Close()

value, err := table.Get(ctx, []byte(cast.ToString(key)), empty)
if err != nil {
Expand All @@ -148,10 +146,10 @@ func (p *PegasusStore) Get(ctx context.Context, key any) (any, error) {
// GetWithTTL returns data stored from a given key and its corresponding TTL
func (p *PegasusStore) GetWithTTL(ctx context.Context, key any) (any, time.Duration, error) {
table, err := p.client.OpenTable(ctx, p.options.TableName)
defer table.Close()
if err != nil {
return nil, 0, err
}
defer table.Close()

value, err := table.Get(ctx, []byte(cast.ToString(key)), empty)
if err != nil {
Expand All @@ -174,10 +172,10 @@ func (p *PegasusStore) Set(ctx context.Context, key, value any, options ...Optio
opts := applyOptions(options...)

table, err := p.client.OpenTable(ctx, p.options.TableName)
defer table.Close()
if err != nil {
return err
}
defer table.Close()

err = table.SetTTL(ctx, []byte(cast.ToString(key)), empty, []byte(cast.ToString(value)), opts.expiration)
if err != nil {
Expand All @@ -194,16 +192,16 @@ func (p *PegasusStore) Set(ctx context.Context, key, value any, options ...Optio

func (p *PegasusStore) setTags(ctx context.Context, key any, tags []string) error {
for _, tag := range tags {
var tagKey = fmt.Sprintf(PegasusTagPattern, tag)
var cacheKeys = []string{}
tagKey := fmt.Sprintf(PegasusTagPattern, tag)
cacheKeys := []string{}

if result, err := p.Get(ctx, tagKey); err == nil {
if bytes, ok := result.([]byte); ok {
cacheKeys = strings.Split(string(bytes), ",")
}
}

var alreadyInserted = false
alreadyInserted := false
for _, cacheKey := range cacheKeys {
if cacheKey == key.(string) {
alreadyInserted = true
Expand All @@ -226,10 +224,10 @@ func (p *PegasusStore) setTags(ctx context.Context, key any, tags []string) erro
// Delete removes data from Pegasus for given key identifier
func (p *PegasusStore) Delete(ctx context.Context, key any) error {
table, err := p.client.OpenTable(ctx, p.options.TableName)
defer table.Close()
if err != nil {
return err
}
defer table.Close()

return table.Del(ctx, []byte(cast.ToString(key)), empty)
}
Expand All @@ -239,13 +237,13 @@ func (p *PegasusStore) Invalidate(ctx context.Context, options ...InvalidateOpti
opts := applyInvalidateOptions(options...)
if tags := opts.tags; len(tags) > 0 {
for _, tag := range tags {
var tagKey = fmt.Sprintf(PegasusTagPattern, tag)
tagKey := fmt.Sprintf(PegasusTagPattern, tag)
result, err := p.Get(ctx, tagKey)
if err != nil {
return nil
}

var cacheKeys = []string{}
cacheKeys := []string{}
if bytes, ok := result.([]byte); ok {
cacheKeys = strings.Split(string(bytes), ",")
}
Expand All @@ -264,10 +262,10 @@ func (p *PegasusStore) Invalidate(ctx context.Context, options ...InvalidateOpti
// Clear resets all data in the store
func (p *PegasusStore) Clear(ctx context.Context) error {
table, err := p.client.OpenTable(ctx, p.options.TableName)
defer table.Close()
if err != nil {
return err
}
defer table.Close()

// init full scan
scanners, err := table.GetUnorderedScanners(ctx, p.options.TablePartitionNum, &pegasus.ScannerOptions{
Expand All @@ -282,7 +280,7 @@ func (p *PegasusStore) Clear(ctx context.Context) error {
// full scan and delete
for _, scanner := range scanners {
// Iterates sequentially.
for true {
for {
completed, hashKey, _, _, err := scanner.Next(ctx)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion store/pegasus_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"math"

"testing"
)

Expand Down
2 changes: 1 addition & 1 deletion store/pegasus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestNewPegasus(t *testing.T) {
ctx := context.Background()

p, err := NewPegasus(ctx, testPegasusOptions())
defer p.Close()
So(err, ShouldBeNil)
defer p.Close()
})
}

Expand Down

0 comments on commit 8326c6c

Please sign in to comment.