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

Get multiple cached values #379

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
28 changes: 28 additions & 0 deletions bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,34 @@ func (c *BigCache) Get(key string) ([]byte, error) {
return shard.get(key, hashedKey)
}

// 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) {

entries := make([][]byte,len(keys))
var err error
shardsLocked := make(map[uint64]bool)
raeidish marked this conversation as resolved.
Show resolved Hide resolved

for i := range keys{
hashedKey := c.hash.Sum64(keys[i])
shard := c.getShard(hashedKey)
raeidish marked this conversation as resolved.
Show resolved Hide resolved

if !shardsLocked[hashedKey & c.shardMask]{
shard.lock.RLock()
shardsLocked[hashedKey & c.shardMask] = true
defer shard.lock.RUnlock()
raeidish marked this conversation as resolved.
Show resolved Hide resolved
}

entries[i],err = shard.getWithoutLock(keys[i],hashedKey)
if err != nil{
return nil,err
}
}

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
83 changes: 83 additions & 0 deletions bigcache_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,89 @@ 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()
raeidish marked this conversation as resolved.
Show resolved Hide resolved
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
31 changes: 25 additions & 6 deletions shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Metadata struct {
}

type cacheShard struct {
hashmap map[uint64]uint32
hashmap map[uint64]uint64
raeidish marked this conversation as resolved.
Show resolved Hide resolved
entries queue.BytesQueue
lock sync.RWMutex
entryBuffer []byte
Expand Down 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) {
raeidish marked this conversation as resolved.
Show resolved Hide resolved
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 Expand Up @@ -140,7 +159,7 @@ func (s *cacheShard) set(key string, hashedKey uint64, entry []byte) error {

for {
if index, err := s.entries.Push(w); err == nil {
s.hashmap[hashedKey] = uint32(index)
s.hashmap[hashedKey] = uint64(index)
s.lock.Unlock()
return nil
}
Expand All @@ -164,7 +183,7 @@ func (s *cacheShard) addNewWithoutLock(key string, hashedKey uint64, entry []byt

for {
if index, err := s.entries.Push(w); err == nil {
s.hashmap[hashedKey] = uint32(index)
s.hashmap[hashedKey] = uint64(index)
return nil
}
if s.removeOldestEntry(NoSpace) != nil {
Expand All @@ -188,7 +207,7 @@ func (s *cacheShard) setWrappedEntryWithoutLock(currentTimestamp uint64, w []byt

for {
if index, err := s.entries.Push(w); err == nil {
s.hashmap[hashedKey] = uint32(index)
s.hashmap[hashedKey] = uint64(index)
return nil
}
if s.removeOldestEntry(NoSpace) != nil {
Expand Down Expand Up @@ -347,7 +366,7 @@ func (s *cacheShard) removeOldestEntry(reason RemoveReason) error {

func (s *cacheShard) reset(config Config) {
s.lock.Lock()
s.hashmap = make(map[uint64]uint32, config.initialShardSize())
s.hashmap = make(map[uint64]uint64, config.initialShardSize())
s.entryBuffer = make([]byte, config.MaxEntrySize+headersSizeInBytes)
s.entries.Reset()
s.lock.Unlock()
Expand Down Expand Up @@ -438,7 +457,7 @@ func initNewShard(config Config, callback onRemoveCallback, clock clock) *cacheS
bytesQueueInitialCapacity = maximumShardSizeInBytes
}
return &cacheShard{
hashmap: make(map[uint64]uint32, config.initialShardSize()),
hashmap: make(map[uint64]uint64, config.initialShardSize()),
hashmapStats: make(map[uint64]uint32, config.initialShardSize()),
entries: *queue.NewBytesQueue(bytesQueueInitialCapacity, maximumShardSizeInBytes, config.Verbose),
entryBuffer: make([]byte, config.MaxEntrySize+headersSizeInBytes),
Expand Down
Loading