redis消息队列相关函数

public function lPush( $key, $value1, $value2 = null, $valueN = null ) {
	$redis->lPush('l', 'v1', 'v2', 'v3', 'v4')   // int(4)
	var_dump( $redis->lRange('l', 0, -1) );
	/* Output:
	 *  array(4) {
	 *    [0]=> string(2) "v4"
	 *    [1]=> string(2) "v3"
	 *    [2]=> string(2) "v2"
	 *    [3]=> string(2) "v1"
	 *  }
	 */ 
}

public function rPush( $key, $value1, $value2 = null, $valueN = null ) {
	$redis->rPush('l', 'v1', 'v2', 'v3', 'v4');    // int(4)
	var_dump( $redis->lRange('l', 0, -1) );
	/* Output:
	 *  array(4) {
	 *    [0]=> string(2) "v1"
	 *    [1]=> string(2) "v2"
	 *    [2]=> string(2) "v3"
	 *    [3]=> string(2) "v4"
	 *  }
	 */
}

public function lPushx( $key, $value ) {
	$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' ]
}

public function rPushx( $key, $value ) {
	$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' ]
}

public function lPop( $key ) {
	$redis->rPush('key1', 'A');
	$redis->rPush('key1', 'B');
	$redis->rPush('key1', 'C');  // key1 => [ 'A', 'B', 'C' ]
	$redis->lPop('key1');        // key1 => [ 'B', 'C' ]
}

public function rPop( $key ) {
	$redis->rPush('key1', 'A');
	$redis->rPush('key1', 'B');
	$redis->rPush('key1', 'C');  // key1 => [ 'A', 'B', 'C' ]
	$redis->rPop('key1');        // key1 => [ 'A', 'B' ]
}

public function blPop( array $keys ) {
	// Non blocking feature
	$redis->lPush('key1', 'A');
	$redis->delete('key2');

	$redis->blPop('key1', 'key2', 10); // array('key1', 'A')
	// OR
	$redis->blPop(array('key1', 'key2'), 10); // array('key1', 'A')
	$redis->brPop('key1', 'key2', 10); // array('key1', 'A')
	// OR
	$redis->brPop(array('key1', 'key2'), 10); // array('key1', 'A')

	// Blocking feature

	// process 1
	$redis->delete('key1');
	$redis->blPop('key1', 10);
	// blocking for 10 seconds
	// process 2
	$redis->lPush('key1', 'A');

	// process 1
	// array('key1', 'A') is returned
}

public function brPop( array $keys ) {
	// Non blocking feature
	$redis->lPush('key1', 'A');
	$redis->delete('key2');
	$redis->blPop('key1', 'key2', 10); // array('key1', 'A')
	// OR
	$redis->blPop(array('key1', 'key2'), 10); // array('key1', 'A')

	$redis->brPop('key1', 'key2', 10); // array('key1', 'A')
	// OR
	$redis->brPop(array('key1', 'key2'), 10); // array('key1', 'A')

	// Blocking feature

	// process 1
	$redis->delete('key1');
	$redis->blPop('key1', 10);
	// blocking for 10 seconds

	// process 2
	$redis->lPush('key1', 'A');

	// process 1
	// array('key1', 'A') is returned
}

public function lLen( $key ) {
	$redis->rPush('key1', 'A');
	$redis->rPush('key1', 'B');
	$redis->rPush('key1', 'C');  // key1 => [ 'A', 'B', 'C' ]
	$redis->lLen('key1');       // 3
	$redis->rPop('key1');
	$redis->lLen('key1');       // 2
}

public function lIndex( $key, $index ) {
	* $redis->rPush('key1', 'A');
	* $redis->rPush('key1', 'B');
	* $redis->rPush('key1', 'C');  // key1 => [ 'A', 'B', 'C' ]
	* $redis->lGet('key1', 0);     // 'A'
	* $redis->lGet('key1', -1);    // 'C'
	* $redis->lGet('key1', 10);    // `FALSE`
}

















                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值