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

Add GetWithFallback method to fallback non existent or expired keys #306

Open
wants to merge 1 commit 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
26 changes: 26 additions & 0 deletions bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,32 @@ func (c *BigCache) Get(key string) ([]byte, error) {
return shard.get(key, hashedKey)
}

// GetWithFallback reads entry for the key and if it is not exists
// tries to call second argument callback function to resolve actual value,
// after tries to save it as new cached value for the key and returns it as result.
func (c *BigCache) GetWithFallback(key string, fallbackFn func() ([]byte, error)) ([]byte, error) {
entry, err := c.Get(key)
if err == nil {
return entry, nil
}

if err != ErrEntryNotFound {
return nil, err
}

entry, err = fallbackFn()
if err != nil {
return nil, err
}

err = c.Set(key, entry)
if err != nil {
return nil, err
}

return entry, nil
}

// GetWithInfo reads entry for the key with Response info.
// It returns an ErrEntryNotFound when
// no entry exists for the given key.
Expand Down
34 changes: 34 additions & 0 deletions bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bigcache

import (
"bytes"
"errors"
"fmt"
"math"
"math/rand"
Expand All @@ -28,6 +29,39 @@ func TestWriteAndGetOnCache(t *testing.T) {
assertEqual(t, value, cachedValue)
}

func TestGetWithFallbackOnCache(t *testing.T) {
t.Parallel()

// given
cache, _ := NewBigCache(DefaultConfig(5 * time.Second))
value := []byte("value")

// when
cachedValue, err := cache.GetWithFallback("key", func() ([]byte, error) {
return value, nil
})

// then
noError(t, err)
assertEqual(t, value, cachedValue)
}

func TestGetWithFallbackOnCacheWithError(t *testing.T) {
t.Parallel()

// given
cache, _ := NewBigCache(DefaultConfig(5 * time.Second))
errStub := errors.New("some error")

// when
_, err := cache.GetWithFallback("key", func() ([]byte, error) {
return nil, errStub
})

// then
assertEqual(t, errStub, err)
}

func TestAppendAndGetOnCache(t *testing.T) {
t.Parallel()

Expand Down
12 changes: 12 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ func Example() {
// Output: value
}

func Example_getWithFallback() {
cache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(10 * time.Minute))

entry, _ := cache.GetWithFallback("my-unique-key", func() ([]byte, error) {
/* Some amount of code for resolving value... */
return []byte("value"), nil
})

fmt.Println(string(entry))
// Output: value
}

func Example_custom() {
// When cache load can be predicted in advance then it is better to use custom initialization
// because additional memory allocation can be avoided in that way.
Expand Down