dever 7 年之前
父节点
当前提交
91553a5e4f
共有 5 个文件被更改,包括 72 次插入34 次删除
  1. 11 6
      src/Data.php
  2. 10 5
      src/Db.php
  3. 27 8
      src/Queue.php
  4. 14 0
      src/QueueInterface.php
  5. 10 15
      src/Redis.php

+ 11 - 6
src/Data.php

@@ -4,17 +4,22 @@ namespace Queue\Src;
 
 use Dever;
 
-class Data
+class Data implements QueueInterface
 {
-	private $data = array();
+	private $db;
 
-	public function push($value)
+	public function push($value, $key)
 	{
-		return array_push($this->data, $value);
+		return array_push($this->db[$key], $value);
 	}
 
-	public function pop()
+	public function pop($key)
 	{
-		return array_shift($this->data);
+		return array_shift($this->db[$key]);
+	}
+
+	public function len($key)
+	{
+		return count($this->db[$key]);
 	}
 }

+ 10 - 5
src/Db.php

@@ -4,7 +4,7 @@ namespace Queue\Src;
 
 use Dever;
 
-class Db
+class Db implements QueueInterface
 {
 	private $db;
 
@@ -13,19 +13,24 @@ class Db
 		$this->db = Dever::db('queue/data');
 	}
 
-	public function push($value)
+	public function push($value, $key)
 	{
-		$data = array('add_value' => $value);
+		$data = array('add_key' => $key, 'add_value' => $value);
 		return $this->db->insert($data);
 	}
 
-	public function pop()
+	public function pop($key)
 	{
-		$data = $this->db->one();
+		$data = $this->db->one(array('option_key' => $key));
 		if ($data) {
 			$this->db->delete($data['id']);
 			return $data['value'];
 		}
 		return false;
 	}
+
+	public function len($key)
+	{
+		return $this->db->total(array('option_key' => $key));
+	}
 }

+ 27 - 8
src/Queue.php

@@ -4,25 +4,44 @@ namespace Queue\Src;
 
 use Dever;
 
-class Queue
+class Queue implements QueueInterface
 {
-	private $method;
+	private $db;
+	private $key;
 
 	public function __construct()
 	{
-		$method = 'Queue\\Src\\' . ucfirst(Dever::config('base')->queue['method']);
-		$this->method = new $method();
+		$db = 'Queue\\Src\\' . ucfirst(Dever::config('base')->queue['method']);
+		$this->db = new $db();
 	}
 
-	public function push($value)
+	private function key($key = false)
+	{
+		if (!$key) {
+			$key = DEVER_PROJECT . '_' . DEVER_APP_NAME;
+		}
+		return $key;
+	}
+
+	public function push($value, $key = false)
 	{
 		$value = json_encode($value, JSON_UNESCAPED_UNICODE);
-		$this->method->push($value);
+		$this->db->push($this->key($key), $value);
 	}
 
-	public function pop()
+	public function pop($key = false)
 	{
-		$data = $this->method->pop();
+		$data = $this->db->pop($this->key($key));
 		return json_decode($data, true);
 	}
+
+	public function len($key = false)
+	{
+		return $this->db->len($this->key($key));
+	}
+
+	public function get()
+	{
+		return $this->db;
+	}
 }

+ 14 - 0
src/QueueInterface.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace Queue\Src;
+
+use Dever;
+
+interface QueueInterface
+{
+	public function push($value, $key);
+
+	public function pop($key);
+
+	public function len($key);
+}

+ 10 - 15
src/Redis.php

@@ -4,12 +4,10 @@ namespace Queue\Src;
 
 use Dever;
 
-class Redis
+class Redis implements QueueInterface
 {
 	private $db;
 
-	private $key;
-
 	public function __construct()
 	{
 		$this->db = new \Redis;
@@ -29,25 +27,22 @@ class Redis
 		}
 	}
 
-	private function key()
-	{
-		if ($this->key) {
-			return $this->key;
-		}
-		return DEVER_PROJECT . '_' . DEVER_APP_NAME;
-	}
-
-	public function push($value)
+	public function push($value, $key)
 	{
-		return $this->db->rpush($this->key(), $value);
+		return $this->db->rpush($key, $value);
 	}
 
-	public function pop()
+	public function pop($key)
 	{
-		$data = $this->db->lpop($this->key());
+		$data = $this->db->lpop($key);
 		if ($data) {
 			return $data;
 		}
 		return false;
 	}
+
+	public function len($key)
+	{
+		return $this->db->llen($key);
+	}
 }