Explorar o código

新增(redis)队列操作代码示例

Sven dunn %!s(int64=2) %!d(string=hai) anos
pai
achega
268161098d

+ 38 - 0
其它规范示例/队列示例/QueueHelp/Queue.php

@@ -0,0 +1,38 @@
+<?php
+/**
+ * for queue operation
+ */
+namespace app\common\util\QueueHelp;
+
+use think\facade\Log;
+
+class Queue{
+	
+    function __construct($config)
+    {
+		//暂时只支持redis一种,之后增加其它方式改为工厂模式
+		$this->redisQueue 		= new RedisQueue($config);
+    }
+	
+	
+    public function push($queue, $obj, $type='redis')
+    {
+        $result =  $this->redisQueue->push($queue, $obj);
+		log::info('[INFO] Queue model push to  '.$type.' '.json_encode($obj).' result:'.json_encode($result));
+        return $result;
+    }	
+	
+    public function queueSize($queue, $type='redis')
+    {
+        $result =  $this->redisQueue->queueSize($queue);
+        log::info('[INFO] Queue size: '.$queue.' '.json_encode($result));
+        return $result;
+    }
+	
+    public function pull($queue, $type='redis')
+    {
+        $result =  $this->redisQueue->pull($queue);
+		log::info('[INFO] Queue model pull data result:'.json_encode($result));
+        return $result;
+    }
+}

+ 66 - 0
其它规范示例/队列示例/QueueHelp/RedisQueue.php

@@ -0,0 +1,66 @@
+<?php
+/**
+ * for redis implement
+ */
+namespace app\common\util\QueueHelp;
+
+class RedisQueue  {
+
+	public function __construct($config) {
+		$this->redis = new \Redis();
+        $this->redis->connect(env('redis.hostname'), env('redis.hostport'));
+        $this->redis->auth(env('redis.password'));				   
+        $this->redis->select($config['db']);				   
+	}	
+
+    function push($queue, $obj)
+    {
+        if (!$queue || !is_object($obj)) {
+            return array('ack'=>false, 'msg'=>'parameter error');
+        }
+        if ($this->redis) {
+            $ack = $this->redis->rpush($queue, json_encode($obj));
+            $ack = ($ack>0)?true:false;
+            $msg = ($ack)?'ok':'redis push fail';
+        } else {
+            $ack = false;
+            $msg = 'redis not support';
+        }
+        return array('ack'=>$ack, 'msg'=>$msg);
+    }	
+	
+    function queueSize($queue)
+    {
+        if (!$queue){
+            return array('ack'=>false, 'msg'=>'parameter error');
+        }
+        if ($this->redis){
+            $size = $this->redis->llen($queue);
+            $ack = true;
+            $msg = 'ok';
+        }else{
+            $ack = false;
+            $msg = 'redis not support';
+            $size = -1;
+        }
+        return array('ack'=>$ack, 'msg'=>$msg, 'data'=>$size);
+    }
+	
+    function pull($queue)
+    {
+		$req 	= new \stdClass();
+        if ($this->redis) {
+			$ack = false;
+            $data = $this->redis->lpop($queue);
+			if ($data) {
+				$ack = true;
+			}
+			$req = json_decode($data);
+            $msg = ($ack)?'ok':'redis pull fail';
+        } else {
+            $ack = false;
+            $msg = 'redis not support';
+        }
+        return array('ack'=>$ack, 'msg'=>$msg, 'data'=>$req);
+    }		
+}

+ 12 - 0
其它规范示例/队列示例/QueueHelp/TestMq.php

@@ -0,0 +1,12 @@
+<?php
+/**
+ * 测试数据队列
+ */
+namespace app\common\util\QueueHelp;
+ 
+class TestMq {
+	
+	var $test_id 	= '';   // 测试id
+	var $test_name 	= '';   // 测试名
+	var $test_type 	= '';   // 测试类型
+}

+ 36 - 0
其它规范示例/队列示例/c层测试类.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace app\index\controller;
+
+use app\common\util\QueueHelp\Queue;
+use app\common\util\QueueHelp\TestMq;
+
+class Test extends Base
+{
+
+	private $queue_name;//队列名
+    public function __construct() 
+    {
+		parent::__construct();
+		$this->queue_name 	= 'TestMq';
+    }
+
+	//测试队列放入与拿出
+	public function test_queue_push_pull(){
+		$config['db'] 			= 1;//队列数据库
+		$Queue                  = new Queue($config);
+		$TestMq                	= new TestMq;
+		//MQ的字段可以自己设,需要在mq文件中对应修改
+		$TestMq->test_id        = 123;
+		$TestMq->test_name    	= 'tester';
+		$TestMq->test_type    	= 0;
+		$result 	= $Queue->push($this->queue_name, $TestMq);
+		//推送队列结果
+		var_dump($result);
+		
+		$res 	= $Queue->pull($this->queue_name);
+		//从队列拿取数据结果
+		var_dump($res);		
+	}	
+	
+}