连接池使用说明

  • 所有连接池的实现均基于 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 连接池的更多相关文章

  1. 压测过程中,获取不到redis连接池,发现redis连接数高

    说明:图片截得比较大,浏览器放大倍数看即可(涉及到隐私,打了码,请见谅,如果有疑问,欢迎骚扰). 最近在压测过程中,出现获取不到redis连接池的问题 xshell连接redis服务器,查看连接数,发 ...

  2. 如何在 Swoole 中优雅的实现 MySQL 连接池

    如何在 Swoole 中优雅的实现 MySQL 连接池 一.为什么需要连接池 ? 数据库连接池指的是程序和数据库之间保持一定数量的连接不断开, 并且各个请求的连接可以相互复用, 减少重复连接数据库带来 ...

  3. Redis 简单使用 and 连接池(python)

    Redis 简介 NoSQL(not only sql):非关系型数据库 支持 key-value,  list,  set,  zset,  hash 等数据结构的存储:支持主从数据备份,集群:支持 ...

  4. ServiceStack.Redis连接阿里云redis服务时使用连接池出现的问题

    创建连接池 private static PooledRedisClientManager prcm = CreateManager(new string[] { "password@ip: ...

  5. Java与redis交互、Jedis连接池JedisPool

    Java与redis交互比较常用的是Jedis. 先导入jar包: commons-pool2-2.3.jar jedis-2.7.0.jar 基本使用: public class RedisTest ...

  6. WebSphere中数据源连接池太小导致的连接超时错误记录

    WebSphere中数据源连接池太小导致的连接超时错误记录. 应用连接超时错误信息: [// ::: CST] webapp E com.ibm.ws.webcontainer.webapp.WebA ...

  7. redis《三》连接池配置参数

    参数 值 setTestWhileIdle() 在空闲时检查有效性 true setMinEvictableIdleTimeMillis() 连接最小空闲时间 1800000L setTimeBetw ...

  8. Redis学习---Redis操作之Python连接

    PyCharm下的Redis连接 连接方式: 1. 操作模式 redis-py提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使 ...

  9. Redis02 Redis客户端之Java、连接远程Redis服务器失败

    1 查看支持Java的redis客户端 本博文采用 Jedis 作为redis客户端,采用 commons-pool2 作为连接redis服务器的连接池 2 下载相关依赖与实战 2.1 到 Repos ...

随机推荐

  1. RAC中常见的高级用法-组合

    组合: concat组合:           按一定顺序执行皇上与皇太子关系 concat底层实现:     1.当拼接信号被订阅,就会调用拼接信号的didSubscribe     2.didSu ...

  2. Java Criteria使用方法

    Criteria Query 可以看作传统sql的对象化表示. Criteria 可以由session创建. Criteria ct= session.createCriteria(TUser.cla ...

  3. 解决Spring MVC @ResponseBody出现问号乱码问题

    原因是SpringMVC的@ResponseBody使用的默认处理字符串编码为ISO-8859-1,而我们前台或者客户端的编码一般是UTF-8或者GBK.现将解决方法分享如下! 第一种方法: 对于需要 ...

  4. 基于spring sringmvc mybatis 做的导入导出

    导入 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...

  5. JSP 文字乱码、${}引用无效

    问题: 代码:<form action="/test/requestPost.do" method="post"> <input type=& ...

  6. Java如何生成随机数 - Random、ThreadLocalRandom、SecureRandom

    Java7 的Random伪随机数和线程安全的ThreadLocalRandom 一.Random伪随机数: Random 类专门用于生成一个伪随机数,它有两个构造器: 一个构造器使用默认的种子(以当 ...

  7. redis迁移工具redis-migrate-tool

    目录 一.简介 二.测试 三.安装 四.验证 一.简介 redis-migrate-tool是在redis之间迁移数据的一个方便且有用的工具.他会已服务方式不断同步两边的数据.等到合适时间,中断red ...

  8. 车载以太网第二弹 | 测试之实锤-物理层PMA测试实践

    前言 本期先从物理层"PMA测试"开始,下图1为"PMA测试"的测试结果汇总图.其中,为了验证以太网通信对线缆的敏感度,特选取两组不同特性线缆进行测试对比,果然 ...

  9. SP8374 PARKET1 - PARKET 题解

    Content 有一个 \(l\times w\) 大小的网格,其四周均被染成了红色,其余部分是棕色,已知红色网格与棕色网格的数量,求 \(l\) 与 \(w\) 的值. Solution 接下来给各 ...

  10. Python的 垃圾回收机制

    垃圾回收 1. 小整数对象池 整数在程序中的使用非常广泛,Python为了优化速度,使用了小整数对象池, 避免为整数频繁申请和销毁内存空间. Python 对小整数的定义是 [-5, 257) 这些整 ...