偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

PHP異步非阻塞MySQL客戶端連接池

開發(fā) 前端
客戶端透明地將這些查詢分布在一個可擴展的可用連接池中,并使用100%的用戶態(tài)PHP,沒有外部擴展依賴性(例如ext/mysqli,ext/pdo等)。

概述

AMPHP是一個事件驅動的PHP庫集合,設計時考慮了纖程和并發(fā)性。amphp/mysql是一個異步MySQL客戶端。該庫通過在可用連接的可伸縮池中透明地分發(fā)查詢來實現(xiàn)并發(fā)查詢??蛻舳送该鞯貙⑦@些查詢分布在一個可擴展的可用連接池中,并使用100%的用戶態(tài)PHP,沒有外部擴展依賴性(例如ext/mysqli,ext/pdo等)。

特征

  • 公開一個非阻塞API,用于并發(fā)發(fā)出多個MySQL查詢
  • 透明的連接池克服了MySQL的基本同步連接協(xié)議
  • MySQL傳輸編碼支持(gzip,TLS加密)
  • 支持參數(shù)化預處理語句
  • 帶有提交和回滾事件鉤子的嵌套事務
  • 無緩沖結果以減少大型結果集的內存使用
  • 完整的MySQL協(xié)議支持,包括所有可用的異步命令

安裝

此包可以作為Composer依賴項安裝

composer require amphp/mysql

使用

入門使用

<?php
/**
 * @desc mysql.php
 * @author Tinywan(ShaoBo Wan)
 * @date 2024/8/16 11:19
 */
declare(strict_types=1);

require 'vendor/autoload.php';

use Amp\Mysql\MysqlConfig;
use Amp\Mysql\MysqlConnectionPool;

$config = MysqlConfig::fromString(
    "host=127.0.0.1 user=root password=123456 db=test"
);

$pool = new MysqlConnectionPool($config);

$statement = $pool->prepare("SELECT * FROM mall_member WHERE member_time = :member_time Limit 10");
$timeOne = microtime(true);
$result = $statement->execute(['member_time' => 0]);
foreach ($result as $key => $row) {
    echo '[x] ['.$key.'] '.$row['member_name'].PHP_EOL;
}
$timeTwo = microtime(true);

echo '[x] Run Time Result : ' . ($timeTwo - $timeOne) . PHP_EOL;

執(zhí)行結果:

[x] [0] 12161435
[x] [1] 開源技術小棧
[x] [2] 12161435
[x] [3] 12161435
[x] [4] T1800082
[x] [5] 12161435
[x] [6] 12161435
[x] [7] 12161387
[x] [8] 12161235
[x] [9] 12161149
[x] Run Time Result : 0.045973062515259

迭代器

<?php

require 'support/bootstrap.php';

use Amp\Future;
use Amp\Mysql\MysqlConfig;
use Amp\Mysql\MysqlConnectionPool;
use function Amp\async;

$db = new MysqlConnectionPool(MysqlConfig::fromAuthority(DB_HOST, DB_USER, DB_PASS, DB_NAME));

$db->query("DROP TABLE IF EXISTS tmp");

/* Create table and insert a few rows */
/* we need to wait until table is finished, so that we can insert. */
$db->query("CREATE TABLE IF NOT EXISTS tmp (a INT(10), b INT(10))");

print "Table successfully created." . PHP_EOL;

$statement = $db->prepare("INSERT INTO tmp (a, b) VALUES (?, ? * 2)");

$future = [];
foreach (\range(1, 5) as $num) {
    $future[] = async(fn () => $statement->execute([$num, $num]));
}

/* wait until everything is inserted */
$results = Future\await($future);

print "Insertion successful (if it wasn't, an exception would have been thrown by now)" . PHP_EOL;

$result = $db->query("SELECT a, b FROM tmp");

foreach ($result as $row) {
    var_dump($row);
}

$db->query("DROP TABLE tmp");

$db->close();

事務支持

<?php

require 'support/bootstrap.php';
require 'support/generic-table.php';

use Amp\Mysql\MysqlConfig;
use Amp\Mysql\MysqlConnectionPool;

$db = new MysqlConnectionPool(MysqlConfig::fromAuthority(DB_HOST, DB_USER, DB_PASS, DB_NAME));

/* create same table than in 3-generic-with-yield.php */
createGenericTable($db);

$transaction = $db->beginTransaction();

$transaction->execute("INSERT INTO tmp VALUES (?, ? * 2)", [6, 6]);

$result = $transaction->execute("SELECT * FROM tmp WHERE a >= ?", [5]); // Two rows should be returned.

foreach ($result as $row) {
    \var_dump($row);
}

$transaction->rollback();

// Run same query again, should only return a single row since the other was rolled back.
$result = $db->execute("SELECT * FROM tmp WHERE a >= ?", [5]);

foreach ($result as $row) {
    \var_dump($row);
}

$db->close();

責任編輯:武曉燕 來源: 開源技術小棧
相關推薦

2020-11-17 08:53:07

MySQL數(shù)據(jù)庫技術

2011-06-01 13:54:10

MySQL

2019-12-30 15:30:13

連接池請求PHP

2009-11-09 15:49:01

WCF異步調用

2009-07-09 16:12:53

WeblogicJDBC

2010-06-09 14:39:58

2019-07-23 11:01:57

Python同步異步

2010-05-17 16:38:08

MySQL 連接池

2011-05-13 09:34:51

TomcatMysql連接池

2021-03-24 09:06:01

MySQL長連接短連接

2022-09-22 10:51:32

服務端開發(fā)者異步非阻塞編程

2021-03-04 08:34:55

同步阻塞非阻塞

2010-10-11 17:46:01

mysql客戶端

2012-10-10 10:00:27

同步異步開發(fā)Java

2017-05-24 08:58:16

HiveServer界面工具

2021-08-06 10:37:34

ElasticOpenSearch開發(fā)者

2020-03-24 15:15:29

HttpClientOkHttpJava

2010-07-12 10:05:08

MemcachedPHP

2009-11-25 13:21:30

PHP作為memcac

2021-09-22 15:46:29

虛擬桌面瘦客戶端胖客戶端
點贊
收藏

51CTO技術棧公眾號