From 50ca632a5bd69216dabac43ee73a5ddb1be722b7 Mon Sep 17 00:00:00 2001 From: tiny-craft <137850705+tiny-craft@users.noreply.github.com> Date: Tue, 26 Sep 2023 16:35:28 +0800 Subject: [PATCH] fix: cannot set key on some low version redis with "keepttl" perf: add soft delete key --- backend/services/connection_service.go | 11 ++++++++-- frontend/src/stores/connections.js | 28 ++++++++++++++------------ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/backend/services/connection_service.go b/backend/services/connection_service.go index 4cf7845..e283249 100644 --- a/backend/services/connection_service.go +++ b/backend/services/connection_service.go @@ -527,6 +527,7 @@ func (c *connectionService) GetKeyValue(connName string, db int, key string) (re } // SetKeyValue set value by key +// @param ttl <= 0 means keep current ttl func (c *connectionService) SetKeyValue(connName string, db int, key, keyType string, value any, ttl int64) (resp types.JSResp) { rdb, ctx, err := c.getRedisClient(connName, db) if err != nil { @@ -536,7 +537,9 @@ func (c *connectionService) SetKeyValue(connName string, db int, key, keyType st var expiration time.Duration if ttl < 0 { - expiration = redis.KeepTTL + if expiration, err = rdb.PTTL(ctx, key).Result(); err != nil { + expiration = redis.KeepTTL + } } else { expiration = time.Duration(ttl) * time.Second } @@ -546,7 +549,11 @@ func (c *connectionService) SetKeyValue(connName string, db int, key, keyType st resp.Msg = "invalid string value" return } else { - _, err = rdb.Set(ctx, key, str, expiration).Result() + _, err = rdb.Set(ctx, key, str, 0).Result() + // set expiration lonely, not "keepttl" + if err == nil && expiration > 0 { + rdb.Expire(ctx, key, expiration) + } } case "list": if strs, ok := value.([]any); !ok { diff --git a/frontend/src/stores/connections.js b/frontend/src/stores/connections.js index 9db3d5d..ba4acb1 100644 --- a/frontend/src/stores/connections.js +++ b/frontend/src/stores/connections.js @@ -533,8 +533,8 @@ const useConnectionStore = defineStore('connections', { }) return } else { - // key not exists, remove this key - await this.deleteKey(server, db, key) + // its danger to delete "non-exists" key, just remove from tree view + await this.deleteKey(server, db, key, true) } } @@ -1327,21 +1327,23 @@ const useConnectionStore = defineStore('connections', { * @param {string} connName * @param {number} db * @param {string} key + * @param {boolean} [soft] do not try to remove from redis if true, just remove from tree data * @returns {Promise} */ - async deleteKey(connName, db, key) { + async deleteKey(connName, db, key, soft) { try { - const { data, success, msg } = await DeleteKey(connName, db, key) - if (success) { - // update tree view data - this._deleteKeyNode(connName, db, key) - this._tidyNode(connName, db, key, true) - - // set tab content empty - const tab = useTabStore() - tab.emptyTab(connName) - return true + if (soft !== true) { + await DeleteKey(connName, db, key) } + + // update tree view data + this._deleteKeyNode(connName, db, key) + this._tidyNode(connName, db, key, true) + + // set tab content empty + const tab = useTabStore() + tab.emptyTab(connName) + return true } finally { } return false