2
pkg/simple/client/cache/cache.go
vendored
2
pkg/simple/client/cache/cache.go
vendored
@@ -2,6 +2,8 @@ package cache
|
||||
|
||||
import "time"
|
||||
|
||||
var NeverExpire = time.Duration(0)
|
||||
|
||||
type Interface interface {
|
||||
// Keys retrieves all keys match the given pattern
|
||||
Keys(pattern string) ([]string, error)
|
||||
|
||||
105
pkg/simple/client/cache/simple_cache.go
vendored
105
pkg/simple/client/cache/simple_cache.go
vendored
@@ -1,40 +1,111 @@
|
||||
package cache
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"kubesphere.io/kubesphere/pkg/server/errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrNoSuchKey = errors.New("no such key")
|
||||
|
||||
type simpleObject struct {
|
||||
value string
|
||||
expire time.Time
|
||||
value string
|
||||
neverExpire bool
|
||||
expiredAt time.Time
|
||||
}
|
||||
|
||||
type SimpleCache struct {
|
||||
// SimpleCache implements cache.Interface use memory objects, it should be used only for testing
|
||||
type simpleCache struct {
|
||||
store map[string]simpleObject
|
||||
}
|
||||
|
||||
func NewSimpleCache() Interface {
|
||||
return &SimpleCache{store: make(map[string]simpleObject)}
|
||||
return &simpleCache{store: make(map[string]simpleObject)}
|
||||
}
|
||||
|
||||
func (s *SimpleCache) Keys(pattern string) ([]string, error) {
|
||||
panic("implement me")
|
||||
func (s *simpleCache) Keys(pattern string) ([]string, error) {
|
||||
// There is a little difference between go regexp and redis key pattern
|
||||
// In redis, * means any character, while in go . means match everything.
|
||||
pattern = strings.Replace(pattern, "*", ".", -1)
|
||||
|
||||
re, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var keys []string
|
||||
for k, _ := range s.store {
|
||||
if re.MatchString(k) {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
}
|
||||
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
func (s *SimpleCache) Set(key string, value string, duration time.Duration) error {
|
||||
panic("implement me")
|
||||
func (s *simpleCache) Set(key string, value string, duration time.Duration) error {
|
||||
sobject := simpleObject{
|
||||
value: value,
|
||||
neverExpire: false,
|
||||
expiredAt: time.Now().Add(duration),
|
||||
}
|
||||
|
||||
if duration == NeverExpire {
|
||||
sobject.neverExpire = true
|
||||
}
|
||||
|
||||
s.store[key] = sobject
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SimpleCache) Del(keys ...string) error {
|
||||
panic("implement me")
|
||||
func (s *simpleCache) Del(keys ...string) error {
|
||||
for _, key := range keys {
|
||||
if _, ok := s.store[key]; ok {
|
||||
delete(s.store, key)
|
||||
} else {
|
||||
return ErrNoSuchKey
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SimpleCache) Get(key string) (string, error) {
|
||||
return "", nil
|
||||
func (s *simpleCache) Get(key string) (string, error) {
|
||||
if sobject, ok := s.store[key]; ok {
|
||||
if sobject.neverExpire || time.Now().Before(sobject.expiredAt) {
|
||||
return sobject.value, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", ErrNoSuchKey
|
||||
}
|
||||
|
||||
func (s *SimpleCache) Exists(keys ...string) (bool, error) {
|
||||
panic("implement me")
|
||||
func (s *simpleCache) Exists(keys ...string) (bool, error) {
|
||||
for _, key := range keys {
|
||||
if _, ok := s.store[key]; !ok {
|
||||
return false, ErrNoSuchKey
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *SimpleCache) Expire(key string, duration time.Duration) error {
|
||||
panic("implement me")
|
||||
func (s *simpleCache) Expire(key string, duration time.Duration) error {
|
||||
value, err := s.Get(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sobject := simpleObject{
|
||||
value: value,
|
||||
neverExpire: false,
|
||||
expiredAt: time.Now().Add(duration),
|
||||
}
|
||||
|
||||
if duration == NeverExpire {
|
||||
sobject.neverExpire = true
|
||||
}
|
||||
|
||||
s.store[key] = sobject
|
||||
return nil
|
||||
}
|
||||
|
||||
123
pkg/simple/client/cache/simple_cache_test.go
vendored
Normal file
123
pkg/simple/client/cache/simple_cache_test.go
vendored
Normal file
@@ -0,0 +1,123 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var dataSet = map[string]string{
|
||||
"foo1": "val1",
|
||||
"foo2": "val2",
|
||||
"foo3": "val3",
|
||||
"bar1": "val1",
|
||||
"bar2": "val2",
|
||||
}
|
||||
|
||||
// load dataset into cache
|
||||
func load(client Interface, data map[string]string) error {
|
||||
for k, v := range data {
|
||||
err := client.Set(k, v, NeverExpire)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// dump retrieve all data in simple into a map
|
||||
func dump(client Interface) (map[string]string, error) {
|
||||
keys, err := client.Keys("*")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
snapshot := make(map[string]string)
|
||||
for _, key := range keys {
|
||||
val, err := client.Get(key)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
snapshot[key] = val
|
||||
}
|
||||
|
||||
return snapshot, nil
|
||||
}
|
||||
|
||||
func TestDeleteAndExpireCache(t *testing.T) {
|
||||
var testCases = []struct {
|
||||
description string
|
||||
deleteKeys sets.String
|
||||
expireKeys sets.String
|
||||
expireDuration time.Duration // never use a 0(NeverExpires) duration with expireKeys, recommend time.Millisecond * 500.
|
||||
expected map[string]string
|
||||
}{
|
||||
{
|
||||
description: "Should get all keys",
|
||||
expected: map[string]string{
|
||||
"foo1": "val1",
|
||||
"foo2": "val2",
|
||||
"foo3": "val3",
|
||||
"bar1": "val1",
|
||||
"bar2": "val2",
|
||||
},
|
||||
},
|
||||
{
|
||||
description: "Test delete should get only keys start with foo",
|
||||
expected: map[string]string{
|
||||
"foo1": "val1",
|
||||
"foo2": "val2",
|
||||
"foo3": "val3",
|
||||
},
|
||||
deleteKeys: sets.NewString("bar1", "bar2"),
|
||||
},
|
||||
{
|
||||
description: "Should get only keys start with bar",
|
||||
expected: map[string]string{
|
||||
"bar1": "val1",
|
||||
"bar2": "val2",
|
||||
},
|
||||
expireDuration: time.Millisecond * 500,
|
||||
expireKeys: sets.NewString("foo1", "foo2", "foo3"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
cacheClient := NewSimpleCache()
|
||||
|
||||
t.Run(testCase.description, func(t *testing.T) {
|
||||
err := load(cacheClient, dataSet)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to load dataset, got error %v", err)
|
||||
}
|
||||
|
||||
if len(testCase.deleteKeys) != 0 {
|
||||
err = cacheClient.Del(testCase.deleteKeys.List()...)
|
||||
if err != nil {
|
||||
t.Fatalf("Error delete keys, %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(testCase.expireKeys) != 0 && testCase.expireDuration != 0 {
|
||||
for _, key := range testCase.expireKeys.List() {
|
||||
err = cacheClient.Expire(key, testCase.expireDuration)
|
||||
if err != nil {
|
||||
t.Fatalf("Error expire keys, %v", err)
|
||||
}
|
||||
}
|
||||
time.Sleep(testCase.expireDuration)
|
||||
}
|
||||
|
||||
got, err := dump(cacheClient)
|
||||
if err != nil {
|
||||
t.Fatalf("Error dump data, %v", err)
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(got, testCase.expected); len(diff) != 0 {
|
||||
t.Errorf("%T differ (-got, +expected) %v", testCase.expected, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user