dever 3 years ago
parent
commit
fb9b5b5833
2 changed files with 29 additions and 10 deletions
  1. 14 4
      src/Data.php
  2. 15 6
      src/Redis.php

+ 14 - 4
src/Data.php

@@ -6,20 +6,30 @@ use Dever;
 
 class Data implements Core
 {
-	private $db;
+	private $data = array();
+
+	private function init($key)
+	{
+		if (empty($this->data[$key])) {
+			$this->data[$key] = array();
+		}
+	}
 
 	public function push($value, $key)
 	{
-		return array_push($this->db[$key], $value);
+		$this->init($key);
+		return array_push($this->data[$key], $value);
 	}
 
 	public function pop($key)
 	{
-		return array_shift($this->db[$key]);
+		$this->init($key);
+		return array_shift($this->data[$key]);
 	}
 
 	public function len($key)
 	{
-		return count($this->db[$key]);
+		$this->init($key);
+		return count($this->data[$key]);
 	}
 }

+ 15 - 6
src/Redis.php

@@ -25,16 +25,25 @@ class Redis implements Core
 
 	public function push($value, $key)
 	{
-		return $this->db->rpush($key, $value);
+		return $this->db->lpush($key, $value);
 	}
 
 	public function pop($key)
 	{
-		$data = $this->db->lpop($key);
-		if ($data) {
-			return $data;
-		}
-		return false;
+		$data = $this->db->brPop($key, 10);
+        if ($data) {
+            if (isset($data[1])) {
+                $data = $data[1];
+            }
+            return $data;
+        } else {
+            $pong = $this->db->ping();
+            if ($pong != '+PONG') {
+                throw new \Exception('Redis ping failure!', 500);
+            }
+            usleep(100000);
+        }
+        return false;
 	}
 
 	public function len($key)