Skip to content

Get multiple cached values #379

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
41 changes: 41 additions & 0 deletions bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,47 @@ func (c *BigCache) Get(key string) ([]byte, error) {
return shard.get(key, hashedKey)
}

// Used to sore information about keys in GetMulti function
// order is the index in the slice the data should go
// hashedKey is the Sum64 hash of the key
// key is the original key input
type keyInfo struct {
order int
hashedKey uint64
key string
}

// GetMulti reads entry for each of the keys.
// It returns an ErrEntryNotFound when
// no entry exists for the given key.
func (c *BigCache) GetMulti(keys []string) ([][]byte, error) {
shards := make(map[uint64][]keyInfo)
entries := make([][]byte, len(keys))

for i, key := range keys {
hashedKey := c.hash.Sum64(key)
shardIndex := hashedKey & c.shardMask
shards[shardIndex] = append(shards[shardIndex], keyInfo{order: i, hashedKey: hashedKey, key: key})
}

for shardKey, keyInfos := range shards {
shard := c.shards[shardKey]
shard.lock.RLock()

for i := range keyInfos {
entry, err := shard.getWithoutLock(keyInfos[i].key, keyInfos[i].hashedKey)

if err != nil {
shard.lock.RUnlock()
return nil, err
}
entries[keyInfos[i].order] = entry
}
shard.lock.RUnlock()
}
return entries, nil
}

// GetWithInfo reads entry for the key with Response info.
// It returns an ErrEntryNotFound when
// no entry exists for the given key.
Expand Down
82 changes: 82 additions & 0 deletions bigcache_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,88 @@ func BenchmarkReadFromCache(b *testing.B) {
}
}

func BenchmarkReadFromCacheManySingle(b *testing.B) {
for _, shards := range []int{1, 512, 1024, 8192} {
b.Run(fmt.Sprintf("%d-shards", shards), func(b *testing.B) {
cache, _ := New(context.Background(), Config{
Shards: shards,
LifeWindow: 1000 * time.Second,
MaxEntriesInWindow: max(b.N, 100),
MaxEntrySize: 500,
})

keys := make([]string, b.N)
for i := 0; i < b.N; i++ {
keys[i] = fmt.Sprintf("key-%d", i)
cache.Set(keys[i], message)
}

b.ReportAllocs()
b.ResetTimer()
for _, key := range keys {
cache.Get(key)
}

})
}
}

func BenchmarkReadFromCacheManyMulti(b *testing.B) {
for _, shards := range []int{1, 512, 1024, 8192} {
b.Run(fmt.Sprintf("%d-shards", shards), func(b *testing.B) {
cache, _ := New(context.Background(), Config{
Shards: shards,
LifeWindow: 1000 * time.Second,
MaxEntriesInWindow: max(b.N, 100),
MaxEntrySize: 500,
})
keys := make([]string, b.N)
for i := 0; i < b.N; i++ {
keys[i] = fmt.Sprintf("key-%d", i)
cache.Set(keys[i], message)
}

b.ReportAllocs()
b.ResetTimer()
cache.GetMulti(keys)
})
}
}

func BenchmarkReadFromCacheManyMultiBatches(b *testing.B) {
for _, shards := range []int{1, 512, 1024, 8192} {
for _, batchSize := range []int{1, 5, 10, 100} {
b.Run(fmt.Sprintf("%d-shards %d-batchSize", shards, batchSize), func(b *testing.B) {
cache, _ := New(context.Background(), Config{
Shards: shards,
LifeWindow: 1000 * time.Second,
MaxEntriesInWindow: max(b.N, 100),
MaxEntrySize: 500,
})
keys := make([]string, b.N)
for i := 0; i < b.N; i++ {
keys[i] = fmt.Sprintf("key-%d", i)
cache.Set(keys[i], message)
}

batches := make([][]string, 0, (len(keys)+batchSize-1)/batchSize)

for batchSize < len(keys) {
keys, batches = keys[batchSize:], append(batches, keys[0:batchSize:batchSize])
}
batches = append(batches, keys)

b.ReportAllocs()
b.ResetTimer()
for _, b := range batches {
cache.GetMulti(b)

}
})
}
}
}

func BenchmarkReadFromCacheWithInfo(b *testing.B) {
for _, shards := range []int{1, 512, 1024, 8192} {
b.Run(fmt.Sprintf("%d-shards", shards), func(b *testing.B) {
Expand Down
22 changes: 22 additions & 0 deletions bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ func TestWriteAndGetOnCache(t *testing.T) {
assertEqual(t, value, cachedValue)
}

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

// given
cache, _ := New(context.Background(), DefaultConfig(5*time.Second))
keys := []string{"k1", "k2", "k3", "k4", "k5"}
values := [][]byte{[]byte("v1"), []byte("v2"), []byte("v3"), []byte("v4"), []byte("v5")}

// when
for i, key := range keys {
cache.Set(key, values[i])
}
cachedValues, err := cache.GetMulti(keys)

// then
noError(t, err)

for i, cachedValue := range cachedValues {
assertEqual(t, values[i], cachedValue)
}
}

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

Expand Down
19 changes: 19 additions & 0 deletions shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ func (s *cacheShard) get(key string, hashedKey uint64) ([]byte, error) {
return entry, nil
}

func (s *cacheShard) getWithoutLock(key string, hashedKey uint64) ([]byte, error) {
wrappedEntry, err := s.getWrappedEntry(hashedKey)
if err != nil {
return nil, err
}

if entryKey := readKeyFromEntry(wrappedEntry); key != entryKey {
s.collision()
if s.isVerbose {
s.logger.Printf("Collision detected. Both %q and %q have the same hash %x", key, entryKey, hashedKey)
}
return nil, ErrEntryNotFound
}
entry := readEntry(wrappedEntry)
s.hitWithoutLock(hashedKey)

return entry, nil
}

func (s *cacheShard) getWrappedEntry(hashedKey uint64) ([]byte, error) {
itemIndex := s.hashmap[hashedKey]

Expand Down