fix issue 4311

This commit is contained in:
AllenX2018
2020-11-17 20:53:33 +08:00
parent 05d8e293f7
commit 6225f0c1e9
8 changed files with 136 additions and 31 deletions

View File

@@ -18,6 +18,8 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
"time"
)
@@ -68,22 +70,28 @@ func (bc *MemoryCache) Get(ctx context.Context, key string) (interface{}, error)
}
return itm.val, nil
}
return nil, nil
return nil, errors.New("the key isn't exist")
}
// GetMulti gets caches from memory.
// If non-existent or expired, return nil.
func (bc *MemoryCache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error) {
var rc []interface{}
for _, name := range keys {
val, err := bc.Get(context.Background(), name)
rc := make([]interface{}, len(keys))
keysErr := make([]string, 0)
for i, ki := range keys {
val, err := bc.Get(context.Background(), ki)
if err != nil {
rc = append(rc, err)
} else {
rc = append(rc, val)
keysErr = append(keysErr, fmt.Sprintf("key [%s] error: %s", ki, err.Error()))
continue
}
rc[i] = val
}
return rc, nil
if len(keysErr) == 0 {
return rc, nil
}
return rc, errors.New(strings.Join(keysErr, "; "))
}
// Put puts cache into memory.