Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added context to the CacheStore interface #42

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func cache(
// read cache first
{
respCache := &ResponseCache{}
err := cacheStore.Get(cacheKey, &respCache)
err := cacheStore.Get(c.Request.Context(), cacheKey, &respCache)
if err == nil {
replyWithCache(c, cfg, respCache)
cfg.hitCacheCallback(c)
Expand Down Expand Up @@ -118,7 +118,7 @@ func cache(

// only cache 2xx response
if !c.IsAborted() && cacheWriter.Status() < 300 && cacheWriter.Status() >= 200 {
if err := cacheStore.Set(cacheKey, respCache, cacheDuration); err != nil {
if err := cacheStore.Set(c.Request.Context(), cacheKey, respCache, cacheDuration); err != nil {
cfg.logger.Errorf("set cache key error: %s, cache key: %s", err, cacheKey)
}
}
Expand Down
7 changes: 4 additions & 3 deletions cache_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cache

import (
"context"
"fmt"
"math/rand"
"net/http"
Expand Down Expand Up @@ -232,7 +233,7 @@ func TestPrefixKey(t *testing.T) {

w1 := mockHttpRequest(cachePathMiddleware, requestPath, true)

err := memoryStore.Delete(prefixKey + requestPath)
err := memoryStore.Delete(context.TODO(), prefixKey+requestPath)
require.NoError(t, err)

w2 := mockHttpRequest(cachePathMiddleware, requestPath, true)
Expand Down Expand Up @@ -290,7 +291,7 @@ func TestCustomCacheStrategy(t *testing.T) {
_ = mockHttpRequest(cacheMiddleware, "/cache?uid=1", false)

var val interface{}
err := memoryStore.Get("custom_cache_key_1", &val)
err := memoryStore.Get(context.TODO(), "custom_cache_key_1", &val)
assert.Nil(t, err)
}

Expand All @@ -306,7 +307,7 @@ func TestCacheByRequestURICustomCacheStrategy(t *testing.T) {

w1 := mockHttpRequest(cacheURIMiddleware, "/cache?uid=u1", true)
var val interface{}
err := memoryStore.Get(customKey, &val)
err := memoryStore.Get(context.TODO(), customKey, &val)
assert.Nil(t, err)
time.Sleep(1 * time.Second)

Expand Down
7 changes: 4 additions & 3 deletions persist/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package persist

import (
"context"
"errors"
"time"
)
Expand All @@ -11,11 +12,11 @@ var ErrCacheMiss = errors.New("persist cache miss error")
// CacheStore is the interface of a Cache backend
type CacheStore interface {
// Get retrieves an item from the Cache. if key does not exist in the store, return ErrCacheMiss
Get(key string, value interface{}) error
Get(ctx context.Context, key string, value interface{}) error

// Set sets an item to the Cache, replacing any existing item.
Set(key string, value interface{}, expire time.Duration) error
Set(ctx context.Context, key string, value interface{}, expire time.Duration) error

// Delete removes an item from the Cache. Does nothing if the key is not in the Cache.
Delete(key string) error
Delete(ctx context.Context, key string) error
}
7 changes: 4 additions & 3 deletions persist/memory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package persist

import (
"context"
"errors"
"reflect"
"time"
Expand All @@ -27,17 +28,17 @@ func NewMemoryStore(defaultExpiration time.Duration) *MemoryStore {
}

// Set put key value pair to memory store, and expire after expireDuration
func (c *MemoryStore) Set(key string, value interface{}, expireDuration time.Duration) error {
func (c *MemoryStore) Set(ctx context.Context, key string, value interface{}, expireDuration time.Duration) error {
return c.Cache.SetWithTTL(key, value, expireDuration)
}

// Delete remove key in memory store, do nothing if key doesn't exist
func (c *MemoryStore) Delete(key string) error {
func (c *MemoryStore) Delete(ctx context.Context, key string) error {
return c.Cache.Remove(key)
}

// Get key in memory store, if key doesn't exist, return ErrCacheMiss
func (c *MemoryStore) Get(key string, value interface{}) error {
func (c *MemoryStore) Get(ctx context.Context, key string, value interface{}) error {
val, err := c.Cache.Get(key)
if errors.Is(err, ttlcache.ErrNotFound) {
return ErrCacheMiss
Expand Down
10 changes: 6 additions & 4 deletions persist/memory_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package persist

import (
"github.com/stretchr/testify/require"
"context"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"
)

func TestMemoryStore(t *testing.T) {
memoryStore := NewMemoryStore(1 * time.Minute)

expectVal := "123"
require.Nil(t, memoryStore.Set("test", expectVal, 1*time.Second))
require.Nil(t, memoryStore.Set(context.TODO(), "test", expectVal, 1*time.Second))

value := ""
assert.Nil(t, memoryStore.Get("test", &value))
assert.Nil(t, memoryStore.Get(context.TODO(), "test", &value))
assert.Equal(t, expectVal, value)

time.Sleep(1 * time.Second)
assert.Equal(t, ErrCacheMiss, memoryStore.Get("test", &value))
assert.Equal(t, ErrCacheMiss, memoryStore.Get(context.TODO(), "test", &value))
}
9 changes: 3 additions & 6 deletions persist/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,22 @@ func NewRedisStore(redisClient *redis.Client) *RedisStore {
}

// Set put key value pair to redis, and expire after expireDuration
func (store *RedisStore) Set(key string, value interface{}, expire time.Duration) error {
func (store *RedisStore) Set(ctx context.Context, key string, value interface{}, expire time.Duration) error {
payload, err := Serialize(value)
if err != nil {
return err
}

ctx := context.TODO()
return store.RedisClient.Set(ctx, key, payload, expire).Err()
}

// Delete remove key in redis, do nothing if key doesn't exist
func (store *RedisStore) Delete(key string) error {
ctx := context.TODO()
func (store *RedisStore) Delete(ctx context.Context, key string) error {
return store.RedisClient.Del(ctx, key).Err()
}

// Get retrieves an item from redis, if key doesn't exist, return ErrCacheMiss
func (store *RedisStore) Get(key string, value interface{}) error {
ctx := context.TODO()
func (store *RedisStore) Get(ctx context.Context, key string, value interface{}) error {
payload, err := store.RedisClient.Get(ctx, key).Bytes()

if errors.Is(err, redis.Nil) {
Expand Down