Claw 项目完整结构提交
This commit is contained in:
51
Claw/Server/shared/examples/embedded_redis_usage.rs
Normal file
51
Claw/Server/shared/examples/embedded_redis_usage.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
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(())
|
||||
}
|
||||
Reference in New Issue
Block a user