Redis的函数的使用
Redis::__construct
说明:
创建一个Redis客户端
范例:
$redis = new Redis();
connect, open
说明:
实例连接到一个Redis.
参数:
Host:string,可以是一个host地址,也可以是一个unix socket
port: int
timeout: float 秒数,(可选参数,默认值为0代表不限制)
返回值:
BOOL 成功返回:TRUE;失败返回:FALSE
范例:
$redis->connect('127.0.0.1', 6379);
$redis->connect('127.0.0.1'); // port 6379 by default
$redis->connect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout.
$redis->connect('/tmp/redis.sock'); // unix domain socket.
pconnect, popen
说明:
实例连接到一个Redis.,或者连接到一个已经通过pconnect/popen创建的连接上。
连接直到遇到close或者php进程结束才会被关闭。
参数:
host: string
port: int
timeout: float
persistent_id: string 持久链接的身份验证
返回值:
BOOL 成功返回:TRUE;失败返回:FALSE
范例:
$redis->pconnect('127.0.0.1', 6379);
$redis->pconnect('127.0.0.1'); // port 6379 by default - same connection like before.
$redis->pconnect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout and would be another connection than the two before.
$redis->pconnect('127.0.0.1', 6379, 2.5, 'x'); // x is sent as persistent_id and would be another connection the the three before.
$redis->pconnect('/tmp/redis.sock'); // unix domain socket - would be another connection than the four before.
close
说明:
断开一个Redis实例连接,除非他是通过pconnect 链接的。
setOption
说明:
创建客户端选项。
参数:
Name
Value
返回值:
BOOL 成功返回:TRUE;失败返回:FALSE
范例:
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // don't serialize data
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // use built-in serialize/unserialize
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // use igBinary serialize/unserialize
$redis->setOption(Redis::OPT_PREFIX, 'myAppName:'); // use custom prefix on all keys
getOption
获得客户端选项
参数:
Name
返回值:
Value
范例:
$redis->getOption(Redis::OPT_SERIALIZER); // return Redis::SERIALIZER_NONE, Redis::SERIALIZER_PHP, or Redis::SERIALIZER_IGBINARY.
ping
说明:
检查当前的连接状态。
范例:
$redis->watch('x');
/* long code here during the execution of which other clients could well modify `x` */
$ret = $redis->multi()
->incr('x')
->exec();
/*
$ret = FALSE if x has been modified between the call to WATCH and the call to EXEC.
*/
subscribe
说明:
方法回调。注意,该方法可能在未来里发生改变
参数:
channels: array
callback: 回调函数名
范例:
function f($redis, $chan, $msg) {
switch($chan) {
case 'chan-1':
...
break;
case 'chan-2':
...
break;
case 'chan-2':
...
break;
}
}
$redis->subscribe(array('chan-1', 'chan-2', 'chan-3'), 'f'); // subscribe to 3 chans