feat: add sync/async delete key option to delete key dialog

This commit is contained in:
tiny-craft 2023-10-16 01:09:04 +08:00
parent 4a0807e463
commit d61eb1323f
5 changed files with 37 additions and 16 deletions

View File

@ -1330,7 +1330,7 @@ func (c *connectionService) SetKeyTTL(connName string, db int, k any, ttl int64)
}
// DeleteKey remove redis key
func (c *connectionService) DeleteKey(connName string, db int, k any) (resp types.JSResp) {
func (c *connectionService) DeleteKey(connName string, db int, k any, async bool) (resp types.JSResp) {
client, ctx, err := c.getRedisClient(connName, db)
if err != nil {
resp.Msg = err.Error()
@ -1344,10 +1344,15 @@ func (c *connectionService) DeleteKey(connName string, db int, k any) (resp type
var mutex sync.Mutex
del := func(ctx context.Context, cli redis.UniversalClient) error {
iter := cli.Scan(ctx, 0, key, 10000).Iterator()
var fn func(c context.Context, ks ...string) *redis.IntCmd
if async {
fn = cli.Unlink
} else {
fn = cli.Del
}
for iter.Next(ctx) {
subKey := iter.Val()
if err = cli.Unlink(ctx, subKey).Err(); err != nil {
log.Println("unlink error", err.Error())
if err = fn(ctx, subKey).Err(); err != nil {
return err
} else {
mutex.Lock()
@ -1373,11 +1378,17 @@ func (c *connectionService) DeleteKey(connName string, db int, k any) (resp type
}
} else {
// delete key only
_, err = client.Del(ctx, key).Result()
if err != nil {
if async {
if _, err = client.Unlink(ctx, key).Result(); err != nil {
resp.Msg = err.Error()
return
}
} else {
if _, err = client.Del(ctx, key).Result(); err != nil {
resp.Msg = err.Error()
return
}
}
deletedKeys = append(deletedKeys, key)
}

View File

@ -12,6 +12,7 @@ const deleteForm = reactive({
showAffected: false,
loadingAffected: false,
affectedKeys: [],
async: true,
})
const dialogStore = useDialog()
@ -27,6 +28,7 @@ watch(
deleteForm.showAffected = false
deleteForm.loadingAffected = false
deleteForm.affectedKeys = []
deleteForm.async = true
}
},
)
@ -50,8 +52,8 @@ const resetAffected = () => {
const i18n = useI18n()
const onConfirmDelete = async () => {
try {
const { server, db, key } = deleteForm
const success = await connectionStore.deleteKeyPrefix(server, db, key)
const { server, db, key, async } = deleteForm
const success = await connectionStore.deleteKeyPrefix(server, db, key, async)
if (success) {
$message.success(i18n.t('dialogue.handle_succ'))
}
@ -86,6 +88,9 @@ const onClose = () => {
<n-form-item :label="$t('dialogue.key.key_expression')" required>
<n-input v-model:value="deleteForm.key" placeholder="" @input="resetAffected" />
</n-form-item>
<n-form-item :label="$t('dialogue.key.async_delete')" required>
<n-checkbox v-model:checked="deleteForm.async">{{ $t('dialogue.key.async_delete_title') }}</n-checkbox>
</n-form-item>
<n-card v-if="deleteForm.showAffected" :title="$t('dialogue.key.affected_key')" size="small">
<n-skeleton v-if="deleteForm.loadingAffected" :repeat="10" text />
<n-log

View File

@ -199,7 +199,9 @@
"key_expression": "Key Expression",
"affected_key": "Affected Keys",
"show_affected_key": "Show Affected Keys",
"confirm_delete_key": "Confirm Delete {num} Keys"
"confirm_delete_key": "Confirm Delete {num} Key(s)",
"async_delete": "Asynchronously Execute",
"async_delete_title": "Do not waiting for the operation's result"
},
"field": {
"new": "Add New Field",

View File

@ -198,7 +198,9 @@
"key_expression": "键名表达式",
"affected_key": "受影响的键名",
"show_affected_key": "查看受影响的键名",
"confirm_delete_key": "确认删除{num}个键"
"confirm_delete_key": "确认删除{num}个键",
"async_delete": "异步执行",
"async_delete_title": "不等待操作结果"
},
"field": {
"new": "添加新字段",

View File

@ -1479,12 +1479,13 @@ const useConnectionStore = defineStore('connections', {
/**
* delete keys with prefix
* @param connName
* @param db
* @param prefix
* @param {string} connName
* @param {number} db
* @param {string} prefix
* @param {boolean} async
* @returns {Promise<boolean>}
*/
async deleteKeyPrefix(connName, db, prefix) {
async deleteKeyPrefix(connName, db, prefix, async) {
if (isEmpty(prefix)) {
return false
}
@ -1492,7 +1493,7 @@ const useConnectionStore = defineStore('connections', {
if (!endsWith(prefix, '*')) {
prefix += '*'
}
const { data, success, msg } = await DeleteKey(connName, db, prefix)
const { data, success, msg } = await DeleteKey(connName, db, prefix, async)
if (success) {
// const { deleted: keys = [] } = data
// for (const key of keys) {