feat(redis): support renaming keys
This commit is contained in:
21 files changed
+297
-2
No files matched your search
@@ -2862,6 +2862,20 @@ where
|
||||
redis::cmd("DEL").arg(key).query_async::<()>(con).await.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
/// Rename a key without overwriting an existing destination.
|
||||
pub async fn rename_key<C>(con: &mut C, key: &[u8], new_key: &[u8]) -> Result<(), String>
|
||||
where
|
||||
C: ConnectionLike + Send + Sync + Unpin,
|
||||
{
|
||||
let renamed =
|
||||
redis::cmd("RENAMENX").arg(key).arg(new_key).query_async::<bool>(con).await.map_err(|e| e.to_string())?;
|
||||
if renamed {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("Target key already exists".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
async fn apply_expire_if_needed<C>(con: &mut C, key: &[u8], ttl: Option<i64>) -> Result<(), String>
|
||||
where
|
||||
C: ConnectionLike + Send + Sync + Unpin,
|
||||
@@ -4177,6 +4191,27 @@ mod tests {
|
||||
assert!(!con.commands[0].contains("\r\nEXPIRE\r\n"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rename_key_uses_renamenx_without_overwriting_the_destination() {
|
||||
let mut con = FakeRedisConnection::new(vec![RedisRawValue::Int(1)]);
|
||||
|
||||
super::rename_key(&mut con, b"session:old", b"session:new").await.unwrap();
|
||||
|
||||
assert_eq!(con.commands.len(), 1);
|
||||
assert!(con.commands[0].contains("\r\nRENAMENX\r\n"));
|
||||
assert!(con.commands[0].contains("\r\nsession:old\r\n"));
|
||||
assert!(con.commands[0].contains("\r\nsession:new\r\n"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rename_key_reports_a_destination_conflict() {
|
||||
let mut con = FakeRedisConnection::new(vec![RedisRawValue::Int(0)]);
|
||||
|
||||
let error = super::rename_key(&mut con, b"session:old", b"session:new").await.unwrap_err();
|
||||
|
||||
assert_eq!(error, "Target key already exists");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn set_expire_at_uses_expireat_with_the_unix_timestamp() {
|
||||
let mut con = FakeRedisConnection::new(vec![RedisRawValue::Int(1)]);
|
||||
|
||||
@@ -366,6 +366,38 @@ pub async fn redis_delete_key_in_db_core(
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn redis_rename_key_in_db_core(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
db: u32,
|
||||
key_raw: &str,
|
||||
new_key_raw: &str,
|
||||
) -> Result<(), String> {
|
||||
ensure_redis_pool(state, connection_id).await?;
|
||||
let connections = state.connections.read().await;
|
||||
let pool = connections.get(connection_id).ok_or("Connection not found")?;
|
||||
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
|
||||
let new_key = redis_driver::redis_key_raw_to_bytes(new_key_raw)?;
|
||||
if key == new_key {
|
||||
return Ok(());
|
||||
}
|
||||
match pool {
|
||||
PoolKind::Redis(redis) => match redis {
|
||||
RedisConnection::Direct(con) => {
|
||||
let mut con = con.lock().await;
|
||||
redis_driver::select_db(&mut *con, db).await?;
|
||||
redis_driver::rename_key(&mut *con, &key, &new_key).await
|
||||
}
|
||||
RedisConnection::Cluster(cluster) => {
|
||||
redis_driver::ensure_cluster_db(db)?;
|
||||
let mut con = redis_driver::cluster_key_connection(cluster, &key).await?;
|
||||
redis_driver::rename_key(&mut con, &key, &new_key).await
|
||||
}
|
||||
},
|
||||
_ => Err("Not a Redis connection".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn redis_hash_set_core(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
|
||||
Reference in new issue
Block a user