From 3b557da573f8cd15345f494bffa48f6b1b64bb59 Mon Sep 17 00:00:00 2001 From: jaxliu Date: Fri, 6 Oct 2023 00:18:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=A4=E6=96=AD=20redis=20client=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC=EF=BC=8C=E5=85=BC=E5=AE=B9=204.0=20=E4=B9=8B?= =?UTF-8?q?=E5=89=8D=20HSet=20=E4=B8=8D=E6=94=AF=E6=8C=81=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E5=A4=9A=E4=B8=AAfield?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/services/connection_service.go | 33 ++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/backend/services/connection_service.go b/backend/services/connection_service.go index 06c3943..0dab0ad 100644 --- a/backend/services/connection_service.go +++ b/backend/services/connection_service.go @@ -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{}