mirror of
https://github.com/astaxie/beego.git
synced 2025-06-15 08:20:41 +00:00
fix issue 4311
This commit is contained in:
29
client/cache/ssdb/ssdb.go
vendored
29
client/cache/ssdb/ssdb.go
vendored
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -41,23 +42,37 @@ func (rc *Cache) Get(ctx context.Context, key string) (interface{}, error) {
|
||||
// GetMulti gets one or keys values from ssdb.
|
||||
func (rc *Cache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error) {
|
||||
size := len(keys)
|
||||
var values []interface{}
|
||||
values := make([]interface{}, size)
|
||||
if rc.conn == nil {
|
||||
if err := rc.connectInit(); err != nil {
|
||||
return values, err
|
||||
}
|
||||
}
|
||||
|
||||
res, err := rc.conn.Do("multi_get", keys)
|
||||
if err != nil {
|
||||
return values, err
|
||||
}
|
||||
|
||||
resSize := len(res)
|
||||
if err == nil {
|
||||
for i := 1; i < resSize; i += 2 {
|
||||
values = append(values, res[i+1])
|
||||
keyIdx := make(map[string]int)
|
||||
for i := 1; i < resSize; i += 2 {
|
||||
keyIdx[res[i]] = i
|
||||
}
|
||||
|
||||
keysErr := make([]string, 0)
|
||||
for i, ki := range keys {
|
||||
if _, ok := keyIdx[ki]; !ok {
|
||||
keysErr = append(keysErr, fmt.Sprintf("key [%s] error: %s", ki, "the key isn't exist"))
|
||||
continue
|
||||
}
|
||||
return values, nil
|
||||
values[i] = res[keyIdx[ki]+1]
|
||||
}
|
||||
for i := 0; i < size; i++ {
|
||||
values = append(values, err)
|
||||
|
||||
if len(keysErr) != 0 {
|
||||
return values, fmt.Errorf(strings.Join(keysErr, "; "))
|
||||
}
|
||||
|
||||
return values, nil
|
||||
}
|
||||
|
||||
|
14
client/cache/ssdb/ssdb_test.go
vendored
14
client/cache/ssdb/ssdb_test.go
vendored
@ -106,6 +106,20 @@ func TestSsdbcacheCache(t *testing.T) {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
|
||||
vv, err = ssdb.GetMulti(context.Background(), []string{"ssdb", "ssdb11"})
|
||||
if len(vv) != 2 {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if vv[0].(string) != "ssdb" {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if vv[1] != nil {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
if err != nil && err.Error() != "key [ssdb11] error: the key isn't exist" {
|
||||
t.Error("getmulti error")
|
||||
}
|
||||
|
||||
// test clear all done
|
||||
if err = ssdb.ClearAll(context.Background()); err != nil {
|
||||
t.Error("clear all err")
|
||||
|
Reference in New Issue
Block a user