-
Notifications
You must be signed in to change notification settings - Fork 0
/
miniRedis.php
65 lines (65 loc) · 1.37 KB
/
miniRedis.php
1
<?phpclass miniRedis { private $count; private $socket; public function __construct($hostname = 'localhost', $port = 6379) { $this->count = 0; $this->socket = fsockopen($hostname, $port); } public function write($s) { fwrite($this->socket, $s . "\r\n"); return $this->count++; } public function read() { $s = ''; $r = array(); $bulk = false; $multibulk = 0; while ($this->count) { $s .= fread($this->socket, 1024); while (strpos($s, "\r\n") !== false) { $a = substr($s, 0, strpos($s, "\r\n")); $s = substr($s, strpos($s, "\r\n") + 2); if ($bulk === false) switch ($a[0]) { case '+': $r[] = substr($a, 1); $this->count--; break; case '-': $r[] = substr($a, 1); $this->count--; break; case ':': $r[] = intval(substr($a, 1)); $this->count--; break; case '$': $bulk = intval(substr($a, 1)); if ($bulk != -1) $a = false; break; case '*': $multibulk = intval(substr($a, 1)); $t = array(); break; } if ($bulk !== false && $a !== false) { if ($bulk == -1) $a = null; $bulk = false; if ($multibulk) { $t[] = $a; $multibulk--; if (!$multibulk) { $r[] = $t; $this->count--; } } else { $r[] = $a; $this->count--; } } } } return $r; }}