diff --git a/store/memcache/memcache.go b/store/memcache/memcache.go index 61a621aa..41b5a079 100644 --- a/store/memcache/memcache.go +++ b/store/memcache/memcache.go @@ -50,6 +50,9 @@ func NewMemcache(client MemcacheClientInterface, options ...lib_store.Option) *M func (s *MemcacheStore) Get(_ context.Context, key any) (any, error) { item, err := s.client.Get(key.(string)) if err != nil { + if errors.Is(err, memcache.ErrCacheMiss) { + return nil, lib_store.NotFoundWithCause(err) + } return nil, err } if item == nil { @@ -63,6 +66,9 @@ func (s *MemcacheStore) Get(_ context.Context, key any) (any, error) { func (s *MemcacheStore) GetWithTTL(_ context.Context, key any) (any, time.Duration, error) { item, err := s.client.Get(key.(string)) if err != nil { + if errors.Is(err, memcache.ErrCacheMiss) { + return nil, 0, lib_store.NotFoundWithCause(err) + } return nil, 0, err } if item == nil { @@ -159,7 +165,11 @@ func (s *MemcacheStore) addKeyToTagValue(tagKey string, key any) error { // Delete removes data from Memcache for given key identifier func (s *MemcacheStore) Delete(_ context.Context, key any) error { - return s.client.Delete(key.(string)) + err := s.client.Delete(key.(string)) + if errors.Is(err, memcache.ErrCacheMiss) { + return nil + } + return err } // Invalidate invalidates some cache data in Memcache for given options diff --git a/store/memcache/memcache_test.go b/store/memcache/memcache_test.go index 03dfb107..6e809cd1 100644 --- a/store/memcache/memcache_test.go +++ b/store/memcache/memcache_test.go @@ -51,6 +51,27 @@ func TestMemcacheGet(t *testing.T) { assert.Equal(t, cacheValue, value) } +func TestMemcacheGetWithMissingItem(t *testing.T) { + // Given + ctrl := gomock.NewController(t) + + ctx := context.Background() + + cacheKey := "my-key" + + client := NewMockMemcacheClientInterface(ctrl) + client.EXPECT().Get(cacheKey).Return(nil, memcache.ErrCacheMiss) + + store := NewMemcache(client, lib_store.WithExpiration(3*time.Second)) + + // When + value, err := store.Get(ctx, cacheKey) + + // Then + assert.ErrorIs(t, err, lib_store.NotFound{}) + assert.Nil(t, value) +} + func TestMemcacheGetWhenError(t *testing.T) { // Given ctrl := gomock.NewController(t) @@ -109,7 +130,7 @@ func TestMemcacheGetWithTTLWhenMissingItem(t *testing.T) { cacheKey := "my-key" client := NewMockMemcacheClientInterface(ctrl) - client.EXPECT().Get(cacheKey).Return(nil, nil) + client.EXPECT().Get(cacheKey).Return(nil, memcache.ErrCacheMiss) store := NewMemcache(client, lib_store.WithExpiration(3*time.Second)) @@ -117,7 +138,7 @@ func TestMemcacheGetWithTTLWhenMissingItem(t *testing.T) { value, ttl, err := store.GetWithTTL(ctx, cacheKey) // Then - assert.NotNil(t, err) + assert.ErrorIs(t, err, lib_store.NotFound{}) assert.Nil(t, value) assert.Equal(t, 0*time.Second, ttl) } @@ -296,6 +317,26 @@ func TestMemcacheDelete(t *testing.T) { assert.Nil(t, err) } +func TestMemcacheDeleteWithMissingItem(t *testing.T) { + // Given + ctrl := gomock.NewController(t) + + ctx := context.Background() + + cacheKey := "my-key" + + client := NewMockMemcacheClientInterface(ctrl) + client.EXPECT().Delete(cacheKey).Return(memcache.ErrCacheMiss) + + store := NewMemcache(client) + + // When + err := store.Delete(ctx, cacheKey) + + // Then + assert.Nil(t, err) +} + func TestMemcacheDeleteWhenError(t *testing.T) { // Given ctrl := gomock.NewController(t)