Files
dbx/src-tauri/src/commands/redis_cmd.rs
T
Abeautifulsnow 919c3d58c1 feat(redis): add key statistics tracking for database browser (#269)
## 背景说明
为 Redis 数据库浏览器增加键统计功能,显示已加载 key 数/总 key 数,
并在动态加载、删除、清空时实时更新。

## 修改内容
- 新增 \RedisDatabaseInfo\ 结构体(db + keys),替代原来的 \Vec<u32>\
- \list_databases\ 返回每个 db 的 key 数量(解析 INFO keyspace)
- \scan_keys_page\ 增加 DBSIZE 调用获取 total_keys
- 前端 TreeNode 增加 loadedKeyCount/totalKeyCount 字段
- 树节点标签动态显示 \db0 (loaded/total)\ 格式
- 新增 \updateRedisDbKeyStats\ 在加载/删除/清空时更新统计

## 测试
- 建议补充 list_keyspace_databases 解析异常输入的单测
- 建议补充 redisDbLabel 边界值测试
2026-05-14 16:24:46 +08:00

210 lines
5.7 KiB
Rust

use std::sync::Arc;
use tauri::State;
use crate::commands::connection::AppState;
use dbx_core::db::redis_driver::{RedisCommandResult, RedisDatabaseInfo, RedisScanResult, RedisValue};
#[tauri::command]
pub async fn redis_list_databases(
state: State<'_, Arc<AppState>>,
connection_id: String,
) -> Result<Vec<RedisDatabaseInfo>, String> {
dbx_core::redis_ops::redis_list_databases_core(&state, &connection_id).await
}
#[tauri::command]
pub async fn redis_scan_keys(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
cursor: u64,
pattern: String,
count: usize,
) -> Result<RedisScanResult, String> {
dbx_core::redis_ops::redis_scan_keys_core(&state, &connection_id, db, cursor, &pattern, count).await
}
#[tauri::command]
pub async fn redis_get_value(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raw: String,
) -> Result<RedisValue, String> {
dbx_core::redis_ops::redis_get_value_in_db_core(&state, &connection_id, db, &key_raw).await
}
#[tauri::command]
pub async fn redis_set_string(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raw: String,
value: String,
ttl: Option<i64>,
) -> Result<(), String> {
dbx_core::redis_ops::redis_set_string_in_db_core(&state, &connection_id, db, &key_raw, &value, ttl).await
}
#[tauri::command]
pub async fn redis_delete_key(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raw: String,
) -> Result<(), String> {
dbx_core::redis_ops::redis_delete_key_in_db_core(&state, &connection_id, db, &key_raw).await
}
#[tauri::command]
pub async fn redis_hash_set(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raw: String,
field: String,
value: String,
) -> Result<(), String> {
dbx_core::redis_ops::redis_hash_set_in_db_core(&state, &connection_id, db, &key_raw, &field, &value).await
}
#[tauri::command]
pub async fn redis_hash_del(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raw: String,
field: String,
) -> Result<(), String> {
dbx_core::redis_ops::redis_hash_del_in_db_core(&state, &connection_id, db, &key_raw, &field).await
}
#[tauri::command]
pub async fn redis_list_push(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raw: String,
value: String,
) -> Result<(), String> {
dbx_core::redis_ops::redis_list_push_in_db_core(&state, &connection_id, db, &key_raw, &value).await
}
#[tauri::command]
pub async fn redis_list_set(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raw: String,
index: i64,
value: String,
) -> Result<(), String> {
dbx_core::redis_ops::redis_list_set_in_db_core(&state, &connection_id, db, &key_raw, index, &value).await
}
#[tauri::command]
pub async fn redis_list_remove(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raw: String,
index: i64,
) -> Result<(), String> {
dbx_core::redis_ops::redis_list_remove_in_db_core(&state, &connection_id, db, &key_raw, index).await
}
#[tauri::command]
pub async fn redis_set_add(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raw: String,
member: String,
) -> Result<(), String> {
dbx_core::redis_ops::redis_set_add_in_db_core(&state, &connection_id, db, &key_raw, &member).await
}
#[tauri::command]
pub async fn redis_set_remove(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raw: String,
member: String,
) -> Result<(), String> {
dbx_core::redis_ops::redis_set_remove_in_db_core(&state, &connection_id, db, &key_raw, &member).await
}
#[tauri::command]
pub async fn redis_zadd(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raw: String,
member: String,
score: f64,
) -> Result<(), String> {
dbx_core::redis_ops::redis_zadd_in_db_core(&state, &connection_id, db, &key_raw, &member, score).await
}
#[tauri::command]
pub async fn redis_zrem(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raw: String,
member: String,
) -> Result<(), String> {
dbx_core::redis_ops::redis_zrem_in_db_core(&state, &connection_id, db, &key_raw, &member).await
}
#[tauri::command]
pub async fn redis_set_ttl(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raw: String,
ttl: i64,
) -> Result<(), String> {
dbx_core::redis_ops::redis_set_ttl_in_db_core(&state, &connection_id, db, &key_raw, ttl).await
}
#[tauri::command]
pub async fn redis_delete_keys(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raws: Vec<String>,
) -> Result<u64, String> {
dbx_core::redis_ops::redis_delete_keys_in_db_core(&state, &connection_id, db, &key_raws).await
}
#[tauri::command]
pub async fn redis_flush_db(state: State<'_, Arc<AppState>>, connection_id: String, db: u32) -> Result<(), String> {
dbx_core::redis_ops::redis_flush_db_core(&state, &connection_id, db).await
}
#[tauri::command]
pub async fn redis_execute_command(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
command: String,
) -> Result<RedisCommandResult, String> {
dbx_core::redis_ops::redis_execute_command_core(&state, &connection_id, db, &command).await
}
#[tauri::command]
pub async fn redis_load_more(
state: State<'_, Arc<AppState>>,
connection_id: String,
db: u32,
key_raw: String,
key_type: String,
cursor: u64,
count: usize,
) -> Result<RedisValue, String> {
dbx_core::redis_ops::redis_load_more_in_db_core(&state, &connection_id, db, &key_raw, &key_type, cursor, count)
.await
}