2 releases
| 0.1.1 | Jul 8, 2025 |
|---|---|
| 0.1.0 | Jul 7, 2025 |
#1442 in Database interfaces
25 downloads per month
91KB
2K
SLoC
RFrame
RFrame 是一个轻量级的 Rust 框架,提供了数据库操作、缓存管理等常用功能的抽象封装。
功能特性
- 🚀 异步支持 - 基于 tokio 的异步运行时
- 📦 模块化设计 - 按需引入所需功能
- 🔒 类型安全 - 充分利用 Rust 的类型系统
- 🛠 可扩展 - 易于扩展的模块化架构
- 💾 数据库支持 - MySQL 支持(可选)
- 📝 缓存系统 - 支持本地缓存和 Redis(可选)
安装
将以下依赖添加到你的 Cargo.toml 文件中:
[dependencies]
rframe = { version = "0.1.0", features = ["db-mysql", "cache-redis"] }
特性说明
RFrame 提供以下特性组件,可以根据需要选择性启用:
数据库特性
db-mysql: 启用 MySQL 数据库支持- 包含连接池管理
- 异步查询支持
- 事务管理
缓存特性
-
cache-local: 启用本地缓存支持- 基于 moka 实现的高性能本地缓存
- 支持 TTL 和容量限制
- 异步操作支持
-
cache-redis: 启用 Redis 缓存支持- 连接池管理
- 支持所有 Redis 数据类型
- 异步操作
-
cache-multilevel: 启用多级缓存支持- 结合本地缓存和Redis
- 自动缓存同步
- 需同时启用
cache-local和cache-redis
-
cache-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 文件了解详情
作者
- fangbc5
Dependencies
~5–27MB
~370K SLoC