cleanup
parent
8f0b91dabd
commit
ce1dd7ca37
|
@ -1,39 +0,0 @@
|
|||
package cache
|
||||
|
||||
// Cache - generic cache interface
|
||||
// type Cache interface {
|
||||
// Put(ctx context.Context, key string, value []byte) error
|
||||
// Get(ctx context.Context, key string) (value []byte, err error)
|
||||
// Delete(ctx context.Context, key string) error
|
||||
// List(prefix string) ([][]byte, error)
|
||||
// }
|
||||
// type Cache interface {
|
||||
// Put(key string, value []byte) error
|
||||
// Get(key string) (value []byte, err error)
|
||||
// Delete(key string) error
|
||||
// List(prefix string) (map[string][]byte, error)
|
||||
// }
|
||||
|
||||
// type expirationContextKeyType int
|
||||
|
||||
// const expirationContextKey expirationContextKeyType = 1
|
||||
|
||||
// // SetContextExpiration - set cache expiration context
|
||||
// func SetContextExpiration(ctx context.Context, expiration time.Duration) context.Context {
|
||||
// return context.WithValue(ctx, expirationContextKey, expiration)
|
||||
// }
|
||||
|
||||
// // GetContextExpiration - gets expiration from context, returns it and also returns
|
||||
// // ok - true/false to indicate whether ctx value was found
|
||||
// func GetContextExpiration(ctx context.Context) (exp time.Duration, ok bool) {
|
||||
// expiration := ctx.Value(expirationContextKey)
|
||||
// if expiration != nil {
|
||||
// return expiration.(time.Duration), true
|
||||
// }
|
||||
// return 0, false
|
||||
// }
|
||||
|
||||
// var (
|
||||
// ErrNotFound = errors.New("not found")
|
||||
// ErrExpired = errors.New("entry expired")
|
||||
// )
|
|
@ -1,46 +0,0 @@
|
|||
package kubekv
|
||||
|
||||
import (
|
||||
"github.com/keel-hq/keel/cache"
|
||||
|
||||
"github.com/rusenask/k8s-kv/kv"
|
||||
)
|
||||
|
||||
type KubeKV struct {
|
||||
kv *kv.KV
|
||||
}
|
||||
|
||||
func New(implementer kv.ConfigMapInterface, bucket string) (*KubeKV, error) {
|
||||
|
||||
kvdb, err := kv.New(implementer, "keel", bucket)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &KubeKV{
|
||||
kv: kvdb,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (k *KubeKV) Put(key string, value []byte) error {
|
||||
return k.kv.Put(key, value)
|
||||
}
|
||||
|
||||
func (k *KubeKV) Get(key string) (value []byte, err error) {
|
||||
value, err = k.kv.Get(key)
|
||||
if err != nil {
|
||||
if err == kv.ErrNotFound {
|
||||
return []byte(""), cache.ErrNotFound
|
||||
}
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (k *KubeKV) Delete(key string) error {
|
||||
return k.kv.Delete(key)
|
||||
}
|
||||
|
||||
func (k *KubeKV) List(prefix string) (map[string][]byte, error) {
|
||||
return k.kv.List(prefix)
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
package memory
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/keel-hq/keel/cache"
|
||||
)
|
||||
|
||||
type Cache struct {
|
||||
entries map[string][]byte
|
||||
mu *sync.RWMutex
|
||||
}
|
||||
|
||||
func NewMemoryCache() *Cache {
|
||||
return &Cache{
|
||||
entries: make(map[string][]byte),
|
||||
mu: &sync.RWMutex{},
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (c *Cache) Put(key string, value []byte) error {
|
||||
c.mu.Lock()
|
||||
c.entries[key] = value
|
||||
c.mu.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
func (c *Cache) Get(key string) ([]byte, error) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
// result := make([]byte, len(k))
|
||||
|
||||
res, ok := c.entries[key]
|
||||
if !ok {
|
||||
return nil, cache.ErrNotFound
|
||||
}
|
||||
|
||||
dst := make([]byte, len(res))
|
||||
copy(dst, res)
|
||||
|
||||
return dst, nil
|
||||
}
|
||||
func (c *Cache) Delete(key string) error {
|
||||
c.mu.Lock()
|
||||
delete(c.entries, key)
|
||||
c.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
func (c *Cache) List(prefix string) (map[string][]byte, error) {
|
||||
c.mu.RLock()
|
||||
values := make(map[string][]byte)
|
||||
|
||||
for k, v := range c.entries {
|
||||
if strings.HasPrefix(k, prefix) {
|
||||
dst := make([]byte, len(v))
|
||||
copy(dst, v)
|
||||
values[k] = dst
|
||||
}
|
||||
}
|
||||
|
||||
c.mu.RUnlock()
|
||||
return values, nil
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
package memory
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCacheSetGet(t *testing.T) {
|
||||
c := NewMemoryCache()
|
||||
|
||||
err := c.Put("a", []byte("b"))
|
||||
if err != nil {
|
||||
t.Errorf("failed to SET a key, got error: %s", err)
|
||||
}
|
||||
|
||||
val, err := c.Get("a")
|
||||
if err != nil {
|
||||
t.Errorf("failed to GET a key, got error: %s", err)
|
||||
}
|
||||
|
||||
if string(val) != "b" {
|
||||
log.Panicf("value %v", val)
|
||||
}
|
||||
|
||||
cc, _ := c.List("")
|
||||
if len(cc) != 1 {
|
||||
t.Errorf("expected 1 item, got: %d", len(cc))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCacheDel(t *testing.T) {
|
||||
c := NewMemoryCache()
|
||||
|
||||
err := c.Put("a", []byte("b"))
|
||||
if err != nil {
|
||||
t.Errorf("failed to SET a key, got error: %s", err)
|
||||
}
|
||||
|
||||
val, err := c.Get("a")
|
||||
if err != nil {
|
||||
t.Errorf("failed to GET a key, got error: %s", err)
|
||||
}
|
||||
|
||||
if string(val) != "b" {
|
||||
log.Panicf("value %v", val)
|
||||
}
|
||||
|
||||
err = c.Delete("a")
|
||||
if err != nil {
|
||||
t.Errorf("faield to delete entry, got error: %s", err)
|
||||
}
|
||||
|
||||
_, err = c.Get("a")
|
||||
if err == nil {
|
||||
t.Errorf("expected to get an error after deletion, but got nil")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue