Skip to content

Commit

Permalink
Merge pull request #3 from 1infras/feature/multiple-cache
Browse files Browse the repository at this point in the history
Refactor multiple cache
  • Loading branch information
ducmeit1 authored Aug 7, 2020
2 parents 89936dc + f358ba7 commit 601727c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 36 deletions.
24 changes: 1 addition & 23 deletions src/cmd/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,7 @@ func (c *MultiCache) Set(key string, values []byte, expiration time.Duration) (b

//Get cache with multiple layers (LRU first, backed by Redis)
func (c *MultiCache) Get(key string) ([]byte, error) {
//Get from LRU
values, ok := c.LRU.Get(key)
if ok {
v := reflect.ValueOf(values)
return v.Bytes(), nil
}

//Get from Redis
v, err := c.Redis.Get(key).Bytes()
if err != nil {
return nil, err
}

//Get TTL from Redis
ttl, err := c.Redis.TTL(key).Result()
if err != nil {
return nil, err
}

//Set back to LRU with TTL
c.LRU.Add(key, v, ttl)

return v, nil
return c.GetCtx(nil, key)
}

//SetCtx cache with value and expiration, support transaction to tracing
Expand Down
9 changes: 8 additions & 1 deletion src/cmd/cache/cache_interface.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package cache

import "time"
import (
"context"
"time"
)

type Cache interface {
//Set cache with optional expiration time, set 0 if the cache would be infinity
Set(key string, values []byte, expiration time.Duration) (bool, error)
//Get cache by key
Get(key string) ([]byte, error)
//SetCtx cache with optional expiration time, set 0 if the cache would be infinity, support tracing
SetCtx(ctx context.Context, key string, values []byte, expiration time.Duration) (bool, error)
//Get cache by key, support tracing
GetCtx(ctx context.Context, key string) ([]byte, error)
}
16 changes: 4 additions & 12 deletions src/cmd/cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package cache

import (
"context"
"github.com/1infras/go-kit/src/cmd/cache/redis"
"github.com/1infras/go-kit/src/cmd/database/elasticsearch"
rd "github.com/go-redis/redis"
"testing"
"time"
Expand Down Expand Up @@ -50,12 +48,6 @@ func TestMultiCache(t *testing.T) {
}

func TestMultiCacheContext(t *testing.T) {
cd := elasticsearch.DefaultAPMConnection()
cd.Active = true
cd.ServiceName = "go-kit"
cd.Environment = "test"
cd.AutoBindEnvironment()
ctx := context.Background()
r, err := redis.NewDefaultRedisUniversalClient()
if err != nil {
t.Fatal(err)
Expand All @@ -66,12 +58,12 @@ func TestMultiCacheContext(t *testing.T) {
t.Fatal(err)
}

_, err = c.SetCtx(ctx, "foo", []byte("bar"), 5 * time.Second)
_, err = c.Set("foo", []byte("bar"), 5 * time.Second)
if err != nil {
t.Fatal(err)
}

v, err := c.GetCtx(ctx, "foo")
v, err := c.Get("foo")
if err != nil {
t.Fatal(err)
}
Expand All @@ -81,15 +73,15 @@ func TestMultiCacheContext(t *testing.T) {
}

time.Sleep(2 * time.Second)
v, err = c.GetCtx(ctx, "foo")
v, err = c.Get("foo")

if p := string(v); p != "bar" {
t.Fatalf("Expected is bar but actual is %v", p)
}

time.Sleep(5 * time.Second)

v, err = c.GetCtx(ctx, "foo")
v, err = c.Get("foo")
if err != rd.Nil {
t.Fatalf("Expected is nil but actual is %v", err)
}
Expand Down

0 comments on commit 601727c

Please sign in to comment.