您的位置:首页 > 数据库 > Redis

redis消息队列相关函数

2016-10-20 11:16 417 查看
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`
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: