[收藏]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

说明:检查当前的连接状态。
参数:无
返回值:STRING:PONG 失败则会返回一个Redis抛出的连接异常。

get

说明:获得一个指定的key的值。
参数:Key
返回值:String or Bool: 如果值存在则返回值,否则返回false。
范例:

$redis->get('key');

set

说明:创建一个值
参数:Key Value Timeout (可选)可以在一定的timeout时间内让SETEX 优先调用。
返回值:成功返回true
范例:

$redis->set('key', 'value');

setex
说明:创建一个有一定存活时间的值
参数:Key TTL Value
返回值:成功返回true
范例:

$redis->setex('key', 3600, 'value'); // sets key → value, with 1h TTL.

setnx

如果key的值不存在,则创建key的值为value
参数:Key Value
返回值:成功返回true 失败返回false
范例:

$redis->setnx('key', 'value'); /* return TRUE */
$redis->setnx('key', 'value'); /* return FALSE */

del, delete

说明:删除一个指定的key的值
参数:可以是一个数组,也可以是一个多个字符串。
返回值:成功删除的个数
范例:

$redis->set('key1', 'val1');
$redis->set('key2', 'val2');
$redis->set('key3', 'val3');
$redis->set('key4', 'val4');

$redis->delete('key1', 'key2'); /* return 2 */
$redis->delete(array('key3', 'key4')); /* return 2 */

multi, exec, discard.

说明:进入或者退出事务模式
参数:(可选)
Redis::MULTI或Redis::PIPELINE. 默认是 Redis::MULTI
Redis::MULTI:将多个操作当成一个事务执行
Redis::PIPELINE:让(多条)执行命令简单的,更加快速的发送给服务器,但是没有任何原子性的保证
discard:删除一个事务
返回值:
multi(),返回一个redis对象,并进入multi-mode模式,一旦进入multi-mode模式,以后调用的所有方法都会返回相同的对象,只到exec()方法被调用。
范例:

$ret = $redis->multi()
    ->set('key1', 'val1')
    ->get('key1')
    ->set('key2', 'val2')
    ->get('key2')
    ->exec();
/*
$ret == array(
    0 => TRUE,
    1 => 'val1',
    2 => TRUE,
    3 => 'val2');
*/

watch, unwatch

说明:
监测一个key的值是否被其它的程序更改。如果这个key在watch 和 exec (方法)间被修改,这个 MULTI/EXEC 事务的执行将失败(return false)
unwatch 取消被这个程序监测的所有key
参数:Keys:一对key的列表
范例:

$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

Publish

说明:发表内容到某一个通道。注意,该方法可能在未来里发生改变
参数:Channel: Messsage:string
范例:

$redis->publish('chan-1', 'hello, world!'); // send message.

exists

说明:验证指定的值是否存在
参数:Key
返回值:成功返回true 失败返回false
范例:

$redis->set('key', 'value');
$redis->exists('key'); /*  TRUE */
$redis->exists('NonExistingKey'); /* FALSE */

incr, incrBy
说明:key中的值进行自增.如果第二个参数存在,它将被用来作为整数值递增
参数:Key Value
返回值:返回新value
范例:

$redis->incr('key1'); /* key1 didn't exists, set to 0 before the increment */
/* and now has the value 1  */

$redis->incr('key1'); /* 2 */
$redis->incr('key1'); /* 3 */
$redis->incr('key1'); /* 4 */
$redis->incrBy('key1', 10); /* 14 */

decr, decrBy

说明:删掉key中的值,用法同incr
范例:

$redis->decr('key1'); /* key1 didn't exists, set to 0 before the increment */
/* and now has the value -1  */
$redis->decr('key1'); /* -2 */
$redis->decr('key1'); /* -3 */
$redis->decrBy('key1', 10); /* -13 */

getMultiple

说明:返回一组数据的值,如果这个数组中的key值不存在,则返回false
参数:Array
返回值:Array
范例:

$redis->set('key1', 'value1');
$redis->set('key2', 'value2');
$redis->set('key3', 'value3');
$redis->getMultiple(array('key1', 'key2', 'key3')); /* array('value1', 'value2', 'value3');
$redis->getMultiple(array('key0', 'key1', 'key5')); /* array(`FALSE`, 'value2', `FALSE`);

lPush

说明:在名称为key的list左边(头)添加一个值为value的 元素,如果这个key值不存在则创建一个。如果key值存在并且不是一个list,则返回false
参数:Key Value
返回值:返回key值得长度。
范例:

$redis->delete('key1');
$redis->lPush('key1', 'C'); // returns 1
$redis->lPush('key1', 'B'); // returns 2
$redis->lPush('key1', 'A'); // returns 3
/* key1 now points to the following list: [ 'A', 'B', 'C' ] */

rPush
说明:
在名称为key的list右边(尾)添加一个值为value的 元素,如果这个key值不存在则创建一个。如果key值存在并且不是一个list,则返回false
参数:Key Value
返回值:返回key值得长度。
范例:

$redis->delete('key1');
$redis->rPush('key1', 'A'); // returns 1
$redis->rPush('key1', 'B'); // returns 2
$redis->rPush('key1', 'C'); // returns 3
/* key1 now points to the following list: [ 'A', 'B', 'C' ] */

lPushx

说明:在名称为key的list左边(头)添加一个值为value的 元素,如果这个value存在则不添加。
参数:Key Value
返回值:返回key值得长度。
范例:

$redis->delete('key1');
$redis->lPushx('key1', 'A'); // returns 0
$redis->lPush('key1', 'A'); // returns 1
$redis->lPushx('key1', 'B'); // returns 2
$redis->lPushx('key1', 'C'); // returns 3
/* key1 now points to the following list: [ 'A', 'B', 'C' ] */

rPushx

说明:在名称为key的list右边(尾)添加一个值为value的 元素,如果这个value存在则不添加。
参数:Key Value
返回值:返回key值得长度。
范例:

$redis->delete('key1');
$redis->rPushx('key1', 'A'); // returns 0
$redis->rPush('key1', 'A'); // returns 1
$redis->rPushx('key1', 'B'); // returns 2
$redis->rPushx('key1', 'C'); // returns 3
/* key1 now points to the following list: [ 'A', 'B', 'C' ] */

lPop

说明:输出名称为key的list左(头)起起的第一个元素,删除该元素
参数:Key
返回值:失败返回false
范例:

$redis->rPush('key1', 'A
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值