CakeFest 2024: The Official CakePHP Conference

Memcache::pconnect

(PECL memcache >= 0.4.0)

Memcache::pconnect打开一个到服务器的持久化连接

说明

Memcache::pconnect(string $host, int $port = ?, int $timeout = ?): mixed

Memcache::pconnect()Memcache::connect() 非常类似,不同点在于这里建立的连接是持久化的。 这个连接不会在脚本执行结束后或者 Memcache::close() 被调用后关闭。 同样你也可以使用函数 memcache_pconnect()

参数

host

服务端监听的主机地址。这个参数还可以指定为其他传输方式比如 unix:///path/to/memcached.sock 来使用 Unix 域套接字,使用这种方式 port 参数必须设置为 0

port

服务端监听的端口号。使用 Unix 域套接字的时候需要将这个参数设置为 0

timeout

连接持续(超时)时间,单位秒。默认值 1 秒,修改此值之前请三思,过长的连接持续时间可能会导致失去所有的缓存优势。

返回值

返回一个 Memcache 对象 或者在失败时返回 false.

示例

示例 #1 Memcache::pconnect() 示例

<?php

/* procedural API */
$memcache_obj = memcache_pconnect('memcache_host', 11211);

/* OO API */

$memcache_obj = new Memcache;
$memcache_obj->pconnect('memcache_host', 11211);

?>

参见

add a note

User Contributed Notes 2 notes

up
1
john.royer [at] gmail.com
5 years ago
pconnect() put error message to stderr if connection failed. This behavior may cause unexpected output.
use '@' infrom of `pconnect()` to avoid it.

<?php

$cache
= new Memcache();
$stat = @$cache->pconnect('localhost', 11211);

if (
false === $stat) {
// connect failed
}
// connect success
up
-26
office at cws-trummer dot biz
15 years ago
use persistent connection if you have problems with system process 0 WAIT_TIME
To Top