fix: DbSelector 数据库 key 数量使用 INFO keyspace 直接获取

- 改用 INFO keyspace 命令替代解析完整 INFO 输出,避免正则匹配 Keyspace 段失败
- activeId 变化时重新获取 DB 数量和 key 计数
- expose refreshDbCounts 方法供父组件调用
This commit is contained in:
2026-07-11 23:03:53 +08:00
parent b7e9c80022
commit b4aab76141
+13 -6
View File
@@ -90,18 +90,18 @@ async function fetchDbInfo() {
} }
try { try {
const info = await connStore.getInfo() // Use INFO keyspace directly for reliable parsing
const info: any = await window.electronAPI.redis.execute(
connStore.activeId, 'INFO', ['keyspace']
)
const keyspace: Record<number, number> = {} const keyspace: Record<number, number> = {}
const match = info.match(/# Keyspace[\s\S]*?(?=\n#|$)/) const lines = String(info).split('\n')
if (match) {
const lines = match[0].split('\n')
for (const line of lines) { for (const line of lines) {
const dbMatch = line.match(/^db(\d+):keys=(\d+)/) const dbMatch = line.match(/^db(\d+):keys=(\d+)/)
if (dbMatch) { if (dbMatch) {
keyspace[parseInt(dbMatch[1], 10)] = parseInt(dbMatch[2], 10) keyspace[parseInt(dbMatch[1], 10)] = parseInt(dbMatch[2], 10)
} }
} }
}
for (let i = 0; i < dbCount.value; i++) { for (let i = 0; i < dbCount.value; i++) {
if (!(i in keyspace)) { if (!(i in keyspace)) {
keyspace[i] = 0 keyspace[i] = 0
@@ -127,8 +127,13 @@ watch(
watch( watch(
() => connStore.activeId, () => connStore.activeId,
() => { async () => {
loadCustomNames() loadCustomNames()
if (connStore.isConnected) {
selectedDb.value = 0
keyStore.currentDb = 0
await fetchDbInfo()
}
} }
) )
@@ -143,6 +148,8 @@ function formatCount(n: number): string {
if (n >= 10000) return `${(n / 1000).toFixed(0)}k` if (n >= 10000) return `${(n / 1000).toFixed(0)}k`
return String(n) return String(n)
} }
defineExpose({ refreshDbCounts: fetchDbInfo })
</script> </script>
<template> <template>