Skip to content

Commit

Permalink
wip return found entries+test
Browse files Browse the repository at this point in the history
  • Loading branch information
raeidish committed Oct 17, 2023
1 parent 2a6bc3e commit 556e314
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
17 changes: 6 additions & 11 deletions bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (c *BigCache) Get(key string) ([]byte, error) {
return shard.get(key, hashedKey)
}

// Used to sore information about keys in GetMulti function
// Used to store 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
Expand All @@ -148,9 +148,9 @@ type keyInfo struct {
}

// 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) {
// returns entries in the same order as the provided keys.
// if entry is not found for a given key, the index will contain nil
func (c *BigCache) GetMulti(keys []string) ([][]byte) {
shards := make(map[uint64][]keyInfo, len(c.shards))
entries := make([][]byte, len(keys))

Expand All @@ -165,17 +165,12 @@ func (c *BigCache) GetMulti(keys []string) ([][]byte, error) {
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
}
entry,_ := shard.getWithoutLock(keyInfos[i].key, keyInfos[i].hashedKey)
entries[keyInfos[i].order] = entry
}
shard.lock.RUnlock()
}
return entries, nil
return entries
}

// GetWithInfo reads entry for the key with Response info.
Expand Down
53 changes: 39 additions & 14 deletions bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,49 @@ func TestWriteAndGetOnCache(t *testing.T) {

func TestWriteAndGetOnCacheMulti(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
keys []string
data [][]byte
want string
}{
{
keys: []string{"k1","k2","k3","k4","k5"},
data: [][]byte{
blob('a',10),
blob('b',10),
blob('c',10),
blob('d',10),
blob('e',10),
},
want: "Get all values ordered",
},
{
keys: []string{"k1","k2","k3","k4","k5"},
data: [][]byte{
blob('a',10),
blob('b',10),
nil,
blob('d',10),
blob('e',10),
},
want: "Get all values ordered with nil",
},
}{
t.Run(tc.want, func(t *testing.T) {
cache, _ := New(context.Background(), DefaultConfig(5*time.Second))

// 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")}
for i := range tc.keys{
if tc.data[i] != nil{
cache.Set(tc.keys[i],tc.data[i])
}
}

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

// then
noError(t, err)
assertEqual(t,tc.data,cachedValues)
})

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

func TestAppendAndGetOnCache(t *testing.T) {
Expand Down

0 comments on commit 556e314

Please sign in to comment.