cloudtay/ripple-rdo

There is no license information available for the latest version (dev-main) of this package.

dev-main 2024-10-31 16:19 UTC

This package is auto-updated.

Last update: 2024-10-31 19:14:03 UTC


README

项目简介

基于PHP自举的PDO驱动, 目前仅支持MySQL数据库。

兼容版本

兼容版本指的是在该版本下测试通过的版本号,不代表其他版本不可用

快速安装

composer require cloudtay/ripple-rdo

基础用法

就像使用PDO一样使用RDO驱动

include __DIR__ . '/../vendor/autoload.php';

/**
 * @var \PDO $rdo 已继承不冲突
 */
$rdo = new \Ripple\RDO(
    'mysql:host=127.0.0.1;port=3306;dbname=hello_world;charset=utf8',
    'benchmarkdbuser',
    'benchmarkdbpass',
);

echo 'connection established' , \PHP_EOL;

$statement    = $rdo->prepare("UPDATE `World` SET `randomNumber` = ? WHERE `id` = 1;");
$statement->execute([':randomNumber' => \rand(1, 10000)]);

echo 'update executed ' , $statement->rowCount() , \PHP_EOL;

异步用法

include __DIR__ . '/../vendor/autoload.php';

\Co\async(static function(){
    echo 'coroutine 1 start ' , microtime(true) , \PHP_EOL;
    $rdo1 = new \Ripple\RDO(
        'mysql:host=127.0.0.1;port=3306;dbname=hello_world;charset=utf8',
        'benchmarkdbuser',
        'benchmarkdbpass',
    );
    
    //模拟耗时一秒查询
    $rdo1->query("SELECT SLEEP(1);");
    
    echo 'coroutine 1 end ' , microtime(true) , \PHP_EOL;
});


\Co\async(static function(){
    echo 'coroutine 2 start ' , microtime(true) , \PHP_EOL;
    $rdo2 = new \Ripple\RDO(
        'mysql:host=127.0.0.1;port=3306;dbname=hello_world;charset=utf8',
        'benchmarkdbuser',
        'benchmarkdbpass',
    );
    
    //模拟耗时一秒查询
    $rdo1->query("SELECT SLEEP(1);");
    
    echo 'coroutine 2 end ' , microtime(true) , \PHP_EOL;
});


\Co\wait();

附录

上述例子中的数据库一键部署

docker run -d --name tfb-database -p 3306:3306 techempower/mysql:latest