Skip to content

Commit

Permalink
feat: 添加Delete接口、具体实现及测试、添加启动第三方环境的命令等 (#25)
Browse files Browse the repository at this point in the history
* feat: 添加Delete接口、具体实现及测试、添加启动第三方环境的命令等

Signed-off-by: longyue0521 <[email protected]>

* refactor: 将TestCache_e2e_Delete改为TestCache_Delete

Signed-off-by: longyue0521 <[email protected]>

---------

Signed-off-by: longyue0521 <[email protected]>
  • Loading branch information
longyue0521 authored Oct 10, 2023
1 parent fa6fdcc commit da8d024
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 40 deletions.
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ check:
# e2e 测试
.PHONY: e2e
e2e:
sh ./script/integrate_test.sh
sh ./script/integrate_test.sh

.PHONY: dev_3rd_up
dev_3rd_up:
docker-compose -f script/integration_test_compose.yml up -d

.PHONY: dev_3rd_down
dev_3rd_down:
docker-compose -f script/integration_test_compose.yml down -v
1 change: 1 addition & 0 deletions internal/errs/errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ package errs
import "errors"

var ErrKeyNotExist = errors.New("key 不存在")
var ErrDeleteKeyFailed = errors.New("删除key失败")
27 changes: 27 additions & 0 deletions memory/lru/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package lru
import (
"context"
"errors"
"fmt"
"sync"
"time"

Expand All @@ -27,6 +28,10 @@ import (
"github.com/hashicorp/golang-lru/v2/simplelru"
)

var (
_ ecache.Cache = (*Cache)(nil)
)

type Cache struct {
lock sync.RWMutex
client simplelru.LRUCache[string, any]
Expand Down Expand Up @@ -89,6 +94,28 @@ func (c *Cache) GetSet(ctx context.Context, key string, val string) (result ecac
return
}

func (c *Cache) Delete(ctx context.Context, key ...string) (int64, error) {
c.lock.Lock()
defer c.lock.Unlock()

n := int64(0)
for _, k := range key {
if ctx.Err() != nil {
return n, ctx.Err()
}
_, ok := c.client.Get(k)
if !ok {
continue
}
if c.client.Remove(k) {
n++
} else {
return n, fmt.Errorf("%w: key = %s", errs.ErrDeleteKeyFailed, k)
}
}
return n, nil
}

// anySliceToValueSlice 公共转换
func (c *Cache) anySliceToValueSlice(data ...any) []ecache.Value {
newVal := make([]ecache.Value, len(data), cap(data))
Expand Down
114 changes: 112 additions & 2 deletions memory/lru/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import (
"time"

"github.com/ecodeclub/ecache"
"github.com/ecodeclub/ecache/internal/errs"
"github.com/ecodeclub/ekit/list"
"github.com/hashicorp/golang-lru/v2/simplelru"

"github.com/ecodeclub/ecache/internal/errs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -251,6 +250,101 @@ func TestCache_GetSet(t *testing.T) {
}
}

func TestCache_Delete(t *testing.T) {
cache, err := newCache()
require.NoError(t, err)

testCases := []struct {
name string
before func(ctx context.Context, t *testing.T, cache ecache.Cache)

ctxFunc func() context.Context
key []string

wantN int64
wantErr error
}{
{
name: "delete single existed key",
before: func(ctx context.Context, t *testing.T, cache ecache.Cache) {
require.NoError(t, cache.Set(ctx, "name", "Alex", 0))
},
ctxFunc: func() context.Context {
return context.Background()
},
key: []string{"name"},
wantN: 1,
},
{
name: "delete single does not existed key",
before: func(ctx context.Context, t *testing.T, cache ecache.Cache) {},
ctxFunc: func() context.Context {
return context.Background()
},
key: []string{"notExistedKey"},
},
{
name: "delete multiple existed keys",
before: func(ctx context.Context, t *testing.T, cache ecache.Cache) {
require.NoError(t, cache.Set(ctx, "name", "Alex", 0))
require.NoError(t, cache.Set(ctx, "age", 18, 0))
},
ctxFunc: func() context.Context {
return context.Background()
},
key: []string{"name", "age"},
wantN: 2,
},
{
name: "delete multiple do not existed keys",
before: func(ctx context.Context, t *testing.T, cache ecache.Cache) {},
ctxFunc: func() context.Context {
return context.Background()
},
key: []string{"name", "age"},
},
{
name: "delete multiple keys, some do not existed keys",
before: func(ctx context.Context, t *testing.T, cache ecache.Cache) {
require.NoError(t, cache.Set(ctx, "name", "Alex", 0))
require.NoError(t, cache.Set(ctx, "age", 18, 0))
require.NoError(t, cache.Set(ctx, "gender", "male", 0))
},
ctxFunc: func() context.Context {
return context.Background()
},
key: []string{"name", "age", "gender", "addr"},
wantN: 3,
},
{
name: "timeout",
before: func(ctx context.Context, t *testing.T, cache ecache.Cache) {},
ctxFunc: func() context.Context {
timeout := time.Millisecond * 100
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
time.Sleep(timeout * 2)
return ctx
},
key: []string{"name", "age", "addr"},
wantErr: context.DeadlineExceeded,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctx := tc.ctxFunc()
tc.before(ctx, t, cache)
n, err := cache.Delete(ctx, tc.key...)
if err != nil {
assert.ErrorIs(t, err, tc.wantErr)
return
}
assert.Equal(t, tc.wantN, n)
})
}
}

func TestCache_LPush(t *testing.T) {
evictCounter := 0
onEvicted := func(key string, value any) {
Expand Down Expand Up @@ -628,3 +722,19 @@ func TestCache_IncrByFloat(t *testing.T) {
})
}
}

func newCache() (ecache.Cache, error) {
client, err := newSimpleLRUClient(10)
if err != nil {
return nil, err
}
return NewCache(client), nil
}

func newSimpleLRUClient(size int) (simplelru.LRUCache[string, any], error) {
evictCounter := 0
onEvicted := func(key string, value any) {
evictCounter++
}
return simplelru.NewLRU[string, any](size, onEvicted)
}
4 changes: 4 additions & 0 deletions redis/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (c *Cache) SetNX(ctx context.Context, key string, val any, expiration time.
return c.client.SetNX(ctx, key, val, expiration).Result()
}

func (c *Cache) Delete(ctx context.Context, key ...string) (int64, error) {
return c.client.Del(ctx, key...).Result()
}

func (c *Cache) Get(ctx context.Context, key string) (val ecache.Value) {
val.Val, val.Err = c.client.Get(ctx, key).Result()
if val.Err != nil && errors.Is(val.Err, redis.Nil) {
Expand Down
Loading

0 comments on commit da8d024

Please sign in to comment.