fix: HSET command causes error when accepts multiple field and value #44

This commit is contained in:
tiny-craft 2023-10-05 20:39:28 +08:00
parent aad4bcf17b
commit ec7a7f18e9
2 changed files with 23 additions and 9 deletions

View File

@ -662,11 +662,17 @@ func (c *connectionService) SetKeyValue(connName string, db int, key, keyType st
resp.Msg = "invalid hash value" resp.Msg = "invalid hash value"
return return
} else { } else {
if len(strs) > 1 { total := len(strs)
err = rdb.HSet(ctx, key, strs).Err() if total > 1 {
if err == nil && expiration > 0 { _, err = rdb.Pipelined(ctx, func(pipe redis.Pipeliner) error {
rdb.Expire(ctx, key, expiration) 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": case "set":
@ -795,9 +801,17 @@ func (c *connectionService) AddHashField(connName string, db int, key string, ac
} }
default: default:
// overwrite duplicated fields // overwrite duplicated fields
_, err = rdb.HSet(ctx, key, fieldItems...).Result() total := len(fieldItems)
for i := 0; i < len(fieldItems); i += 2 { if total > 1 {
updated[fieldItems[i].(string)] = fieldItems[i+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 { if err != nil {

View File

@ -187,7 +187,7 @@ const onClose = () => {
{{ $t('common.second') }} {{ $t('common.second') }}
</template> </template>
</n-input-number> </n-input-number>
<n-button secondary type="primary" :focusable="false" @click="newForm.ttl = -1"> <n-button secondary type="primary" :focusable="false" @click="() => (newForm.ttl = -1)">
{{ $t('dialogue.key.persist_key') }} {{ $t('dialogue.key.persist_key') }}
</n-button> </n-button>
</n-input-group> </n-input-group>