moon/couchbaserestclient

该包最新版本(v0.1)没有可用的许可证信息。

v0.1 2018-02-19 07:43 UTC

This package is not auto-updated.

Last update: 2024-09-20 01:43:07 UTC


README

Couchbase REST API 包装器,可轻松访问视图和 N1QL 查询,无需安装 Couchbase C 和 PHP SDK。

安装

composer require moon/couchbaserestclient

优点

  • 无需安装 C 和 PHP SDK 即可访问 Couchbase。
  • 支持使用多 curl 进行并行查询处理。

无需安装 C 和 PHP SDK 即可访问 Couchbase 很方便,但这并不是一个很大的优势,因为一旦安装,您就无法真正卸载 C 和 PHP SDK。Couchbase 有批处理操作(https://developer.couchbase.com/documentation/server/current/sdk/php/document-operations.html#topic_eqq_rmd_yv__batching),即使在 PHP 中也可以插入/获取多个文档。然而,您无法在 PHP SDK 中并行发送多个 n1ql 查询。我发现 Couchbase REST API 方法的一个非常实用之处在于,您可以并行发送多个更新请求,而这在官方 PHP SDK 中是不可能的(或很难)实现的。

缺点

  • Couchbase Rest API 端点可能不如其官方 SDK 表现得好,因为官方 SDK 采用了优化的协议。

设置

用法

创建 CouchbaseRestApiClient 实例

$client = new CouchbaseRestApiClient("http://couchbase:8091", "username", "password");
$client->setN1qlHost("http://couchbase:8093/service/query");

查询视图

$paginator = $client->createViewPaginator('designDocumentName', 'viewName');
$paginator->setStartKey("startKey");
foreach ($paginator as $page) {
    var_dump($page);
}

查询 N1QL

同步查询
$query = "select * from testBucket use keys ['testKey']";
$response = $client->queryN1ql($query);
var_dump($response);
并行查询

您可以使用 ParallelQueryQueue 向 Couchbase 发送多个查询。

// Let's send 30 queries at once
$parallelQueryQueue = $client->createParallelQueryQueue(30);

// queue 90 queries
for($i=0; $i<=89; $i++) {
    $key = "testKey".$i;
    $parallelQueryQueue->queue("update myBucket use keys ['{$key}'] set index=$i";
}

// run
while($responses = $parallelQueryQueue->next()) {

}