feat(redis): download large Redis values

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-09-30 18:57:37 +08:00
1 parent 0292d4c984
commit 43b6e218b0
19 files changed
+166 -4

No files matched your search

+29 -1
View File
@@ -1,6 +1,6 @@
use crate::connection::{AppState, PoolKind};
use crate::db::redis_driver::{
self, RedisCollectionPage, RedisCommandResult, RedisConnection, RedisDatabaseInfo, RedisKeysExpiry,
self, RedisBlob, RedisCollectionPage, RedisCommandResult, RedisConnection, RedisDatabaseInfo, RedisKeysExpiry,
RedisKeysExpiryResult, RedisScanResult, RedisStreamConsumer, RedisStreamGroup, RedisStreamPage,
RedisStreamPendingPage, RedisValue,
};
@@ -134,6 +134,34 @@ pub async fn redis_get_value_in_db_core(
}
}
pub async fn redis_get_raw_value_in_db_core(
state: &AppState,
connection_id: &str,
db: u32,
key_raw: &str,
) -> Result<RedisBlob, String> {
ensure_redis_pool(state, connection_id).await?;
let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?;
redis_driver::get_raw_value(&mut *con, &key).await
}
RedisConnection::Cluster(cluster) => {
redis_driver::ensure_cluster_db(db)?;
let mut con = redis_driver::cluster_key_connection(cluster, &key).await?;
redis_driver::get_raw_value(&mut con, &key).await
}
}
}
_ => Err("Not a Redis connection".to_string()),
}
}
/// Fetch only the TTL for a selected key. This avoids repeatedly loading a
/// potentially large Redis value when the desktop detail view is polling.
pub async fn redis_get_ttl_in_db_core(
+18
View File
@@ -2796,6 +2796,24 @@ where
})
}
/// Read the complete raw payload for a Redis string-like key.
///
/// The normal value path deliberately caps previews at 64 KiB. Downloads must
/// bypass that cap while preserving the exact bytes, including non-UTF-8 data.
pub async fn get_raw_value<C>(con: &mut C, key: &[u8]) -> Result<RedisBlob, String>
where
C: ConnectionLike + Send + Sync + Unpin,
{
let redis_type: String = redis::cmd("TYPE").arg(key).query_async(con).await.map_err(|e| e.to_string())?;
let raw: RedisRawValue = match redis_type.as_str() {
"string" | "bitmap" => redis::cmd("GET").arg(key).query_async(con).await.map_err(|e| e.to_string())?,
"none" => return Err("Redis key does not exist".to_string()),
_ => return Err("Only Redis string values can be downloaded".to_string()),
};
let bytes = redis_value_to_bytes(raw).ok_or_else(|| "Redis value is not byte-addressable".to_string())?;
Ok(redis_blob_from_bytes(&bytes))
}
/// Read only a key's TTL without loading its value or collection members.
///
/// Redis returns `-2` for a missing key and `-1` for a key without an expiry;
+1
View File
@@ -830,6 +830,7 @@ async fn serve() {
.route("/redis/scan-keys-batch", post(routes::redis::scan_keys_batch))
.route("/redis/scan-values", post(routes::redis::scan_values))
.route("/redis/get-value", post(routes::redis::get_value))
.route("/redis/get-raw-value", post(routes::redis::get_raw_value))
.route("/redis/get-ttl", post(routes::redis::get_ttl))
.route("/redis/get-stream-entries", post(routes::redis::get_stream_entries))
.route("/redis/get-stream-groups", post(routes::redis::get_stream_groups))
+11
View File
@@ -411,6 +411,17 @@ pub async fn get_value(
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn get_raw_value(
State(state): State<Arc<WebState>>,
Json(req): Json<RedisKeyRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
let result =
dbx_core::redis_ops::redis_get_raw_value_in_db_core(&state.app, &req.connection_id, req.db, &req.key_raw)
.await
.map_err(AppError::from)?;
Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}
pub async fn get_ttl(
State(state): State<Arc<WebState>>,
Json(req): Json<RedisKeyRequest>,