voryx/pgasync
Composer 安装命令:
composer require voryx/pgasync
包简介
Async Reactive Postgres Driver for PHP (Non-blocking)
README 文档
README
PgAsync
Asynchronous Reactive Postgres Library for PHP (Non-blocking)
What it is
This is an asynchronous Postgres library for PHP. Observables are returned by the query methods allowing asynchronous row-by-row data handling (and other Rx operators on the data) See Rx.PHP. Network and event processing is handled by ReactPHP.
This is a pure PHP implementation (you don't need Postgres extensions to use it).
Example - Simple Query
$client = new PgAsync\Client([ "host" => "127.0.0.1", "port" => "5432", "user" => "matt", "database" => "matt" ]); $client->query('SELECT * FROM channel')->subscribe( function ($row) { var_dump($row); }, function ($e) { echo "Failed.\n"; }, function () { echo "Complete.\n"; } );
Example - parameterized query
$client = new PgAsync\Client([ "host" => "127.0.0.1", "port" => "5432", "user" => "matt", "database" => "matt", "auto_disconnect" => true //This option will force the client to disconnect as soon as it completes. The connection will not be returned to the connection pool. ]); $client->executeStatement('SELECT * FROM channel WHERE id = $1', ['5']) ->subscribe( function ($row) { var_dump($row); }, function ($e) { echo "Failed.\n"; }, function () { echo "Complete.\n"; } );
Example - LISTEN/NOTIFY
$client = new PgAsync\Client([ "host" => "127.0.0.1", "port" => "5432", "user" => "matt", "database" => "matt" ]); $client->listen('some_channel') ->subscribe(function (\PgAsync\Message\NotificationResponse $message) { echo $message->getChannelName() . ': ' . $message->getPayload() . "\n"; }); $client->query("NOTIFY some_channel, 'Hello World'")->subscribe();
Example - Connecting over TLS with CA certificate file
$client = new PgAsync\Client([ "host" => "127.0.0.1", "port" => "5432", "user" => "matt", "database" => "matt", "tls" => "verify-full", "tls_connector_flags" => [ "cafile" => "/path/to/ca.crt", ], ]); $client->query('SELECT * FROM channel')->subscribe( function ($row) { var_dump($row); }, function ($e) { echo "Failed.\n"; }, function () { echo "Complete.\n"; } );
Install
With composer install into you project with:
Install pgasync:
composer require voryx/pgasync
What it can do
- Run queries (CREATE, UPDATE, INSERT, SELECT, DELETE)
- Queue commands
- Return results asynchronously (using Observables - you get data one row at a time as it comes from the db server)
- Prepared statements (as parameterized queries)
- Connection pooling (basic pooling)
What it can't quite do yet
- Transactions (Actually though, just grab a connection and you can run your transaction on that single connection)
What's next
- Add more testing
- Transactions
- Take over the world
Keep in mind
This is an asynchronous library. If you begin 3 queries (subscribe to their observable):
$client->query("SELECT * FROM table1")->subscribe(...); $client->query("SELECT * FROM table2")->subscribe(...); $client->query("SELECT * FROM table3")->subscribe(...);
It will start all of them almost simultaneously (and you will begin receiving rows on all 3 before any of them have completed). This can be great if you want to run 3 queries at the same time, but if you have some queries that need information that was modified by other statements, this can cause a race condition:
$client->query("INSERT INTO invoices(inv_no, customer_id, amount) VALUES('1234A', 1, 35.75)")->subscribe(...); $client->query("SELECT SUM(amount) AS balance FROM invoices WHERE customer_id = 1")->subscribe(...);
In the above situation, your balance may or may not include the invoice inserted on the first line.
You can avoid this by using the Rx concat* operator to only start up the second observable after the first has completed:
$insert = $client->query("INSERT INTO invoices(inv_no, customer_id, amount) VALUES('1234A', 1, 35.75)"); $select = $client->query("SELECT SUM(amount) AS balance FROM invoices WHERE customer_id = 1"); $insert ->concat($select) ->subscribe(...);
Testing
We use docker to run a postgresql instance for testing. To run locally, just install docker and run the following command from the project root:
docker-compose -f docker/docker-compose.yml up -d
If you need to reset the database, just stop the docker instance and delete
the docker/database directory. Restart the docker with the above command and it will
initialize the database again.
The tests do not change the ending structure of the database, so you should not normally need to do this.
voryx/pgasync 适用场景与选型建议
voryx/pgasync 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 98.21k 次下载、GitHub Stars 达 112, 最近一次更新时间为 2015 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「postgresql」 「pgsql」 「postgres」 「async」 「driver」 「react」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 voryx/pgasync 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 voryx/pgasync 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 voryx/pgasync 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SilverStripe now has tentative support for PostgreSQL ('Postgres')
Dibi is Database Abstraction Library for PHP
Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.
Enum type behavior and helper for Yii2, for PostgreSQL only
PostgreSQL ROUND() function for Doctrine ORM
Slimmed down concise interfaces and query builder for database queries and transactions which can be layered / decorated.
统计信息
- 总下载量: 98.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 112
- 点击次数: 6
- 依赖项目数: 7
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-12