fix: cannot set key on some low version redis with "keepttl"

perf: add soft delete key
This commit is contained in:
tiny-craft 2023-09-26 16:35:28 +08:00
parent 47e9d908e7
commit 50ca632a5b
2 changed files with 24 additions and 15 deletions

View File

@ -527,6 +527,7 @@ func (c *connectionService) GetKeyValue(connName string, db int, key string) (re
} }
// SetKeyValue set value by key // 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) { 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) rdb, ctx, err := c.getRedisClient(connName, db)
if err != nil { if err != nil {
@ -536,7 +537,9 @@ func (c *connectionService) SetKeyValue(connName string, db int, key, keyType st
var expiration time.Duration var expiration time.Duration
if ttl < 0 { if ttl < 0 {
expiration = redis.KeepTTL if expiration, err = rdb.PTTL(ctx, key).Result(); err != nil {
expiration = redis.KeepTTL
}
} else { } else {
expiration = time.Duration(ttl) * time.Second 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" resp.Msg = "invalid string value"
return return
} else { } 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": case "list":
if strs, ok := value.([]any); !ok { if strs, ok := value.([]any); !ok {

View File

@ -533,8 +533,8 @@ const useConnectionStore = defineStore('connections', {
}) })
return return
} else { } else {
// key not exists, remove this key // its danger to delete "non-exists" key, just remove from tree view
await this.deleteKey(server, db, key) await this.deleteKey(server, db, key, true)
} }
} }
@ -1327,21 +1327,23 @@ const useConnectionStore = defineStore('connections', {
* @param {string} connName * @param {string} connName
* @param {number} db * @param {number} db
* @param {string} key * @param {string} key
* @param {boolean} [soft] do not try to remove from redis if true, just remove from tree data
* @returns {Promise<boolean>} * @returns {Promise<boolean>}
*/ */
async deleteKey(connName, db, key) { async deleteKey(connName, db, key, soft) {
try { try {
const { data, success, msg } = await DeleteKey(connName, db, key) if (soft !== true) {
if (success) { 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
} }
// 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 { } finally {
} }
return false return false