#![allow(clippy::uninlined_format_args)]
use redis_oxide::{Client, ConnectionConfig, PoolConfig, PoolStrategy};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Testing advanced Redis-oxide features...");
println!("Testing with Connection Pool strategy...");
let mut config = ConnectionConfig::new("redis://localhost:6379");
config.pool = PoolConfig {
strategy: PoolStrategy::Pool,
max_size: 10,
min_idle: 2,
connection_timeout: Duration::from_secs(5),
};
let pool_client = Client::connect(config).await?;
println!("Connected with pool strategy!");
println!("Testing transaction operations...");
let mut transaction = pool_client.transaction().await?;
transaction.set("tx:key1", "value1");
transaction.set("tx:key2", "value2");
transaction.set("tx:key3", "value3");
let results = transaction.exec().await?;
println!("Transaction executed with {} results", results.len());
if let Some(value) = pool_client.get("tx:key1").await? {
println!("tx:key1 = {}", value);
}
if let Some(value) = pool_client.get("tx:key2").await? {
println!("tx:key2 = {}", value);
}
if let Some(value) = pool_client.get("tx:key3").await? {
println!("tx:key3 = {}", value);
}
println!("Testing pipeline operations...");
let mut pipeline = pool_client.pipeline();
pipeline.set("pipeline:key1", "pipeline_value1");
pipeline.get("pipeline:key1");
pipeline.incr("pipeline:counter");
pipeline.expire("pipeline:key1", Duration::from_secs(30));
let results = pipeline.execute().await?;
println!("Pipeline executed with {} results", results.len());
println!("Testing hash operations...");
pool_client.hset("hash:test", "field1", "value1").await?;
pool_client.hset("hash:test", "field2", "value2").await?;
if let Some(value) = pool_client.hget("hash:test", "field1").await? {
println!("Hash field1 = {}", value);
}
let all_fields = pool_client.hgetall("hash:test").await?;
println!("All hash fields: {:?}", all_fields);
println!("Testing list operations...");
pool_client
.lpush("list:test", vec!["item1".to_string()])
.await?;
pool_client
.lpush("list:test", vec!["item2".to_string()])
.await?;
pool_client
.lpush("list:test", vec!["item3".to_string()])
.await?;
let items = pool_client.lrange("list:test", 0, -1).await?;
println!("List items: {:?}", items);
println!("Testing with Multiplexed connection strategy...");
let config = ConnectionConfig::new("redis://localhost:6379");
let multiplexed_client = Client::connect(config).await?;
let mut tasks = Vec::new();
for i in 0..10 {
let client_clone = multiplexed_client.clone();
let task = tokio::spawn(async move {
client_clone
.set(format!("mux_test:{}", i), format!("mux_value:{}", i))
.await
.map_err(|e| format!("Error setting key {}: {}", i, e))
});
tasks.push(task);
}
for (i, task) in tasks.into_iter().enumerate() {
match task.await {
Ok(Ok(_)) => println!("Task {} completed successfully", i),
Ok(Err(e)) => eprintln!("Task {} failed: {}", i, e),
Err(e) => eprintln!("Task {} panicked: {}", i, e),
}
}
for i in 0..10 {
if let Some(value) = multiplexed_client.get(&format!("mux_test:{}", i)).await? {
println!("mux_test:{} = {}", i, value);
}
}
println!("Cleaning up test keys...");
pool_client
.del(vec![
"tx:key1".to_string(),
"tx:key2".to_string(),
"tx:key3".to_string(),
"pipeline:key1".to_string(),
"pipeline:counter".to_string(),
"hash:test".to_string(),
"list:test".to_string(),
])
.await?;
let mux_keys: Vec<String> = (0..10).map(|i| format!("mux_test:{}", i)).collect();
multiplexed_client.del(mux_keys).await?;
println!("Advanced tests completed successfully!");
Ok(())
}