Files
JoyD/Claw/Server/shared/examples/embedded_redis_usage.rs
2026-03-16 15:47:55 +08:00

51 lines
1.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use shared::embedded_redis::{EmbeddedRedisManager, EmbeddedRedisConfig};
use std::sync::Arc;
/// 嵌入式Redis使用示例
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("🚀 启动嵌入式Redis服务器示例...");
// 配置嵌入式Redis
let config = EmbeddedRedisConfig {
bind: "127.0.0.1".to_string(),
port: 6379,
data_dir: "./embedded_redis_data".to_string(),
persistence: true,
max_memory: 64 * 1024 * 1024, // 64MB
cleanup_interval: 300, // 5分钟
};
// 创建管理器
let redis_manager = Arc::new(EmbeddedRedisManager::new(config));
// 启动Redis服务器
redis_manager.start().await?;
println!("✅ 嵌入式Redis服务器已启动");
println!("📍 监听地址: {}", redis_manager.get_connection_url().await);
// 验证服务器状态
let is_running = redis_manager.is_running().await;
println!("🔍 服务器运行状态: {}", if is_running { "运行中" } else { "已停止" });
// 获取配置信息
let config = redis_manager.get_config().await;
println!("📋 配置信息:");
println!(" - 绑定地址: {}", config.bind);
println!(" - 监听端口: {}", config.port);
println!(" - 数据目录: {}", config.data_dir);
println!(" - 持久化: {}", if config.persistence { "启用" } else { "禁用" });
println!(" - 最大内存: {}MB", config.max_memory / (1024 * 1024));
// 保持服务器运行一段时间
println!("⏰ 服务器将在5秒后停止...");
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
// 停止Redis服务器
redis_manager.stop().await;
println!("✅ 嵌入式Redis服务器已停止");
Ok(())
}