refactor: reimplement logic of remove multiple keys
This commit is contained in:
tiny-craft 2023-07-06 14:50:36 +08:00
parent 1841ccf3d3
commit 9cd0d34c5d
4 changed files with 49 additions and 19 deletions

View File

@ -870,22 +870,49 @@ func (c *connectionService) SetKeyTTL(connName string, db int, key string, ttl i
} }
// DeleteKey remove redis key // DeleteKey remove redis key
func (c *connectionService) DeleteKey(connName string, db int, keys []string) (resp types.JSResp) { func (c *connectionService) DeleteKey(connName string, db int, key string) (resp types.JSResp) {
rdb, ctx, err := c.getRedisClient(connName, db) rdb, ctx, err := c.getRedisClient(connName, db)
if err != nil { if err != nil {
resp.Msg = err.Error() resp.Msg = err.Error()
return return
} }
var deletedKeys []string
rmCount, err := rdb.Del(ctx, keys...).Result() if strings.HasSuffix(key, "*") {
// delete by prefix
var cursor uint64
for {
var loadedKey []string
if loadedKey, cursor, err = rdb.Scan(ctx, cursor, key, 10000).Result(); err != nil {
resp.Msg = err.Error()
return
} else {
if err = rdb.Del(ctx, loadedKey...).Err(); err != nil {
resp.Msg = err.Error()
return
} else {
deletedKeys = append(deletedKeys, loadedKey...)
}
}
// no more loadedKey
if cursor == 0 {
break
}
}
} else {
// delete key only
_, err = rdb.Del(ctx, key).Result()
if err != nil { if err != nil {
resp.Msg = err.Error() resp.Msg = err.Error()
return return
} }
deletedKeys = append(deletedKeys, key)
}
resp.Success = true resp.Success = true
resp.Data = map[string]any{ resp.Data = map[string]any{
"effect_count": rmCount, "deleted": deletedKeys,
} }
return return
} }

View File

@ -53,7 +53,7 @@ const message = useMessage()
const onConfirmDelete = async () => { const onConfirmDelete = async () => {
try { try {
const { server, db, key } = deleteForm const { server, db, key } = deleteForm
const success = await connectionStore.deleteKeys(server, db, key, deleteForm.affectedKeys) const success = await connectionStore.deleteKeyPrefix(server, db, key)
if (success) { if (success) {
message.success(i18n.t('handle_succ')) message.success(i18n.t('handle_succ'))
} }

View File

@ -284,7 +284,7 @@ const findSiblingsAndIndex = (node, nodes) => {
} }
// delay save until stop drop after 2 seconds // delay save until stop drop after 2 seconds
const saveSort = debounce(connectionStore.saveConnectionSort, 2000, { trailing: true }) const saveSort = debounce(connectionStore.saveConnectionSorted, 2000, { trailing: true })
const handleDrop = ({ node, dragNode, dropPosition }) => { const handleDrop = ({ node, dragNode, dropPosition }) => {
const [dragNodeSiblings, dragNodeIndex] = findSiblingsAndIndex(dragNode, connectionStore.connections) const [dragNodeSiblings, dragNodeIndex] = findSiblingsAndIndex(dragNode, connectionStore.connections)
if (dragNodeSiblings === null || dragNodeIndex === null) { if (dragNodeSiblings === null || dragNodeIndex === null) {

View File

@ -57,12 +57,12 @@ const useConnectionStore = defineStore('connections', {
/** /**
* *
* @returns {{databases: Object<string, DatabaseItem[]>, connections: ConnectionItem[]}} * @returns {{groups: string[], databases: Object<string, DatabaseItem[]>, connections: ConnectionItem[]}}
*/ */
state: () => ({ state: () => ({
groups: [], // all group name groups: [], // all group name set
connections: [], // all connections connections: [], // all connections
databases: {}, // all databases in opened connections group by name databases: {}, // all databases in opened connections group by server name
}), }),
getters: { getters: {
anyConnectionOpened() { anyConnectionOpened() {
@ -196,10 +196,10 @@ const useConnectionStore = defineStore('connections', {
}, },
/** /**
* save connection * save connection after sort
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async saveConnectionSort() { async saveConnectionSorted() {
const mapToList = (conns) => { const mapToList = (conns) => {
const list = [] const list = []
for (const conn of conns) { for (const conn of conns) {
@ -1137,7 +1137,7 @@ const useConnectionStore = defineStore('connections', {
*/ */
async deleteKey(connName, db, key) { async deleteKey(connName, db, key) {
try { try {
const { data, success, msg } = await DeleteKey(connName, db, [key]) const { data, success, msg } = await DeleteKey(connName, db, key)
if (success) { if (success) {
// update tree view data // update tree view data
this._deleteKeyNode(connName, db, key) this._deleteKeyNode(connName, db, key)
@ -1157,18 +1157,21 @@ const useConnectionStore = defineStore('connections', {
* @param connName * @param connName
* @param db * @param db
* @param prefix * @param prefix
* @param keys
* @returns {Promise<boolean>} * @returns {Promise<boolean>}
*/ */
async deleteKeys(connName, db, prefix, keys) { async deleteKeyPrefix(connName, db, prefix) {
if (isEmpty(keys)) { if (isEmpty(prefix)) {
return false return false
} }
try { try {
const { success, msg } = await DeleteKey(connName, db, keys) if (!endsWith(prefix, '*')) {
prefix += '*'
}
const { data, success, msg } = await DeleteKey(connName, db, prefix)
if (success) { if (success) {
const { deleted: keys = [] } = data
for (const key of keys) { for (const key of keys) {
await this.deleteKey(connName, db, key) await this._deleteKeyNode(connName, db, key)
await nextTick() await nextTick()
} }
return true return true