simplifying kv cache
parent
8f9d6cd184
commit
346ae6a2b8
|
@ -7,11 +7,17 @@ import (
|
|||
)
|
||||
|
||||
// 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(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)
|
||||
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
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -84,7 +83,7 @@ func (c *Cache) service() {
|
|||
req := <-c.requestChannel
|
||||
resp := &response{}
|
||||
switch req.requestType {
|
||||
case GET:
|
||||
case GET:
|
||||
val, ok := c.cache[req.key]
|
||||
if !ok {
|
||||
resp.error = cache.ErrNotFound
|
||||
|
@ -129,7 +128,7 @@ func (c *Cache) service() {
|
|||
}
|
||||
|
||||
// Get - looks up value and returns it
|
||||
func (c *Cache) Get(ctx context.Context, key string) ([]byte, error) {
|
||||
func (c *Cache) Get(key string) ([]byte, error) {
|
||||
respChannel := make(chan *response)
|
||||
c.requestChannel <- &request{
|
||||
requestType: GET,
|
||||
|
@ -141,7 +140,7 @@ func (c *Cache) Get(ctx context.Context, key string) ([]byte, error) {
|
|||
}
|
||||
|
||||
// Put - sets key/string. Overwrites existing key
|
||||
func (c *Cache) Put(ctx context.Context, key string, value []byte) error {
|
||||
func (c *Cache) Put(key string, value []byte) error {
|
||||
respChannel := make(chan *response)
|
||||
c.requestChannel <- &request{
|
||||
requestType: SET,
|
||||
|
@ -154,7 +153,7 @@ func (c *Cache) Put(ctx context.Context, key string, value []byte) error {
|
|||
}
|
||||
|
||||
// Delete - deletes key
|
||||
func (c *Cache) Delete(ctx context.Context, key string) error {
|
||||
func (c *Cache) Delete(key string) error {
|
||||
respChannel := make(chan *response)
|
||||
c.requestChannel <- &request{
|
||||
requestType: DELETE,
|
||||
|
@ -166,7 +165,7 @@ func (c *Cache) Delete(ctx context.Context, key string) error {
|
|||
}
|
||||
|
||||
// List all values for specified prefix
|
||||
func (c *Cache) List(prefix string) ([][]byte, error) {
|
||||
func (c *Cache) List(prefix string) (map[string][]byte, error) {
|
||||
respChannel := make(chan *response)
|
||||
c.requestChannel <- &request{
|
||||
requestType: COPY,
|
||||
|
@ -174,11 +173,11 @@ func (c *Cache) List(prefix string) ([][]byte, error) {
|
|||
}
|
||||
resp := <-respChannel
|
||||
|
||||
var list [][]byte
|
||||
list := make(map[string][]byte)
|
||||
|
||||
for k, v := range resp.mapCopy {
|
||||
if strings.HasPrefix(k, prefix) {
|
||||
list = append(list, v)
|
||||
list[k] = v
|
||||
}
|
||||
}
|
||||
return list, nil
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -10,12 +9,12 @@ import (
|
|||
func TestCacheSetGet(t *testing.T) {
|
||||
c := NewMemoryCache(100*time.Millisecond, 100*time.Millisecond, 10*time.Millisecond)
|
||||
|
||||
err := c.Put(context.Background(), "a", []byte("b"))
|
||||
err := c.Put("a", []byte("b"))
|
||||
if err != nil {
|
||||
t.Errorf("failed to SET a key, got error: %s", err)
|
||||
}
|
||||
|
||||
val, err := c.Get(context.Background(), "a")
|
||||
val, err := c.Get("a")
|
||||
if err != nil {
|
||||
t.Errorf("failed to GET a key, got error: %s", err)
|
||||
}
|
||||
|
@ -33,12 +32,12 @@ func TestCacheSetGet(t *testing.T) {
|
|||
func TestCacheDel(t *testing.T) {
|
||||
c := NewMemoryCache(100*time.Millisecond, 100*time.Millisecond, 10*time.Millisecond)
|
||||
|
||||
err := c.Put(context.Background(), "a", []byte("b"))
|
||||
err := c.Put("a", []byte("b"))
|
||||
if err != nil {
|
||||
t.Errorf("failed to SET a key, got error: %s", err)
|
||||
}
|
||||
|
||||
val, err := c.Get(context.Background(), "a")
|
||||
val, err := c.Get("a")
|
||||
if err != nil {
|
||||
t.Errorf("failed to GET a key, got error: %s", err)
|
||||
}
|
||||
|
@ -47,12 +46,12 @@ func TestCacheDel(t *testing.T) {
|
|||
log.Panicf("value %v", val)
|
||||
}
|
||||
|
||||
err = c.Delete(context.Background(), "a")
|
||||
err = c.Delete("a")
|
||||
if err != nil {
|
||||
t.Errorf("faield to delete entry, got error: %s", err)
|
||||
}
|
||||
|
||||
_, err = c.Get(context.Background(), "a")
|
||||
_, err = c.Get("a")
|
||||
if err == nil {
|
||||
t.Errorf("expected to get an error after deletion, but got nil")
|
||||
}
|
||||
|
@ -61,12 +60,12 @@ func TestCacheDel(t *testing.T) {
|
|||
func TestCacheExpiration(t *testing.T) {
|
||||
c := NewMemoryCache(100*time.Millisecond, 100*time.Millisecond, 10*time.Millisecond)
|
||||
|
||||
err := c.Put(context.Background(), "a", []byte("b"))
|
||||
err := c.Put("a", []byte("b"))
|
||||
if err != nil {
|
||||
t.Errorf("failed to SET a key, got error: %s", err)
|
||||
}
|
||||
|
||||
val, err := c.Get(context.Background(), "a")
|
||||
val, err := c.Get("a")
|
||||
if err != nil {
|
||||
t.Errorf("failed to GET a key, got error: %s", err)
|
||||
}
|
||||
|
@ -77,7 +76,7 @@ func TestCacheExpiration(t *testing.T) {
|
|||
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
_, err = c.Get(context.Background(), "a")
|
||||
_, err = c.Get("a")
|
||||
if err == nil {
|
||||
t.Errorf("expected to get an error after deletion, but got nil")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue