Swoole 中使用 PDO 连接池、Redis 连接池、Mysqli 连接池
连接池使用说明
- 所有连接池的实现均基于 ConnectionPool 原始连接池;
- 连接池的底层原理是基于 Channel 的自动调度;
- 开发者需要自己保证归还的连接是可重用的;
- 若连接不可重用,需要调用
$pool->put(null);归还一个空连接; - 归还空连接后,原始连接池会重新创建连接以保证连接池的数量一致。
PDO 连接池
<?php
declare(strict_types=1);
use Swoole\Coroutine;
use Swoole\Database\PDOConfig;
use Swoole\Database\PDOPool;
use Swoole\Runtime;
Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]);
$s = microtime(true);
const N = 1024;
Coroutine\run(function () {
$config = (new PDOConfig)
->withHost('127.0.0.1')
->withPort(3306)
// ->withUnixSocket('/tmp/mysql.sock')
->withDbName('test')
->withCharset('utf8mb4')
->withUsername('root')
->withPassword('root');
// 创建连接池对象,默认创建64个连接
$pool = new PDOPool($config);
for ($n = N; $n--;) {
Coroutine::create(function () use ($pool) {
// 获取连接
$pdo = $pool->get();
$statement = $pdo->prepare('SELECT ? + ?');
if (!$statement) {
throw new RuntimeException('Prepare failed');
}
$a = mt_rand(1, 100);
$b = mt_rand(1, 100);
$result = $statement->execute([$a, $b]);
if (!$result) {
throw new RuntimeException('Execute failed');
}
$result = $statement->fetchAll();
if ($a + $b !== (int)$result[0][0]) {
throw new RuntimeException('Bad result');
}
// 回收连接
$pool->put($pdo);
});
}
});
$s = microtime(true) - $s;
echo 'Use ' . $s . 's for ' . N . ' queries' . PHP_EOL;
Redis 连接池
<?php
declare(strict_types=1);
use Swoole\Coroutine;
use Swoole\Database\RedisConfig;
use Swoole\Database\RedisPool;
use Swoole\Runtime;
Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]);
$s = microtime(true);
const N = 1024;
Coroutine\run(function () {
$config = (new RedisConfig)
->withHost('127.0.0.1')
->withPort(6379)
->withAuth('')
->withDbIndex(0)
->withTimeout(1);
// 创建连接池对象,默认创建64个连接
$pool = new RedisPool($config);
for ($n = N; $n--;) {
Coroutine::create(function () use ($pool) {
// 获取连接
$redis = $pool->get();
$result = $redis->set('foo', 'bar');
if (!$result) {
throw new RuntimeException('Set failed');
}
$result = $redis->get('foo');
if ($result !== 'bar') {
throw new RuntimeException('Get failed');
}
// 回收连接
$pool->put($redis);
});
}
});
$s = microtime(true) - $s;
echo 'Use ' . $s . 's for ' . (N * 2) . ' queries' . PHP_EOL;
Mysqli 连接池
<?php
declare(strict_types=1);
use Swoole\Coroutine;
use Swoole\Database\MysqliConfig;
use Swoole\Database\MysqliPool;
use Swoole\Runtime;
Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]);
$s = microtime(true);
const N = 1024;
Coroutine\run(function () {
$config = (new MysqliConfig)
->withHost('127.0.0.1')
->withPort(3306)
// ->withUnixSocket('/tmp/mysql.sock')
->withDbName('test')
->withCharset('utf8mb4')
->withUsername('root')
->withPassword('root');
// 创建连接池对象,默认创建64个连接
$pool = new MysqliPool($config);
for ($n = N; $n--;) {
Coroutine::create(function () use ($pool) {
// 获取连接
$mysqli = $pool->get();
$statement = $mysqli->prepare('SELECT ? + ?');
if (!$statement) {
throw new RuntimeException('Prepare failed');
}
$a = mt_rand(1, 100);
$b = mt_rand(1, 100);
if (!$statement->bind_param('dd', $a, $b)) {
throw new RuntimeException('Bind param failed');
}
if (!$statement->execute()) {
throw new RuntimeException('Execute failed');
}
if (!$statement->bind_result($result)) {
throw new RuntimeException('Bind result failed');
}
if (!$statement->fetch()) {
throw new RuntimeException('Fetch failed');
}
if ($a + $b !== (int)$result) {
throw new RuntimeException('Bad result');
}
while ($statement->fetch()) {
continue;
}
// 回收连接
$pool->put($mysqli);
});
}
});
$s = microtime(true) - $s;
echo 'Use ' . $s . 's for ' . N . ' queries' . PHP_EOL;
Swoole 中使用 PDO 连接池、Redis 连接池、Mysqli 连接池的更多相关文章
- 压测过程中,获取不到redis连接池,发现redis连接数高
说明:图片截得比较大,浏览器放大倍数看即可(涉及到隐私,打了码,请见谅,如果有疑问,欢迎骚扰). 最近在压测过程中,出现获取不到redis连接池的问题 xshell连接redis服务器,查看连接数,发 ...
- 如何在 Swoole 中优雅的实现 MySQL 连接池
如何在 Swoole 中优雅的实现 MySQL 连接池 一.为什么需要连接池 ? 数据库连接池指的是程序和数据库之间保持一定数量的连接不断开, 并且各个请求的连接可以相互复用, 减少重复连接数据库带来 ...
- Redis 简单使用 and 连接池(python)
Redis 简介 NoSQL(not only sql):非关系型数据库 支持 key-value, list, set, zset, hash 等数据结构的存储:支持主从数据备份,集群:支持 ...
- ServiceStack.Redis连接阿里云redis服务时使用连接池出现的问题
创建连接池 private static PooledRedisClientManager prcm = CreateManager(new string[] { "password@ip: ...
- Java与redis交互、Jedis连接池JedisPool
Java与redis交互比较常用的是Jedis. 先导入jar包: commons-pool2-2.3.jar jedis-2.7.0.jar 基本使用: public class RedisTest ...
- WebSphere中数据源连接池太小导致的连接超时错误记录
WebSphere中数据源连接池太小导致的连接超时错误记录. 应用连接超时错误信息: [// ::: CST] webapp E com.ibm.ws.webcontainer.webapp.WebA ...
- redis《三》连接池配置参数
参数 值 setTestWhileIdle() 在空闲时检查有效性 true setMinEvictableIdleTimeMillis() 连接最小空闲时间 1800000L setTimeBetw ...
- Redis学习---Redis操作之Python连接
PyCharm下的Redis连接 连接方式: 1. 操作模式 redis-py提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使 ...
- Redis02 Redis客户端之Java、连接远程Redis服务器失败
1 查看支持Java的redis客户端 本博文采用 Jedis 作为redis客户端,采用 commons-pool2 作为连接redis服务器的连接池 2 下载相关依赖与实战 2.1 到 Repos ...
随机推荐
- ClassLoader.loadClass()与Class.forName()的区别《 转》
ClassLoader.loadClass()与Class.forName()区别: ClassLoader.loadClass()与Class.forName()大家都知道是反射用来构造类的方法,但 ...
- 6.Vue.js-条件与循环
条件判断 v-if 条件判断使用 v-if 指令: <div id="app"> <p v-if="seen">现在你看到我了</ ...
- 前端避坑指南丨辛辛苦苦开发的 APP 竟然被判定为简单网页打包?
传统混合移动App开发模式,通常会使用WebView作为桥接层,但随着iOS和Android应用商店审核政策日趋严格,有时会被错误判定为简单网页打包成App,上架容易遭到拒绝. 既然可能存在风险,那我 ...
- Linux 目录结构及详细操作
目录 Linux 目录结构及详细操作 目录结构 目录结构的特点 目录结构挂载 目录结构发展 关闭selinux(了解) 重要目录说明(etc目录说明) 1.网卡配置文件 2.解析配置文件 3.主机名称 ...
- centos7部署mysql-5.7
目录 一.环境声明 二.程序部署 三.更改初始密码 一.环境声明 [mysql-Server] 主机名 = host-1 系统 = centos-7.3 地址 = 1.1.1.1 软件 = mysql ...
- tableau绘制热力地图
一.右键国家地区和城市字段分别设置为地理角色-国家地区和城市 二.双击国家地区和城市添加到工作表 三.把订单id拖拽至标记卡的详细信息,标记改为密度显示,颜色设置为温度发散 四.最终整理结果如下图所示
- 学习整理--vue篇(1)
vue学习 vue指令 模板指令: v-model:绑定data数据实现数据双向绑定 v-html:绑定模板内容,可书写标签 v-text:绑定数据实现单向绑定 可缩写为{{}} 支持逻辑运算 可结合 ...
- 转: iPhone屏幕尺寸、分辨率及适配
1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角线 Diagonal 逻辑分辨率(point) Scale Factor 设备分辨率(pixel) PPI 3GS ...
- Spring Cloud Eureka源码分析之三级缓存的设计原理及源码分析
Eureka Server 为了提供响应效率,提供了两层的缓存结构,将 Eureka Client 所需要的注册信息,直接存储在缓存结构中,实现原理如下图所示. 第一层缓存:readOnlyCache ...
- supermarket(uaf)!!!!
在这道题目我花费了很长的时间去理解,因为绕进了死圈子 例行检查我就不放了 关键处在于选择5 使用了realloc,却没有让结构体指针node-> description正确指回去 (11条消息) ...