1,819   Redis

一,业务背景:

1,有时候数据已经排好队列了,但是突然优先级更高的数据来了,需要先处理这些优先级更高的数据

2,Redis的brPop/blPop命令可以读取多个List,而且有优先顺序;

3,Redis还有一个有序集合Sorted Set,也能达到优先顺序的效果,只是操作复杂度比brPop/blPop大,效率也稍低

 

二,具体实现:利用brPop/blPop读取多个List
1,入队操作,分别在不同时候对这两个List插入数据

while (true) {
	try {   
		$data = 'test_1_1_'.date('Y-m-d H:i:s');
		echo date('Y-m-d H:i:s').' 数据进入队列 result_list_1:'.$data,"\n";
  		$result = $redis->lPush('result_list_1', $data); 

  		$data1 = 'test_2_1_'.date('Y-m-d H:i:s');
  		$data2 = 'test_2_2_'.date('Y-m-d H:i:s');
  		$data3 = 'test_2_3_'.date('Y-m-d H:i:s');

 		echo date('Y-m-d H:i:s').' 数据进入队列 result_list_2:'.$data1,"\n";
  		$result = $redis->lPush('result_list_2', $data1,$data2,$data3); 

		$seconds = 1;
		echo date('Y-m-d H:i:s').' 等待'.$seconds.'秒',"\n";
		sleep($seconds);

  		$data = 'test_1_2_'.date('Y-m-d H:i:s');
		echo date('Y-m-d H:i:s').' 数据进入队列 result_list_1:'.$data,"\n";
  		$result = $redis->lPush('result_list_1', $data); 	

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

}

 

2,出队操作,先把result_list_1优先级高于result_list_2,优先读取result_list_1

while (true) {
	try {   
		echo date('Y-m-d H:i:s').' 读取队列数据',"\n";
		$result = $redis->brPop('result_list_1','result_list_2', 10);
		echo date('Y-m-d H:i:s').' 读到数据为:',"\n";
		var_dump($result);
	} catch (Exception $e) {   
		var_dump($e->getMessage());   
	}  
	$seconds = 1;
	echo date('Y-m-d H:i:s').' 等待'.$seconds.'秒',"\n";
	sleep($seconds);
	echo date('Y-m-d H:i:s').' 循环读取',"\n";

}

 

3,测试结果:

入队操作:

先插入1个数据到result_list_1,然后插入3个数据到result_list_2,再插入1个数据result_list_1

2015-05-28 11:36:19 数据进入队列 result_list_1:test_1_1_2015-05-28 11:36:19
2015-05-28 11:36:19 数据进入队列 result_list_2:test_2_1_2015-05-28 11:36:19
2015-05-28 11:36:19 等待1秒
2015-05-28 11:36:20 数据进入队列 result_list_1:test_1_2_2015-05-28 11:36:20
2015-05-28 11:36:20 等待5秒

出队处理:

result_list_1有1个数据,读取完毕之后,再读result_list_2中3个数据,当读取到result_list_2的第2数据时,发现优先级更高的result_list_1有数据插入了,那么就不读result_list_2的第2个数据了,而是读取result_list_1的新数据了,读取完result_list_1的所有数据后,再去result_list_2读取剩下的数据

chenyunhui@ubuntu:/mnt/hgfs/ShareFolder/web/click$ php queue.php 
2015-05-28 11:36:16 读取队列数据
2015-05-28 11:36:19 读到数据为:
array(2) {
  [0]=>
  string(13) "result_list_1"
  [1]=>
  string(28) "test_1_1_2015-05-28 11:36:19"
}
2015-05-28 11:36:19 等待1秒
2015-05-28 11:36:20 循环读取
2015-05-28 11:36:20 读取队列数据
2015-05-28 11:36:20 读到数据为:
array(2) {
  [0]=>
  string(13) "result_list_2"
  [1]=>
  string(28) "test_2_1_2015-05-28 11:36:19"
}
2015-05-28 11:36:20 等待1秒
2015-05-28 11:36:21 循环读取
2015-05-28 11:36:21 读取队列数据
2015-05-28 11:36:21 读到数据为:
array(2) {
  [0]=>
  string(13) "result_list_1"
  [1]=>
  string(28) "test_1_2_2015-05-28 11:36:20"
}
2015-05-28 11:36:21 等待1秒
2015-05-28 11:36:22 循环读取
2015-05-28 11:36:22 读取队列数据
2015-05-28 11:36:22 读到数据为:
array(2) {
  [0]=>
  string(13) "result_list_2"
  [1]=>
  string(28) "test_2_2_2015-05-28 11:36:19"
}
2015-05-28 11:36:22 等待1秒
2015-05-28 11:36:23 循环读取
2015-05-28 11:36:23 读取队列数据
2015-05-28 11:36:23 读到数据为:
array(2) {
  [0]=>
  string(13) "result_list_2"
  [1]=>
  string(28) "test_2_3_2015-05-28 11:36:19"
}
2015-05-28 11:36:23 等待1秒
2015-05-28 11:36:24 循环读取




Leave a Reply

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