Download the PHP package yangweijie/think-orm-async without Composer
On this page you can find all versions of the php package yangweijie/think-orm-async. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Please rate this library. Is it a good library?
Informations about the package think-orm-async
ThinkOrmAsync - think-orm 异步批量查询扩展
功能特性
- ✅ 透明的异步查询 API(与同步代码完全一致)
- ✅ 支持 ORM 查询和原生 SQL 查询
- ✅ 自动从 ThinkPHP 配置读取数据库配置
- ✅ 自动连接重试(仅限连接错误)
- ✅ 延迟加载结果占位符
- ✅ 零侵入集成(trait + 单例)
- ✅ 非 Swoole 环境可用(纯 PHP mysqli)
安装
快速开始
1. 在模型中引入 trait
2. 使用异步查询
基本使用
ORM 查询
原生 SQL 查询
混合使用 ORM 和原生查询
配置
自动配置(推荐)
在 ThinkPHP 项目中,AsyncContext::start() 会自动从 config('database.connections.mysql') 读取配置:
手动配置
在非 ThinkPHP 项目中,需要手动传递配置:
设置超时
重试机制
仅重试连接错误,不重试 SQL 逻辑错误。
可重试错误
- 2002: Connection refused
- 2003: Can't connect to MySQL server
- 2006: MySQL server has gone away
- 2013: Lost connection to MySQL server during query
不重试错误
- SQL 语法错误 (1064)
- 表不存在 (1146)
- 字段不存在 (1054)
- 权限错误
- 其他 SQL 逻辑错误
重试策略
- 最多重连一次(总共最多执行 2 次)
- 固定延迟 1 秒后重连
- 重连失败后抛出异常
错误处理
处理查询失败
处理部分失败
高级用法
数组访问和遍历
转换为数组和 JSON
获取模型对象
辅助方法
注意事项
- ThinkPHP 环境:在 ThinkPHP 项目中,会自动从
config('database.connections.mysql')读取配置 - mysqlnd 驱动:需要使用 mysqlnd 驱动(PHP 5.3+ 默认)
- 不要在循环中使用:避免在循环中嵌套
AsyncContext::start()/AsyncContext::end() - 错误处理:查询失败时会抛出异常,需要自行处理
- 表前缀:确保模型类正确设置了
$name和$prefix属性
性能提升
在多个独立查询的场景下,异步查询可以显著减少网络往返延迟:
| 查询数 | 同步查询 | 异步查询 | 提升 |
|---|---|---|---|
| 3 | 300ms | 150ms | 50% |
| 5 | 500ms | 200ms | 60% |
| 10 | 1000ms | 350ms | 65% |
假设:每个查询平均耗时 100ms,网络往返 50ms
测试
测试使用 Pest 框架,使用 Mock 对象,无需真实数据库连接。
许可证
MIT
All versions of think-orm-async with dependencies
PHP Build Version
Package Version
No informations.
The package yangweijie/think-orm-async contains the following files
Loading the files please wait ...