Skip to content

Commit

Permalink
feat: add cache options
Browse files Browse the repository at this point in the history
  • Loading branch information
Han-Ya-Jun committed Jul 30, 2024
1 parent b73efb5 commit 9608ba0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
8 changes: 5 additions & 3 deletions cache/memory/base_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ func (c *BaseCache) doRetrieve(ctx context.Context, k cache.Key) (interface{}, e
return c.retrieveFunc(ctx, k)
})

if err != nil && c.withEmptyCache {
// ! if error, cache it too, make it short enough(5s)
c.backend.Set(key, EmptyCache{err: err}, c.emptyCacheExpireDuration)
if err != nil {
if c.withEmptyCache {
// ! if error, cache it too, make it short enough(5s)
c.backend.Set(key, EmptyCache{err: err}, c.emptyCacheExpireDuration)
}
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions cache/memory/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
// - expiration: the expiration time.
// - randomExtraExpirationFunc: the function to generate a random duration, used to add extra expiration for each key.
// - options: the options for the cache . eg:
// WithNoCache(disable cache)
// WithEmptyCache(set the key EmptyCache if retrieve fail from retrieveFunc)
// WithNoCache:disable cache
// WithEmptyCache(duration): set the key EmptyCache if retrieve fail from retrieveFunc
func NewCache(
name string,
retrieveFunc RetrieveFunc,
Expand Down
24 changes: 24 additions & 0 deletions cache/memory/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package memory

import (
"context"
"errors"
"time"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -25,7 +26,14 @@ func retrieveOK(ctx context.Context, k cache.Key) (interface{}, error) {
return "ok", nil
}

func retrieveErr(ctx context.Context, k cache.Key) (interface{}, error) {
return "ok", errors.ErrUnsupported

Check failure on line 30 in cache/memory/cache_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: errors.ErrUnsupported
}

var _ = Describe("Cache", func() {

var ctx context.Context

It("New", func() {
expiration := 5 * time.Minute

Expand All @@ -37,4 +45,20 @@ var _ = Describe("Cache", func() {
c := NewMockCache(retrieveOK)
assert.NotNil(GinkgoT(), c)
})

It("Cache Disable", func() {
expiration := 5 * time.Minute
c := NewCache("test", retrieveOK, expiration, nil, WithNoCache())
assert.True(GinkgoT(), c.Disabled())
})

It("Cache WithEmptyCache", func() {
aKey := cache.NewStringKey("test")
expiration := 5 * time.Minute
c := NewCache("test", retrieveErr, expiration, nil, WithEmptyCache(0))
_, err := c.Get(ctx, aKey)
assert.ErrorIs(GinkgoT(), err, errors.ErrUnsupported)

Check failure on line 60 in cache/memory/cache_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: errors.ErrUnsupported

})

})

0 comments on commit 9608ba0

Please sign in to comment.