| Crates.io | rframe |
| lib.rs | rframe |
| version | 0.1.1 |
| created_at | 2025-07-07 13:35:04.423396+00 |
| updated_at | 2025-07-08 03:20:15.235286+00 |
| description | A lightweight framework implemented in Rust |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1741284 |
| size | 162,837 |
RFrame 是一个轻量级的 Rust 框架,提供了数据库操作、缓存管理等常用功能的抽象封装。
将以下依赖添加到你的 Cargo.toml 文件中:
[dependencies]
rframe = { version = "0.1.0", features = ["db-mysql", "cache-redis"] }
RFrame 提供以下特性组件,可以根据需要选择性启用:
db-mysql: 启用 MySQL 数据库支持
cache-local: 启用本地缓存支持
cache-redis: 启用 Redis 缓存支持
cache-multilevel: 启用多级缓存支持
cache-local 和 cache-rediscache-full: 启用所有缓存功能
use rframe::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
// 初始化数据库连接
let db = Database::new()
.connection_string("mysql://user:pass@localhost/db")
.connect()
.await?;
// 执行查询
let users = db.query("SELECT * FROM users")
.fetch_all()
.await?;
println!("Found {} users", users.len());
}
use rframe::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
// 初始化本地缓存
let cache = LocalCache::new()
.max_capacity(1000)
.build()?;
// 存储数据
cache.set("key", "value").await?;
// 获取数据
if let Some(value) = cache.get("key").await? {
println!("Value: {}", value);
}
}
MySQL 配置示例:
use rframe::db::mysql::Config;
let config = Config::new()
.host("localhost")
.port(3306)
.username("user")
.password("pass")
.database("db")
.max_connections(10)
.build()?;
本地缓存配置:
use rframe::cache::local::Config;
let config = Config::new()
.max_capacity(1000)
.time_to_live(Duration::from_secs(3600))
.build()?;
Redis 缓存配置:
use rframe::cache::redis::Config;
let config = Config::new()
.url("redis://localhost:6379")
.password("pass")
.max_connections(20)
.build()?;
RFrame 使用 thiserror 和 anyhow 进行错误处理:
use rframe::error::Error;
fn handle_error(result: Result<(), Error>) {
match result {
Ok(_) => println!("操作成功"),
Err(Error::Database(e)) => println!("数据库错误: {}", e),
Err(Error::Cache(e)) => println!("缓存错误: {}", e),
Err(e) => println!("其他错误: {}", e),
}
}
欢迎提交 Issues 和 Pull Requests!在提交 PR 之前,请确保:
本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情