fix: 判断 redis client 版本,兼容 4.0 之前 HSet 不支持设置多个field

This commit is contained in:
jaxliu 2023-10-06 00:18:15 +08:00
parent d33d723eee
commit 3b557da573
1 changed files with 31 additions and 2 deletions

View File

@ -663,7 +663,13 @@ func (c *connectionService) SetKeyValue(connName string, db int, key, keyType st
return
} else {
if len(strs) > 1 {
err = rdb.HMSet(ctx, key, strs).Err()
version := getRedisVersion(rdb, ctx)
if strings.HasPrefix(version, "2.") || strings.HasPrefix(version, "3.") {
// 4.0以前老版本redis
err = rdb.HMSet(ctx, key, strs).Err()
} else {
err = rdb.HSet(ctx, key, strs).Err()
}
if err == nil && expiration > 0 {
rdb.Expire(ctx, key, expiration)
}
@ -795,7 +801,13 @@ func (c *connectionService) AddHashField(connName string, db int, key string, ac
}
default:
// overwrite duplicated fields
_, err = rdb.HMSet(ctx, key, fieldItems...).Result()
version := getRedisVersion(rdb, ctx)
if strings.HasPrefix(version, "2.") || strings.HasPrefix(version, "3.") {
// 4.0以前老版本redis
_, err = rdb.HMSet(ctx, key, fieldItems...).Result()
} else {
_, err = rdb.HSet(ctx, key, fieldItems...).Result()
}
for i := 0; i < len(fieldItems); i += 2 {
updated[fieldItems[i].(string)] = fieldItems[i+1]
}
@ -1166,6 +1178,23 @@ func (c *connectionService) CleanCmdHistory() (resp types.JSResp) {
return
}
func getRedisVersion(rdb *redis.Client, ctx context.Context) string {
var version = ""
info, err := rdb.Info(ctx, "server").Result()
if err != nil {
return version
}
// 解析 Redis 版本信息
infoStr := strings.Split(info, "\r\n")
for _, line := range infoStr {
if strings.HasPrefix(line, "redis_version:") {
version = line[len("redis_version:"):]
break
}
}
return version
}
// update or insert key info to database
//func (c *connectionService) updateDBKey(connName string, db int, keys []string, separator string) {
// dbStruct := map[string]any{}