Skip to content

Commit

Permalink
Merge pull request #261 from woutslakhorst/master
Browse files Browse the repository at this point in the history
memcached: fix Delete and Get for missing items
  • Loading branch information
eko authored Dec 23, 2024
2 parents 621044b + f2799e8 commit 2384199
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
12 changes: 11 additions & 1 deletion store/memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
45 changes: 43 additions & 2 deletions store/memcache/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -109,15 +130,15 @@ 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))

// When
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)
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2384199

Please sign in to comment.