From 610648141799bd2df9b41f211f967d0bd0a92711 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 17 Dec 2023 17:51:50 +0100 Subject: [PATCH] app/cache: implement store interface. --- app/cache/memory_store.go | 7 ++++--- app/cache/memory_store_test.go | 19 ++++++++++--------- app/cache/redis_store.go | 18 ++++++++---------- app/cache/redis_store_test.go | 15 ++++++++------- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/app/cache/memory_store.go b/app/cache/memory_store.go index 7c3bd9f..0dbc1be 100644 --- a/app/cache/memory_store.go +++ b/app/cache/memory_store.go @@ -1,6 +1,7 @@ package cache import ( + "context" "fmt" "reflect" "time" @@ -21,7 +22,7 @@ func NewMemoryStore() *MemoryStore { return &MemoryStore{make(map[string]memoryStoreItem)} } -func (s *MemoryStore) Get(key string, value any) error { +func (s *MemoryStore) Get(ctx context.Context, key string, value any) error { if item, ok := s.data[key]; ok { if item.expired.Before(now()) { @@ -41,12 +42,12 @@ func (s *MemoryStore) Get(key string, value any) error { return fmt.Errorf("key: %s does not exist", key) } -func (s *MemoryStore) Has(key string) bool { +func (s *MemoryStore) Has(ctx context.Context, key string) bool { _, hit := s.data[key] return hit } -func (s *MemoryStore) Set(key string, value any, ttl time.Duration) error { +func (s *MemoryStore) Set(ctx context.Context, key string, value any, ttl time.Duration) error { s.data[key] = memoryStoreItem{ value: value, expired: now().Add(ttl), diff --git a/app/cache/memory_store_test.go b/app/cache/memory_store_test.go index d6bc6bd..9c949e3 100644 --- a/app/cache/memory_store_test.go +++ b/app/cache/memory_store_test.go @@ -1,6 +1,7 @@ package cache import ( + "context" "testing" "time" @@ -30,7 +31,7 @@ func TestMemoryStore_Set(t *testing.T) { } store := NewMemoryStore() - err := store.Set("key1", item, time.Hour) + err := store.Set(context.Background(), "key1", item, time.Hour) assert.NoError(t, err) assert.Equal(t, expected, store.data) @@ -40,7 +41,7 @@ func TestMemoryStore_GetMiss(t *testing.T) { store := NewMemoryStore() var v any - err := store.Get("Key2", &v) + err := store.Get(context.Background(), "Key2", &v) assert.Error(t, err) } @@ -52,11 +53,11 @@ func TestMemoryStore_GetHit(t *testing.T) { } store := NewMemoryStore() - err := store.Set("key1", expected, time.Hour) + err := store.Set(context.Background(), "key1", expected, time.Hour) assert.NoError(t, err) var actual memoryTestItem - err = store.Get("key1", &actual) + err = store.Get(context.Background(), "key1", &actual) assert.NoError(t, err) assert.Equal(t, expected, actual) } @@ -69,19 +70,19 @@ func TestMemoryStore_GetNonPointer(t *testing.T) { } store := NewMemoryStore() - err := store.Set("key1", expected, time.Hour) + err := store.Set(context.Background(), "key1", expected, time.Hour) assert.NoError(t, err) var actual string - err = store.Get("key1", actual) + err = store.Get(context.Background(), "key1", actual) assert.EqualError(t, err, "value must be of pointer type, 'string' passed") } func TestMemoryStore_Has(t *testing.T) { store := NewMemoryStore() - err := store.Set("key1", "value", time.Hour) + err := store.Set(context.Background(), "key1", "value", time.Hour) assert.NoError(t, err) - assert.True(t, store.Has("key1")) - assert.False(t, store.Has("key2")) + assert.True(t, store.Has(context.Background(), "key1")) + assert.False(t, store.Has(context.Background(), "key2")) } diff --git a/app/cache/redis_store.go b/app/cache/redis_store.go index 5016a15..54d7f4d 100644 --- a/app/cache/redis_store.go +++ b/app/cache/redis_store.go @@ -8,28 +8,26 @@ import ( ) type RedisStore struct { - c *cache.Cache - ctx context.Context + c *cache.Cache } func NewRedisStore(options *cache.Options) *RedisStore { return &RedisStore{ - c: cache.New(options), - ctx: context.Background(), + c: cache.New(options), } } -func (s *RedisStore) Get(key string, value interface{}) error { - return s.c.Get(s.ctx, key, value) +func (s *RedisStore) Get(ctx context.Context, key string, value interface{}) error { + return s.c.Get(ctx, key, value) } -func (s *RedisStore) Has(key string) bool { - return s.c.Exists(s.ctx, key) +func (s *RedisStore) Has(ctx context.Context, key string) bool { + return s.c.Exists(ctx, key) } -func (s *RedisStore) Set(key string, value any, ttl time.Duration) error { +func (s *RedisStore) Set(ctx context.Context, key string, value any, ttl time.Duration) error { return s.c.Set(&cache.Item{ - Ctx: s.ctx, + Ctx: ctx, Key: key, Value: value, TTL: ttl, diff --git a/app/cache/redis_store_test.go b/app/cache/redis_store_test.go index 34e3f34..f12cc33 100644 --- a/app/cache/redis_store_test.go +++ b/app/cache/redis_store_test.go @@ -1,6 +1,7 @@ package cache import ( + "context" "testing" "time" @@ -32,7 +33,7 @@ func TestRedisStore_Set(t *testing.T) { mock.ExpectSet("mykey", bytes, time.Minute).SetVal("OK") - err = store.Set("mykey", expected, time.Minute) + err = store.Set(context.Background(), "mykey", expected, time.Minute) assert.NoError(t, err) assert.NoError(t, mock.ExpectationsWereMet()) @@ -48,7 +49,7 @@ func TestRedisStore_GetMiss(t *testing.T) { mock.ExpectGet("mykey").SetErr(redis_cache.ErrCacheMiss) expected := testItem{} - err := store.Get("mykey", &expected) + err := store.Get(context.Background(), "mykey", &expected) assert.ErrorIs(t, err, redis_cache.ErrCacheMiss) assert.NoError(t, mock.ExpectationsWereMet()) } @@ -71,11 +72,11 @@ func TestRedisStore_GetHit(t *testing.T) { mock.ExpectSet("mykey2", bytes, time.Second*20).SetVal("OK") mock.ExpectGet("mykey2").SetVal(string(bytes)) - err = store.Set("mykey2", expected, time.Second*20) + err = store.Set(context.Background(), "mykey2", expected, time.Second*20) assert.NoError(t, err) actual := testItem{} - err = store.Get("mykey2", &actual) + err = store.Get(context.Background(), "mykey2", &actual) assert.NoError(t, err) assert.Equal(t, expected, actual) assert.NoError(t, mock.ExpectationsWereMet()) @@ -95,9 +96,9 @@ func TestRedisStore_Has(t *testing.T) { mock.ExpectGet("key1").SetVal(string(bytes)) mock.ExpectGet("key2").RedisNil() - err = store.Set("key1", "value", time.Minute*15) + err = store.Set(context.Background(), "key1", "value", time.Minute*15) assert.NoError(t, err) - assert.True(t, store.Has("key1")) - assert.False(t, store.Has("key2")) + assert.True(t, store.Has(context.Background(), "key1")) + assert.False(t, store.Has(context.Background(), "key2")) assert.NoError(t, mock.ExpectationsWereMet()) }