php redis管道,PHP中利用Redis管道加快执行

~~~

$redis->muti($mode)

->get($key)

->set($key)

->exec();

~~~

既然是这样的, 当我要使用管道执行一万次操作的时候,需要写一万次操作在muti()的后面,有没有更好的写法?

设计者没有想到这个问题么?

~~~

$redis = new Redis();

$redis->connect('10.1.132.86', 6379);

$pipe = $redis->multi(Redis::PIPELINE);

for ($i = 0; $i < 10000; $i++) {

$pipe->set("key::$i", str_pad($i, 4, '0', 0));

$pipe->get("key::$i");

}

$replies = $pipe->exec(); echo " "; print_r($replies);

~~~

今天测试成功了

* * * * *

#### Description: Enter and exit transactional mode.

#### Parameters

(optional) Redis::MULTI or Redis::PIPELINE. Defaults toRedis::MULTI.

A Redis::MULTI block of commands runs as a single transaction; aRedis::PIPELINE block is simply transmitted faster to the server, but without any guarantee of atomicity.discard cancels a transaction.

#### Return value

multi() returns the Redis instance and enters multi-mode. Once in multi-mode, all subsequent method calls return the same object untilexec() is called.

#### Example

~~~

$ret = $redis->multi()

->set('key1', 'val1')

->get('key1')

->set('key2', 'val2')

->get('key2')

->exec();

/*

$ret == array(

0 => TRUE,

1 => 'val1',

2 => TRUE,

3 => 'val2');

*/

~~~

> mget也可以实现简单数据多条记录的读取

#### mGet, getMultiple

Description: Get the values of all the specified keys. If one or more keys dont exist, the array will contain FALSE at the position of the key.

#### Parameters

Array: Array containing the list of the keys

#### Return value

Array: Array containing the values related to keys in argument

Examples

~~~

$redis->set('key1', 'value1');

$redis->set('key2', 'value2');

$redis->set('key3', 'value3');

# $redis->mSet(array('key1' => 'value1', 'key2 => 'value2', 'key3 => 'value3'));

$redis->mGet(array('key1', 'key2', 'key3')); /* array('value1', 'value2', 'value3');

$redis->mGet(array('key0', 'key1', 'key5')); /* array(`FALSE`, 'value2', `FALSE`);

~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值