Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 26 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 34 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 12 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.