From ec7a7f18e9155ff3b08c29595f5600ac1ad2cc2c Mon Sep 17 00:00:00 2001 From: tiny-craft <137850705+tiny-craft@users.noreply.github.com> Date: Thu, 5 Oct 2023 20:39:28 +0800 Subject: [PATCH] fix: HSET command causes error when accepts multiple field and value #44 --- backend/services/connection_service.go | 30 ++++++++++++++----- .../src/components/dialogs/NewKeyDialog.vue | 2 +- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/backend/services/connection_service.go b/backend/services/connection_service.go index efff948..8ceb6a8 100644 --- a/backend/services/connection_service.go +++ b/backend/services/connection_service.go @@ -662,11 +662,17 @@ func (c *connectionService) SetKeyValue(connName string, db int, key, keyType st resp.Msg = "invalid hash value" return } else { - if len(strs) > 1 { - err = rdb.HSet(ctx, key, strs).Err() - if err == nil && expiration > 0 { - rdb.Expire(ctx, key, expiration) - } + total := len(strs) + if total > 1 { + _, err = rdb.Pipelined(ctx, func(pipe redis.Pipeliner) error { + for i := 0; i < total; i += 2 { + pipe.HSet(ctx, key, strs[i], strs[i+1]) + } + if expiration > 0 { + pipe.Expire(ctx, key, expiration) + } + return nil + }) } } case "set": @@ -795,9 +801,17 @@ func (c *connectionService) AddHashField(connName string, db int, key string, ac } default: // overwrite duplicated fields - _, err = rdb.HSet(ctx, key, fieldItems...).Result() - for i := 0; i < len(fieldItems); i += 2 { - updated[fieldItems[i].(string)] = fieldItems[i+1] + total := len(fieldItems) + if total > 1 { + _, err = rdb.Pipelined(ctx, func(pipe redis.Pipeliner) error { + for i := 0; i < total; i += 2 { + rdb.HSet(ctx, key, fieldItems[i], fieldItems[i+1]) + } + return nil + }) + for i := 0; i < total; i += 2 { + updated[fieldItems[i].(string)] = fieldItems[i+1] + } } } if err != nil { diff --git a/frontend/src/components/dialogs/NewKeyDialog.vue b/frontend/src/components/dialogs/NewKeyDialog.vue index d6fc64d..ad77a3e 100644 --- a/frontend/src/components/dialogs/NewKeyDialog.vue +++ b/frontend/src/components/dialogs/NewKeyDialog.vue @@ -187,7 +187,7 @@ const onClose = () => { {{ $t('common.second') }} - + {{ $t('dialogue.key.persist_key') }}