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

@@ -33,6 +33,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
@@ -68,19 +69,31 @@ func (rc *Cache) Get(ctx context.Context, key string) (interface{}, error) {
// GetMulti gets a value from a key in memcache.
func (rc *Cache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error) {
var rv []interface{}
rv := make([]interface{}, len(keys))
if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return rv, err
}
}
mv, err := rc.conn.GetMulti(keys)
if err == nil {
for _, v := range mv {
rv = append(rv, v.Value)
}
if err != nil {
return rv, err
}
return rv, err
keysErr := make([]string, 0)
for i, ki := range keys {
if _, ok := mv[ki]; !ok {
keysErr = append(keysErr, fmt.Sprintf("key [%s] error: %s", ki, "the key isn't exist"))
continue
}
rv[i] = mv[ki].Value
}
if len(keysErr) == 0 {
return rv, nil
}
return rv, fmt.Errorf(strings.Join(keysErr, "; "))
}
// Put puts a value into memcache.