2,659   Redis

上一篇文章介绍了利用Redis的List实现优先级队列,现在介绍Sorted Set实现优先级队列的原理

 

一,具体实现:

 

1,入队操作,队列优先级为1,2,3,4,5,6 ·······, 然后把优先等级和数据一起插入Sorted Set中

$redis->delete('result_list');
$num = 0;
while (true) {
	try {   
		
		echo date('Y-m-d H:i:s').' 数据进入队列 result_list',"\n";
		for($i=1; $i<=3; $i++){ 
                    // 优先等级  
                    $score = $i+$num;  
                    $data = 'test_'.$score.'_'.date('H:i:s'); 
                    // 插入数据 $redis->zAdd('result_list',$score,$data);
		}
		if($score>9){
			$num = 0;
		}else{
			$num = $score;
		}
  		// 获取所有数据
  		$result = $redis->zRangeByScore('result_list','-inf','+inf',array('withscores'=>true));

  		var_dump($result);
		
  		$seconds = 1;
		echo date('H:i:s').' 等待'.$seconds.'秒',"\n";
		sleep($seconds);
	} catch (Exception $e) {   
		var_dump($e->getMessage());   
	}   

}

 

2,出队处理,每次读取优先级前三的数据进行处理

 

while (true) {
	try {   
		echo date('H:i:s').' 读取队列数据',"\n";
                // 读取优先级前三的数据
		$result = $redis->zRangeByScore('result_list','-inf','+inf',array('withscores'=>true,'limit' => array(0, 3)));
		echo date('H:i:s').' 读到数据为:',"\n";
		var_dump($result);
	
		if($result && is_array($result)){
			foreach ($result as $key => $val) {
                                echo date('H:i:s').' 处理数据并移除',"\n";
				$_result = $redis->zRem('result_list',$key);
				
			}
		}

	} catch (Exception $e) {   
		var_dump($e->getMessage());   
	}  
	$seconds = 2;
	echo date('H:i:s').' 等待'.$seconds.'秒',"\n";
	sleep($seconds);
	echo date('H:i:s').' 循环读取',"\n";

}

 

二,测试结果

 

1,入队操作,按照优先级插入数据

chenyunhui@ubuntu:/mnt/hgfs/ShareFolder/web/click$ php test.php2016-04-22 17:11:47 数据进入队列 result_list
array(3) {
  ["test_1_17:11:47"]=>
  float(1)
  ["test_2_17:11:47"]=>
  float(2)
  ["test_3_17:11:47"]=>
  float(3)
}
17:11:47 等待1秒
2016-04-22 17:11:48 数据进入队列 result_list
array(3) {
  ["test_4_17:11:48"]=>
  float(4)
  ["test_5_17:11:48"]=>
  float(5)
  ["test_6_17:11:48"]=>
  float(6)
}
17:11:48 等待1秒
2016-04-22 17:11:49 数据进入队列 result_list
array(6) {
  ["test_4_17:11:48"]=>
  float(4)
  ["test_5_17:11:48"]=>
  float(5)
  ["test_6_17:11:48"]=>
  float(6)
  ["test_7_17:11:49"]=>
  float(7)
  ["test_8_17:11:49"]=>
  float(8)
  ["test_9_17:11:49"]=>
  float(9)
}
17:11:49 等待1秒
2016-04-22 17:11:50 数据进入队列 result_list
array(6) {
  ["test_7_17:11:49"]=>
  float(7)
  ["test_8_17:11:49"]=>
  float(8)
  ["test_9_17:11:49"]=>
  float(9)
  ["test_10_17:11:50"]=>
  float(10)
  ["test_11_17:11:50"]=>
  float(11)
  ["test_12_17:11:50"]=>
  float(12)
}
17:11:50 等待1秒
2016-04-22 17:11:51 数据进入队列 result_list
array(9) {
  ["test_1_17:11:51"]=>
  float(1)
  ["test_2_17:11:51"]=>
  float(2)
  ["test_3_17:11:51"]=>
  float(3)
  ["test_7_17:11:49"]=>
  float(7)
  ["test_8_17:11:49"]=>
  float(8)
  ["test_9_17:11:49"]=>
  float(9)
  ["test_10_17:11:50"]=>
  float(10)
  ["test_11_17:11:50"]=>
  float(11)
  ["test_12_17:11:50"]=>
  float(12)
}
17:11:51 等待1秒
2016-04-22 17:11:52 数据进入队列 result_list
array(9) {
  ["test_4_17:11:52"]=>
  float(4)
  ["test_5_17:11:52"]=>
  float(5)
  ["test_6_17:11:52"]=>
  float(6)
  ["test_7_17:11:49"]=>
  float(7)
  ["test_8_17:11:49"]=>
  float(8)
  ["test_9_17:11:49"]=>
  float(9)
  ["test_10_17:11:50"]=>
  float(10)
  ["test_11_17:11:50"]=>
  float(11)
  ["test_12_17:11:50"]=>
  float(12)
}
17:11:52 等待1秒

 

2,出队处理,安装排序优先处理1,2,3;之后是4,5,6;如果这时1,2,3插入新数据,则优先处理,处理完毕再去读取4,5,6的数据,实现不同优先级的效果

 

chenyunhui@ubuntu:/mnt/hgfs/ShareFolder/web/click$ php queue.php 
17:11:48 读取队列数据
17:11:48 读到数据为:
array(3) {
  ["test_1_17:11:47"]=>
  float(1)
  ["test_2_17:11:47"]=>
  float(2)
  ["test_3_17:11:47"]=>
  float(3)
}
17:11:48 处理数据并移除
17:11:48 等待2秒
17:11:50 循环读取
17:11:50 读取队列数据
17:11:50 读到数据为:
array(3) {
  ["test_4_17:11:48"]=>
  float(4)
  ["test_5_17:11:48"]=>
  float(5)
  ["test_6_17:11:48"]=>
  float(6)
}
17:11:50 处理数据并移除
17:11:50 等待2秒
17:11:52 循环读取
17:11:52 读取队列数据
17:11:52 读到数据为:
array(3) {
  ["test_1_17:11:51"]=>
  float(1)
  ["test_2_17:11:51"]=>
  float(2)
  ["test_3_17:11:51"]=>
  float(3)
}
17:11:52 处理数据并移除
17:11:52 等待2秒
17:11:54 循环读取
17:11:54 读取队列数据
17:11:54 读到数据为:
array(3) {
  ["test_4_17:11:52"]=>
  float(4)
  ["test_5_17:11:52"]=>
  float(5)
  ["test_6_17:11:52"]=>
  float(6)
}
17:11:54 处理数据并移除
17:11:54 等待2秒
17:11:56 循环读取




Leave a Reply

Your email address will not be published. Required fields are marked *