跳到内容

Couchbase Collection 缓存适配器

编辑此页

此适配器使用一个(或多个)Couchbase 服务器实例在内存中存储值。与 APCu 适配器不同,并且与 Memcached 适配器类似,它不限于当前服务器的共享内存;您可以独立于 PHP 环境存储内容。还可以使用服务器集群来提供冗余和/或故障转移。

警告

要求: 必须安装、激活并运行 Couchbase PHP 扩展Couchbase 服务器才能使用此适配器。此适配器需要版本 3.0 或更高版本的 Couchbase PHP 扩展

此适配器期望将 Couchbase Collection 实例作为第一个参数传递。命名空间和默认缓存生命周期可以分别作为第二个和第三个参数可选传递。

1
2
3
4
5
6
7
8
9
10
11
12
13
use Symfony\Component\Cache\Adapter\CouchbaseCollectionAdapter;

$cache = new CouchbaseCollectionAdapter(
    // the client object that sets options and adds the server instance(s)
    $client,

    // a string prefixed to the keys of the items stored in this cache
    $namespace,

    // the default lifetime (in seconds) for cache items that do not define their
    // own lifetime, with a value 0 causing items to be stored indefinitely
    $defaultLifetime
);

配置连接

createConnection() 辅助方法允许使用 数据源名称 (DSN) 或 DSN 数组创建和配置 Couchbase Collection 类实例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use Symfony\Component\Cache\Adapter\CouchbaseCollectionAdapter;

// pass a single DSN string to register a single server with the client
$client = CouchbaseCollectionAdapter::createConnection(
    'couchbase://127.0.0.1'
    // the DSN can include config options (pass them as a query string):
    // 'couchbase://127.0.0.1:11210?operationTimeout=10'
    // 'couchbase://127.0.0.1:11210?operationTimeout=10&configTimout=20'
);

// pass an array of DSN strings to register multiple servers with the client
$client = CouchbaseCollectionAdapter::createConnection([
    'couchbase://10.0.0.100',
    'couchbase://10.0.0.101',
    'couchbase://10.0.0.102',
    // etc...
]);

// a single DSN can define multiple servers using the following syntax:
// host[hostname-or-IP:port] (where port is optional). Sockets must include a trailing ':'
$client = CouchbaseCollectionAdapter::createConnection(
    'couchbase:?host[localhost]&host[localhost:12345]'
);

配置选项

createConnection() 辅助方法还接受一个选项数组作为其第二个参数。预期的格式是表示选项名称及其各自值的 key => value 键值对关联数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
use Symfony\Component\Cache\Adapter\CouchbaseCollectionAdapter;

$client = CouchbaseCollectionAdapter::createConnection(
    // a DSN string or an array of DSN strings
    [],

    // associative array of configuration options
    [
        'username' => 'xxxxxx',
        'password' => 'yyyyyy',
        'configTimeout' => '100',
    ]
);

可用选项

username (类型: string)
连接 CouchbaseCluster 的用户名。
password (类型: string)
连接 CouchbaseCluster 的密码。
operationTimeout (类型: int, 默认值: 2500000)
操作超时(以微秒为单位)是库在调用回调并返回失败状态之前,等待操作接收响应的最长时间。
configTimeout (类型: int, 默认值: 5000000)
客户端等待获取初始配置的时长(以微秒为单位)。
configNodeTimeout (类型: int, 默认值: 2000000)
每个节点的配置超时时间(以微秒为单位)。
viewTimeout (类型: int, 默认值: 75000000)
用于 Couchbase Views API 的 HTTP 请求的 I/O 超时时间(以微秒为单位)。
httpTimeout (类型: int, 默认值: 75000000)
用于 HTTP 查询(管理 API)的 I/O 超时时间(以微秒为单位)。
configDelay (类型: int, 默认值: 10000)
配置刷新节流。修改配置错误阈值强制设置为其最大数量以强制配置刷新的时间量(以微秒为单位)。
htconfigIdleTimeout (类型: int, 默认值: 4294967295)
HTTP 引导的空闲/持久性(以微秒为单位)。
durabilityInterval (类型: int, 默认值: 100000)
客户端在重复探测给定服务器之间等待的时间(以微秒为单位)。
durabilityTimeout (类型: int, 默认值: 5000000)
客户端在向给定键的 vBucket 主节点和副本发送重复探测之前,将花费的时间(以微秒为单位),之后它们将被认为未满足持久性要求。

提示

参考 Couchbase Collection 扩展的 预定义常量 文档,以获取有关可用选项的更多信息。

本作品,包括代码示例,根据 Creative Commons BY-SA 3.0 许可获得许可。
目录
    版本