Skip to content

Commit

Permalink
Fix multi cache without context
Browse files Browse the repository at this point in the history
  • Loading branch information
Trọng Đức committed Aug 7, 2020
1 parent 601727c commit 3995528
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 40 deletions.
12 changes: 12 additions & 0 deletions src/cmd/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,48 @@ func NewMultiCache(size int, expiration time.Duration, redisCache redis.Universa
}, nil
}

func (c *MultiCache) wrapContext(ctx context.Context) context.Context {
if ctx == nil {
return context.Background()
}
return ctx
}

//Set cache to redis
func (c *MultiCache) redisSet(ctx context.Context, key string, values []byte, expiration time.Duration) error {
ctx = c.wrapContext(ctx)
span, _ := apm.StartSpan(ctx, "redis.set", "cache.multi_cache")
defer span.End()
return c.Redis.Set(key, values, expiration).Err()
}

//Set cache to LRU
func (c *MultiCache) lruSet(ctx context.Context, key string, values []byte, expiration time.Duration) bool {
ctx = c.wrapContext(ctx)
span, _ := apm.StartSpan(ctx, "lru.set", "cache.multi_cache")
defer span.End()
return c.LRU.Add(key, values, expiration)
}

//Get cache from redis
func (c *MultiCache) redisGet(ctx context.Context, key string) ([]byte, error) {
ctx = c.wrapContext(ctx)
span, _ := apm.StartSpan(ctx, "redis.get", "cache.multi_cache")
defer span.End()
return c.Redis.Get(key).Bytes()
}

//Get ttl cache from redis
func (c *MultiCache) redisGetTTL(ctx context.Context, key string) (time.Duration, error) {
ctx = c.wrapContext(ctx)
span, _ := apm.StartSpan(ctx, "redis.get_ttl", "cache.multi_cache")
defer span.End()
return c.Redis.TTL(key).Result()
}

//Get cache from lru
func (c *MultiCache) lruGet(ctx context.Context, key string) (interface{}, bool) {
ctx = c.wrapContext(ctx)
span, _ := apm.StartSpan(ctx, "lru.get", "cache.multi_cache")
defer span.End()
return c.LRU.Get(key)
Expand Down
40 changes: 0 additions & 40 deletions src/cmd/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,43 +46,3 @@ func TestMultiCache(t *testing.T) {
t.Fatalf("Expected is nil but actual is %v", err)
}
}

func TestMultiCacheContext(t *testing.T) {
r, err := redis.NewDefaultRedisUniversalClient()
if err != nil {
t.Fatal(err)
}

c, err := NewMultiCache(100, 5 * time.Second, r)
if err != nil {
t.Fatal(err)
}

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

v, err := c.Get("foo")
if err != nil {
t.Fatal(err)
}

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

time.Sleep(2 * time.Second)
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.Get("foo")
if err != rd.Nil {
t.Fatalf("Expected is nil but actual is %v", err)
}
}

0 comments on commit 3995528

Please sign in to comment.