@@ -31,6 +31,9 @@ |
||
| 31 | 31 | parent::connect(); |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | + /** |
|
| 35 | + * @param string $direction |
|
| 36 | + */ |
|
| 34 | 37 | private function storeDebug(CommandInterface $command, $direction) |
| 35 | 38 | { |
| 36 | 39 | $firtsArg = $command->getArgument(0); |
@@ -50,7 +50,7 @@ |
||
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | 52 | * @param mixed $parameters Connection parameters for one or more servers. |
| 53 | - * @param mixed $options Options to configure some behaviours of the client. |
|
| 53 | + * @param OptionsInterface $options Options to configure some behaviours of the client. |
|
| 54 | 54 | */ |
| 55 | 55 | public function __construct($parameters = null, $options = null) |
| 56 | 56 | { |
@@ -155,7 +155,7 @@ |
||
| 155 | 155 | */ |
| 156 | 156 | protected function getConnectionInitializerWrapper($callable) |
| 157 | 157 | { |
| 158 | - return function () use ($callable) { |
|
| 158 | + return function() use ($callable) { |
|
| 159 | 159 | $connection = call_user_func_array($callable, func_get_args()); |
| 160 | 160 | |
| 161 | 161 | if (!$connection instanceof ConnectionInterface) { |
@@ -169,7 +169,7 @@ |
||
| 169 | 169 | /** |
| 170 | 170 | * Returns the list of IDs for the supported commands. |
| 171 | 171 | * |
| 172 | - * @return array |
|
| 172 | + * @return string[] |
|
| 173 | 173 | */ |
| 174 | 174 | public function getSupportedCommands() |
| 175 | 175 | { |
@@ -42,7 +42,7 @@ |
||
| 42 | 42 | /** |
| 43 | 43 | * Returns the processed response to PUBSUB NUMSUB. |
| 44 | 44 | * |
| 45 | - * @param array $channels List of channels |
|
| 45 | + * @param string $channels List of channels |
|
| 46 | 46 | * |
| 47 | 47 | * @return array |
| 48 | 48 | */ |
@@ -43,7 +43,7 @@ |
||
| 43 | 43 | /** |
| 44 | 44 | * Returns a processed response to SENTINEL MASTERS or SENTINEL SLAVES. |
| 45 | 45 | * |
| 46 | - * @param array $servers List of Redis servers. |
|
| 46 | + * @param string $servers List of Redis servers. |
|
| 47 | 47 | * |
| 48 | 48 | * @return array |
| 49 | 49 | */ |
@@ -59,6 +59,7 @@ |
||
| 59 | 59 | * Subscribes to the specified channels. |
| 60 | 60 | * |
| 61 | 61 | * @param mixed $channel,... One or more channel names. |
| 62 | + * @param string $channel |
|
| 62 | 63 | */ |
| 63 | 64 | public function subscribe($channel /*, ... */) |
| 64 | 65 | { |
@@ -28,9 +28,9 @@ discard block |
||
| 28 | 28 | const PMESSAGE = 'pmessage'; |
| 29 | 29 | const PONG = 'pong'; |
| 30 | 30 | |
| 31 | - const STATUS_VALID = 1; // 0b0001 |
|
| 32 | - const STATUS_SUBSCRIBED = 2; // 0b0010 |
|
| 33 | - const STATUS_PSUBSCRIBED = 4; // 0b0100 |
|
| 31 | + const STATUS_VALID = 1; // 0b0001 |
|
| 32 | + const STATUS_SUBSCRIBED = 2; // 0b0010 |
|
| 33 | + const STATUS_PSUBSCRIBED = 4; // 0b0100 |
|
| 34 | 34 | |
| 35 | 35 | private $position = null; |
| 36 | 36 | private $statusFlags = self::STATUS_VALID; |
@@ -208,7 +208,7 @@ discard block |
||
| 208 | 208 | */ |
| 209 | 209 | protected function invalidate() |
| 210 | 210 | { |
| 211 | - $this->statusFlags = 0; // 0b0000; |
|
| 211 | + $this->statusFlags = 0; // 0b0000; |
|
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | /** |
@@ -19,6 +19,9 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | class SimpleSSDB extends SSDB |
| 21 | 21 | { |
| 22 | + /** |
|
| 23 | + * @param integer $port |
|
| 24 | + */ |
|
| 22 | 25 | function __construct($host, $port, $timeout_ms=2000){ |
| 23 | 26 | parent::__construct($host, $port, $timeout_ms); |
| 24 | 27 | $this->easy(); |
@@ -174,6 +177,10 @@ discard block |
||
| 174 | 177 | $resp = $this->check_easy_resp($cmd, $resp); |
| 175 | 178 | return $resp; |
| 176 | 179 | } |
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * @param SSDB_Response $resp |
|
| 183 | + */ |
|
| 177 | 184 | private function check_easy_resp($cmd, $resp){ |
| 178 | 185 | $this->last_resp = $resp; |
| 179 | 186 | if($this->_easy){ |
@@ -19,558 +19,558 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class SimpleSSDB extends SSDB |
| 21 | 21 | { |
| 22 | - function __construct($host, $port, $timeout_ms=2000){ |
|
| 23 | - parent::__construct($host, $port, $timeout_ms); |
|
| 24 | - $this->easy(); |
|
| 25 | - } |
|
| 22 | + function __construct($host, $port, $timeout_ms=2000){ |
|
| 23 | + parent::__construct($host, $port, $timeout_ms); |
|
| 24 | + $this->easy(); |
|
| 25 | + } |
|
| 26 | 26 | } |
| 27 | 27 | class SSDB_Response |
| 28 | 28 | { |
| 29 | - public $cmd; |
|
| 30 | - public $code; |
|
| 31 | - public $data = null; |
|
| 32 | - public $message; |
|
| 33 | - function __construct($code='ok', $data_or_message=null){ |
|
| 34 | - $this->code = $code; |
|
| 35 | - if($code == 'ok'){ |
|
| 36 | - $this->data = $data_or_message; |
|
| 37 | - }else{ |
|
| 38 | - $this->message = $data_or_message; |
|
| 39 | - } |
|
| 40 | - } |
|
| 41 | - function __toString(){ |
|
| 42 | - if($this->code == 'ok'){ |
|
| 43 | - $s = $this->data === null? '' : json_encode($this->data); |
|
| 44 | - }else{ |
|
| 45 | - $s = $this->message; |
|
| 46 | - } |
|
| 47 | - return sprintf('%-13s %12s %s', $this->cmd, $this->code, $s); |
|
| 48 | - } |
|
| 49 | - function ok(){ |
|
| 50 | - return $this->code == 'ok'; |
|
| 51 | - } |
|
| 52 | - function not_found(){ |
|
| 53 | - return $this->code == 'not_found'; |
|
| 54 | - } |
|
| 29 | + public $cmd; |
|
| 30 | + public $code; |
|
| 31 | + public $data = null; |
|
| 32 | + public $message; |
|
| 33 | + function __construct($code='ok', $data_or_message=null){ |
|
| 34 | + $this->code = $code; |
|
| 35 | + if($code == 'ok'){ |
|
| 36 | + $this->data = $data_or_message; |
|
| 37 | + }else{ |
|
| 38 | + $this->message = $data_or_message; |
|
| 39 | + } |
|
| 40 | + } |
|
| 41 | + function __toString(){ |
|
| 42 | + if($this->code == 'ok'){ |
|
| 43 | + $s = $this->data === null? '' : json_encode($this->data); |
|
| 44 | + }else{ |
|
| 45 | + $s = $this->message; |
|
| 46 | + } |
|
| 47 | + return sprintf('%-13s %12s %s', $this->cmd, $this->code, $s); |
|
| 48 | + } |
|
| 49 | + function ok(){ |
|
| 50 | + return $this->code == 'ok'; |
|
| 51 | + } |
|
| 52 | + function not_found(){ |
|
| 53 | + return $this->code == 'not_found'; |
|
| 54 | + } |
|
| 55 | 55 | } |
| 56 | 56 | class SSDB |
| 57 | 57 | { |
| 58 | - private $debug = false; |
|
| 59 | - public $sock = null; |
|
| 60 | - private $_closed = false; |
|
| 61 | - private $recv_buf = ''; |
|
| 62 | - private $_easy = false; |
|
| 63 | - public $last_resp = null; |
|
| 64 | - function __construct($host, $port, $timeout_ms=2000){ |
|
| 65 | - $timeout_f = (float)$timeout_ms/1000; |
|
| 66 | - $this->sock = @stream_socket_client("$host:$port", $errno, $errstr, $timeout_f); |
|
| 67 | - if(!$this->sock){ |
|
| 68 | - throw new SSDBException("$errno: $errstr"); |
|
| 69 | - } |
|
| 70 | - $timeout_sec = intval($timeout_ms/1000); |
|
| 71 | - $timeout_usec = ($timeout_ms - $timeout_sec * 1000) * 1000; |
|
| 72 | - @stream_set_timeout($this->sock, $timeout_sec, $timeout_usec); |
|
| 73 | - if(function_exists('stream_set_chunk_size')){ |
|
| 74 | - @stream_set_chunk_size($this->sock, 1024 * 1024); |
|
| 75 | - } |
|
| 76 | - } |
|
| 58 | + private $debug = false; |
|
| 59 | + public $sock = null; |
|
| 60 | + private $_closed = false; |
|
| 61 | + private $recv_buf = ''; |
|
| 62 | + private $_easy = false; |
|
| 63 | + public $last_resp = null; |
|
| 64 | + function __construct($host, $port, $timeout_ms=2000){ |
|
| 65 | + $timeout_f = (float)$timeout_ms/1000; |
|
| 66 | + $this->sock = @stream_socket_client("$host:$port", $errno, $errstr, $timeout_f); |
|
| 67 | + if(!$this->sock){ |
|
| 68 | + throw new SSDBException("$errno: $errstr"); |
|
| 69 | + } |
|
| 70 | + $timeout_sec = intval($timeout_ms/1000); |
|
| 71 | + $timeout_usec = ($timeout_ms - $timeout_sec * 1000) * 1000; |
|
| 72 | + @stream_set_timeout($this->sock, $timeout_sec, $timeout_usec); |
|
| 73 | + if(function_exists('stream_set_chunk_size')){ |
|
| 74 | + @stream_set_chunk_size($this->sock, 1024 * 1024); |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - function set_timeout($timeout_ms){ |
|
| 79 | - $timeout_sec = intval($timeout_ms/1000); |
|
| 80 | - $timeout_usec = ($timeout_ms - $timeout_sec * 1000) * 1000; |
|
| 81 | - @stream_set_timeout($this->sock, $timeout_sec, $timeout_usec); |
|
| 82 | - } |
|
| 78 | + function set_timeout($timeout_ms){ |
|
| 79 | + $timeout_sec = intval($timeout_ms/1000); |
|
| 80 | + $timeout_usec = ($timeout_ms - $timeout_sec * 1000) * 1000; |
|
| 81 | + @stream_set_timeout($this->sock, $timeout_sec, $timeout_usec); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * After this method invoked with yesno=true, all requesting methods |
|
| 86 | - * will not return a SSDB_Response object. |
|
| 87 | - * And some certain methods like get/zget will return false |
|
| 88 | - * when response is not ok(not_found, etc) |
|
| 89 | - */ |
|
| 90 | - function easy(){ |
|
| 91 | - $this->_easy = true; |
|
| 92 | - } |
|
| 93 | - function close(){ |
|
| 94 | - if(!$this->_closed){ |
|
| 95 | - @fclose($this->sock); |
|
| 96 | - $this->_closed = true; |
|
| 97 | - $this->sock = null; |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - function closed(){ |
|
| 101 | - return $this->_closed; |
|
| 102 | - } |
|
| 103 | - private $batch_mode = false; |
|
| 104 | - private $batch_cmds = array(); |
|
| 105 | - function batch(){ |
|
| 106 | - $this->batch_mode = true; |
|
| 107 | - $this->batch_cmds = array(); |
|
| 108 | - return $this; |
|
| 109 | - } |
|
| 110 | - function multi(){ |
|
| 111 | - return $this->batch(); |
|
| 112 | - } |
|
| 113 | - function exec(){ |
|
| 114 | - $ret = array(); |
|
| 115 | - foreach($this->batch_cmds as $op){ |
|
| 116 | - list($cmd, $params) = $op; |
|
| 117 | - $this->send_req($cmd, $params); |
|
| 118 | - } |
|
| 119 | - foreach($this->batch_cmds as $op){ |
|
| 120 | - list($cmd, $params) = $op; |
|
| 121 | - $resp = $this->recv_resp($cmd, $params); |
|
| 122 | - $resp = $this->check_easy_resp($cmd, $resp); |
|
| 123 | - $ret[] = $resp; |
|
| 124 | - } |
|
| 125 | - $this->batch_mode = false; |
|
| 126 | - $this->batch_cmds = array(); |
|
| 127 | - return $ret; |
|
| 128 | - } |
|
| 84 | + /** |
|
| 85 | + * After this method invoked with yesno=true, all requesting methods |
|
| 86 | + * will not return a SSDB_Response object. |
|
| 87 | + * And some certain methods like get/zget will return false |
|
| 88 | + * when response is not ok(not_found, etc) |
|
| 89 | + */ |
|
| 90 | + function easy(){ |
|
| 91 | + $this->_easy = true; |
|
| 92 | + } |
|
| 93 | + function close(){ |
|
| 94 | + if(!$this->_closed){ |
|
| 95 | + @fclose($this->sock); |
|
| 96 | + $this->_closed = true; |
|
| 97 | + $this->sock = null; |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + function closed(){ |
|
| 101 | + return $this->_closed; |
|
| 102 | + } |
|
| 103 | + private $batch_mode = false; |
|
| 104 | + private $batch_cmds = array(); |
|
| 105 | + function batch(){ |
|
| 106 | + $this->batch_mode = true; |
|
| 107 | + $this->batch_cmds = array(); |
|
| 108 | + return $this; |
|
| 109 | + } |
|
| 110 | + function multi(){ |
|
| 111 | + return $this->batch(); |
|
| 112 | + } |
|
| 113 | + function exec(){ |
|
| 114 | + $ret = array(); |
|
| 115 | + foreach($this->batch_cmds as $op){ |
|
| 116 | + list($cmd, $params) = $op; |
|
| 117 | + $this->send_req($cmd, $params); |
|
| 118 | + } |
|
| 119 | + foreach($this->batch_cmds as $op){ |
|
| 120 | + list($cmd, $params) = $op; |
|
| 121 | + $resp = $this->recv_resp($cmd, $params); |
|
| 122 | + $resp = $this->check_easy_resp($cmd, $resp); |
|
| 123 | + $ret[] = $resp; |
|
| 124 | + } |
|
| 125 | + $this->batch_mode = false; |
|
| 126 | + $this->batch_cmds = array(); |
|
| 127 | + return $ret; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - function request(){ |
|
| 131 | - $args = func_get_args(); |
|
| 132 | - $cmd = array_shift($args); |
|
| 133 | - return $this->__call($cmd, $args); |
|
| 134 | - } |
|
| 130 | + function request(){ |
|
| 131 | + $args = func_get_args(); |
|
| 132 | + $cmd = array_shift($args); |
|
| 133 | + return $this->__call($cmd, $args); |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - private $async_auth_password = null; |
|
| 136 | + private $async_auth_password = null; |
|
| 137 | 137 | |
| 138 | - function auth($password){ |
|
| 139 | - $this->async_auth_password = $password; |
|
| 140 | - return null; |
|
| 141 | - } |
|
| 142 | - function __call($cmd, $params=array()){ |
|
| 143 | - $cmd = strtolower($cmd); |
|
| 144 | - if($this->async_auth_password !== null){ |
|
| 145 | - $pass = $this->async_auth_password; |
|
| 146 | - $this->async_auth_password = null; |
|
| 147 | - $auth = $this->__call('auth', array($pass)); |
|
| 148 | - if($auth !== true){ |
|
| 149 | - throw new Exception("Authentication failed"); |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - if($this->batch_mode){ |
|
| 153 | - $this->batch_cmds[] = array($cmd, $params); |
|
| 154 | - return $this; |
|
| 155 | - } |
|
| 156 | - try{ |
|
| 157 | - if($this->send_req($cmd, $params) === false){ |
|
| 158 | - $resp = new SSDB_Response('error', 'send error'); |
|
| 159 | - }else{ |
|
| 160 | - $resp = $this->recv_resp($cmd, $params); |
|
| 161 | - } |
|
| 162 | - }catch(SSDBException $e){ |
|
| 163 | - if($this->_easy){ |
|
| 164 | - throw $e; |
|
| 165 | - }else{ |
|
| 166 | - $resp = new SSDB_Response('error', $e->getMessage()); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - if($resp->code == 'noauth'){ |
|
| 170 | - $msg = $resp->message; |
|
| 171 | - throw new Exception($msg); |
|
| 172 | - } |
|
| 138 | + function auth($password){ |
|
| 139 | + $this->async_auth_password = $password; |
|
| 140 | + return null; |
|
| 141 | + } |
|
| 142 | + function __call($cmd, $params=array()){ |
|
| 143 | + $cmd = strtolower($cmd); |
|
| 144 | + if($this->async_auth_password !== null){ |
|
| 145 | + $pass = $this->async_auth_password; |
|
| 146 | + $this->async_auth_password = null; |
|
| 147 | + $auth = $this->__call('auth', array($pass)); |
|
| 148 | + if($auth !== true){ |
|
| 149 | + throw new Exception("Authentication failed"); |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + if($this->batch_mode){ |
|
| 153 | + $this->batch_cmds[] = array($cmd, $params); |
|
| 154 | + return $this; |
|
| 155 | + } |
|
| 156 | + try{ |
|
| 157 | + if($this->send_req($cmd, $params) === false){ |
|
| 158 | + $resp = new SSDB_Response('error', 'send error'); |
|
| 159 | + }else{ |
|
| 160 | + $resp = $this->recv_resp($cmd, $params); |
|
| 161 | + } |
|
| 162 | + }catch(SSDBException $e){ |
|
| 163 | + if($this->_easy){ |
|
| 164 | + throw $e; |
|
| 165 | + }else{ |
|
| 166 | + $resp = new SSDB_Response('error', $e->getMessage()); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + if($resp->code == 'noauth'){ |
|
| 170 | + $msg = $resp->message; |
|
| 171 | + throw new Exception($msg); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - $resp = $this->check_easy_resp($cmd, $resp); |
|
| 175 | - return $resp; |
|
| 176 | - } |
|
| 177 | - private function check_easy_resp($cmd, $resp){ |
|
| 178 | - $this->last_resp = $resp; |
|
| 179 | - if($this->_easy){ |
|
| 180 | - if($resp->not_found()){ |
|
| 181 | - return NULL; |
|
| 182 | - }else if(!$resp->ok() && !is_array($resp->data)){ |
|
| 183 | - return false; |
|
| 184 | - }else{ |
|
| 185 | - return $resp->data; |
|
| 186 | - } |
|
| 187 | - }else{ |
|
| 188 | - $resp->cmd = $cmd; |
|
| 189 | - return $resp; |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - function multi_set($kvs=array()){ |
|
| 193 | - $args = array(); |
|
| 194 | - foreach($kvs as $k=>$v){ |
|
| 195 | - $args[] = $k; |
|
| 196 | - $args[] = $v; |
|
| 197 | - } |
|
| 198 | - return $this->__call(__FUNCTION__, $args); |
|
| 199 | - } |
|
| 200 | - function multi_hset($name, $kvs=array()){ |
|
| 201 | - $args = array($name); |
|
| 202 | - foreach($kvs as $k=>$v){ |
|
| 203 | - $args[] = $k; |
|
| 204 | - $args[] = $v; |
|
| 205 | - } |
|
| 206 | - return $this->__call(__FUNCTION__, $args); |
|
| 207 | - } |
|
| 208 | - function multi_zset($name, $kvs=array()){ |
|
| 209 | - $args = array($name); |
|
| 210 | - foreach($kvs as $k=>$v){ |
|
| 211 | - $args[] = $k; |
|
| 212 | - $args[] = $v; |
|
| 213 | - } |
|
| 214 | - return $this->__call(__FUNCTION__, $args); |
|
| 215 | - } |
|
| 216 | - function incr($key, $val=1){ |
|
| 217 | - $args = func_get_args(); |
|
| 218 | - return $this->__call(__FUNCTION__, $args); |
|
| 219 | - } |
|
| 220 | - function decr($key, $val=1){ |
|
| 221 | - $args = func_get_args(); |
|
| 222 | - return $this->__call(__FUNCTION__, $args); |
|
| 223 | - } |
|
| 224 | - function zincr($name, $key, $score=1){ |
|
| 225 | - $args = func_get_args(); |
|
| 226 | - return $this->__call(__FUNCTION__, $args); |
|
| 227 | - } |
|
| 228 | - function zdecr($name, $key, $score=1){ |
|
| 229 | - $args = func_get_args(); |
|
| 230 | - return $this->__call(__FUNCTION__, $args); |
|
| 231 | - } |
|
| 232 | - function zadd($key, $score, $value){ |
|
| 233 | - $args = array($key, $value, $score); |
|
| 234 | - return $this->__call('zset', $args); |
|
| 235 | - } |
|
| 236 | - function zRevRank($name, $key){ |
|
| 237 | - $args = func_get_args(); |
|
| 238 | - return $this->__call("zrrank", $args); |
|
| 239 | - } |
|
| 240 | - function zRevRange($name, $offset, $limit){ |
|
| 241 | - $args = func_get_args(); |
|
| 242 | - return $this->__call("zrrange", $args); |
|
| 243 | - } |
|
| 244 | - function hincr($name, $key, $val=1){ |
|
| 245 | - $args = func_get_args(); |
|
| 246 | - return $this->__call(__FUNCTION__, $args); |
|
| 247 | - } |
|
| 248 | - function hdecr($name, $key, $val=1){ |
|
| 249 | - $args = func_get_args(); |
|
| 250 | - return $this->__call(__FUNCTION__, $args); |
|
| 251 | - } |
|
| 252 | - private function send_req($cmd, $params){ |
|
| 253 | - $req = array($cmd); |
|
| 254 | - foreach($params as $p){ |
|
| 255 | - if(is_array($p)){ |
|
| 256 | - $req = array_merge($req, $p); |
|
| 257 | - }else{ |
|
| 258 | - $req[] = $p; |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - return $this->send($req); |
|
| 262 | - } |
|
| 263 | - private function recv_resp($cmd, $params){ |
|
| 264 | - $resp = $this->recv(); |
|
| 265 | - if($resp === false){ |
|
| 266 | - return new SSDB_Response('error', 'Unknown error'); |
|
| 267 | - }else if(!$resp){ |
|
| 268 | - return new SSDB_Response('disconnected', 'Connection closed'); |
|
| 269 | - } |
|
| 270 | - if($resp[0] == 'noauth'){ |
|
| 271 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 272 | - return new SSDB_Response($resp[0], $errmsg); |
|
| 273 | - } |
|
| 274 | - switch($cmd){ |
|
| 275 | - case 'dbsize': |
|
| 276 | - case 'ping': |
|
| 277 | - case 'qset': |
|
| 278 | - case 'getbit': |
|
| 279 | - case 'setbit': |
|
| 280 | - case 'countbit': |
|
| 281 | - case 'strlen': |
|
| 282 | - case 'set': |
|
| 283 | - case 'setx': |
|
| 284 | - case 'setnx': |
|
| 285 | - case 'zset': |
|
| 286 | - case 'hset': |
|
| 287 | - case 'qpush': |
|
| 288 | - case 'qpush_front': |
|
| 289 | - case 'qpush_back': |
|
| 290 | - case 'qtrim_front': |
|
| 291 | - case 'qtrim_back': |
|
| 292 | - case 'del': |
|
| 293 | - case 'zdel': |
|
| 294 | - case 'hdel': |
|
| 295 | - case 'hsize': |
|
| 296 | - case 'zsize': |
|
| 297 | - case 'qsize': |
|
| 298 | - case 'hclear': |
|
| 299 | - case 'zclear': |
|
| 300 | - case 'qclear': |
|
| 301 | - case 'multi_set': |
|
| 302 | - case 'multi_del': |
|
| 303 | - case 'multi_hset': |
|
| 304 | - case 'multi_hdel': |
|
| 305 | - case 'multi_zset': |
|
| 306 | - case 'multi_zdel': |
|
| 307 | - case 'incr': |
|
| 308 | - case 'decr': |
|
| 309 | - case 'zincr': |
|
| 310 | - case 'zdecr': |
|
| 311 | - case 'hincr': |
|
| 312 | - case 'hdecr': |
|
| 313 | - case 'zget': |
|
| 314 | - case 'zrank': |
|
| 315 | - case 'zrrank': |
|
| 316 | - case 'zcount': |
|
| 317 | - case 'zsum': |
|
| 318 | - case 'zremrangebyrank': |
|
| 319 | - case 'zremrangebyscore': |
|
| 320 | - case 'ttl': |
|
| 321 | - case 'expire': |
|
| 322 | - if($resp[0] == 'ok'){ |
|
| 323 | - $val = isset($resp[1])? intval($resp[1]) : 0; |
|
| 324 | - return new SSDB_Response($resp[0], $val); |
|
| 325 | - }else{ |
|
| 326 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 327 | - return new SSDB_Response($resp[0], $errmsg); |
|
| 328 | - } |
|
| 329 | - case 'zavg': |
|
| 330 | - if($resp[0] == 'ok'){ |
|
| 331 | - $val = isset($resp[1])? floatval($resp[1]) : (float)0; |
|
| 332 | - return new SSDB_Response($resp[0], $val); |
|
| 333 | - }else{ |
|
| 334 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 335 | - return new SSDB_Response($resp[0], $errmsg); |
|
| 336 | - } |
|
| 337 | - case 'get': |
|
| 338 | - case 'substr': |
|
| 339 | - case 'getset': |
|
| 340 | - case 'hget': |
|
| 341 | - case 'qget': |
|
| 342 | - case 'qfront': |
|
| 343 | - case 'qback': |
|
| 344 | - if($resp[0] == 'ok'){ |
|
| 345 | - if(count($resp) == 2){ |
|
| 346 | - return new SSDB_Response('ok', $resp[1]); |
|
| 347 | - }else{ |
|
| 348 | - return new SSDB_Response('server_error', 'Invalid response'); |
|
| 349 | - } |
|
| 350 | - }else{ |
|
| 351 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 352 | - return new SSDB_Response($resp[0], $errmsg); |
|
| 353 | - } |
|
| 354 | - break; |
|
| 355 | - case 'qpop': |
|
| 356 | - case 'qpop_front': |
|
| 357 | - case 'qpop_back': |
|
| 358 | - if($resp[0] == 'ok'){ |
|
| 359 | - $size = 1; |
|
| 360 | - if(isset($params[1])){ |
|
| 361 | - $size = intval($params[1]); |
|
| 362 | - } |
|
| 363 | - if($size <= 1){ |
|
| 364 | - if(count($resp) == 2){ |
|
| 365 | - return new SSDB_Response('ok', $resp[1]); |
|
| 366 | - }else{ |
|
| 367 | - return new SSDB_Response('server_error', 'Invalid response'); |
|
| 368 | - } |
|
| 369 | - }else{ |
|
| 370 | - $data = array_slice($resp, 1); |
|
| 371 | - return new SSDB_Response('ok', $data); |
|
| 372 | - } |
|
| 373 | - }else{ |
|
| 374 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 375 | - return new SSDB_Response($resp[0], $errmsg); |
|
| 376 | - } |
|
| 377 | - break; |
|
| 378 | - case 'keys': |
|
| 379 | - case 'zkeys': |
|
| 380 | - case 'hkeys': |
|
| 381 | - case 'hlist': |
|
| 382 | - case 'zlist': |
|
| 383 | - case 'qslice': |
|
| 384 | - if($resp[0] == 'ok'){ |
|
| 385 | - $data = array(); |
|
| 386 | - if($resp[0] == 'ok'){ |
|
| 387 | - $data = array_slice($resp, 1); |
|
| 388 | - } |
|
| 389 | - return new SSDB_Response($resp[0], $data); |
|
| 390 | - }else{ |
|
| 391 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 392 | - return new SSDB_Response($resp[0], $errmsg); |
|
| 393 | - } |
|
| 394 | - case 'auth': |
|
| 395 | - case 'exists': |
|
| 396 | - case 'hexists': |
|
| 397 | - case 'zexists': |
|
| 398 | - if($resp[0] == 'ok'){ |
|
| 399 | - if(count($resp) == 2){ |
|
| 400 | - return new SSDB_Response('ok', (bool)$resp[1]); |
|
| 401 | - }else{ |
|
| 402 | - return new SSDB_Response('server_error', 'Invalid response'); |
|
| 403 | - } |
|
| 404 | - }else{ |
|
| 405 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 406 | - return new SSDB_Response($resp[0], $errmsg); |
|
| 407 | - } |
|
| 408 | - break; |
|
| 409 | - case 'multi_exists': |
|
| 410 | - case 'multi_hexists': |
|
| 411 | - case 'multi_zexists': |
|
| 412 | - if($resp[0] == 'ok'){ |
|
| 413 | - if(count($resp) % 2 == 1){ |
|
| 414 | - $data = array(); |
|
| 415 | - for($i=1; $i<count($resp); $i+=2){ |
|
| 416 | - $data[$resp[$i]] = (bool)$resp[$i + 1]; |
|
| 417 | - } |
|
| 418 | - return new SSDB_Response('ok', $data); |
|
| 419 | - }else{ |
|
| 420 | - return new SSDB_Response('server_error', 'Invalid response'); |
|
| 421 | - } |
|
| 422 | - }else{ |
|
| 423 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 424 | - return new SSDB_Response($resp[0], $errmsg); |
|
| 425 | - } |
|
| 426 | - break; |
|
| 427 | - case 'scan': |
|
| 428 | - case 'rscan': |
|
| 429 | - case 'zscan': |
|
| 430 | - case 'zrscan': |
|
| 431 | - case 'zrange': |
|
| 432 | - case 'zrrange': |
|
| 433 | - case 'hscan': |
|
| 434 | - case 'hrscan': |
|
| 435 | - case 'hgetall': |
|
| 436 | - case 'multi_hsize': |
|
| 437 | - case 'multi_zsize': |
|
| 438 | - case 'multi_get': |
|
| 439 | - case 'multi_hget': |
|
| 440 | - case 'multi_zget': |
|
| 441 | - case 'zpop_front': |
|
| 442 | - case 'zpop_back': |
|
| 443 | - if($resp[0] == 'ok'){ |
|
| 444 | - if(count($resp) % 2 == 1){ |
|
| 445 | - $data = array(); |
|
| 446 | - for($i=1; $i<count($resp); $i+=2){ |
|
| 447 | - if($cmd[0] == 'z'){ |
|
| 448 | - $data[$resp[$i]] = intval($resp[$i + 1]); |
|
| 449 | - }else{ |
|
| 450 | - $data[$resp[$i]] = $resp[$i + 1]; |
|
| 451 | - } |
|
| 452 | - } |
|
| 453 | - return new SSDB_Response('ok', $data); |
|
| 454 | - }else{ |
|
| 455 | - return new SSDB_Response('server_error', 'Invalid response'); |
|
| 456 | - } |
|
| 457 | - }else{ |
|
| 458 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 459 | - return new SSDB_Response($resp[0], $errmsg); |
|
| 460 | - } |
|
| 461 | - break; |
|
| 462 | - default: |
|
| 463 | - return new SSDB_Response($resp[0], array_slice($resp, 1)); |
|
| 464 | - } |
|
| 465 | - return new SSDB_Response('error', 'Unknown command: $cmd'); |
|
| 466 | - } |
|
| 467 | - private function send($data){ |
|
| 468 | - $ps = array(); |
|
| 469 | - foreach($data as $p){ |
|
| 470 | - $ps[] = strlen($p); |
|
| 471 | - $ps[] = $p; |
|
| 472 | - } |
|
| 473 | - $s = join("\n", $ps) . "\n\n"; |
|
| 474 | - if($this->debug){ |
|
| 475 | - echo '> ' . str_replace(array("\r", "\n"), array('\r', '\n'), $s) . "\n"; |
|
| 476 | - } |
|
| 477 | - try{ |
|
| 478 | - while(true){ |
|
| 479 | - $ret = @fwrite($this->sock, $s); |
|
| 480 | - if($ret === false || $ret === 0){ |
|
| 481 | - $this->close(); |
|
| 482 | - throw new SSDBException('Connection lost'); |
|
| 483 | - } |
|
| 484 | - $s = substr($s, $ret); |
|
| 485 | - if(strlen($s) == 0){ |
|
| 486 | - break; |
|
| 487 | - } |
|
| 488 | - @fflush($this->sock); |
|
| 489 | - } |
|
| 490 | - }catch(Exception $e){ |
|
| 491 | - $this->close(); |
|
| 492 | - throw new SSDBException($e->getMessage()); |
|
| 493 | - } |
|
| 494 | - return $ret; |
|
| 495 | - } |
|
| 496 | - private function recv(){ |
|
| 497 | - $this->step = self::STEP_SIZE; |
|
| 498 | - while(true){ |
|
| 499 | - $ret = $this->parse(); |
|
| 500 | - if($ret === null){ |
|
| 501 | - try{ |
|
| 502 | - $data = @fread($this->sock, 1024 * 1024); |
|
| 503 | - if($this->debug){ |
|
| 504 | - echo '< ' . str_replace(array("\r", "\n"), array('\r', '\n'), $data) . "\n"; |
|
| 505 | - } |
|
| 506 | - }catch(Exception $e){ |
|
| 507 | - $data = ''; |
|
| 508 | - } |
|
| 509 | - if($data === false || $data === ''){ |
|
| 510 | - if(feof($this->sock)){ |
|
| 511 | - $this->close(); |
|
| 512 | - throw new SSDBException('Connection lost'); |
|
| 513 | - }else{ |
|
| 514 | - throw new SSDBTimeoutException('Connection timeout'); |
|
| 515 | - } |
|
| 516 | - } |
|
| 517 | - $this->recv_buf .= $data; |
|
| 174 | + $resp = $this->check_easy_resp($cmd, $resp); |
|
| 175 | + return $resp; |
|
| 176 | + } |
|
| 177 | + private function check_easy_resp($cmd, $resp){ |
|
| 178 | + $this->last_resp = $resp; |
|
| 179 | + if($this->_easy){ |
|
| 180 | + if($resp->not_found()){ |
|
| 181 | + return NULL; |
|
| 182 | + }else if(!$resp->ok() && !is_array($resp->data)){ |
|
| 183 | + return false; |
|
| 184 | + }else{ |
|
| 185 | + return $resp->data; |
|
| 186 | + } |
|
| 187 | + }else{ |
|
| 188 | + $resp->cmd = $cmd; |
|
| 189 | + return $resp; |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + function multi_set($kvs=array()){ |
|
| 193 | + $args = array(); |
|
| 194 | + foreach($kvs as $k=>$v){ |
|
| 195 | + $args[] = $k; |
|
| 196 | + $args[] = $v; |
|
| 197 | + } |
|
| 198 | + return $this->__call(__FUNCTION__, $args); |
|
| 199 | + } |
|
| 200 | + function multi_hset($name, $kvs=array()){ |
|
| 201 | + $args = array($name); |
|
| 202 | + foreach($kvs as $k=>$v){ |
|
| 203 | + $args[] = $k; |
|
| 204 | + $args[] = $v; |
|
| 205 | + } |
|
| 206 | + return $this->__call(__FUNCTION__, $args); |
|
| 207 | + } |
|
| 208 | + function multi_zset($name, $kvs=array()){ |
|
| 209 | + $args = array($name); |
|
| 210 | + foreach($kvs as $k=>$v){ |
|
| 211 | + $args[] = $k; |
|
| 212 | + $args[] = $v; |
|
| 213 | + } |
|
| 214 | + return $this->__call(__FUNCTION__, $args); |
|
| 215 | + } |
|
| 216 | + function incr($key, $val=1){ |
|
| 217 | + $args = func_get_args(); |
|
| 218 | + return $this->__call(__FUNCTION__, $args); |
|
| 219 | + } |
|
| 220 | + function decr($key, $val=1){ |
|
| 221 | + $args = func_get_args(); |
|
| 222 | + return $this->__call(__FUNCTION__, $args); |
|
| 223 | + } |
|
| 224 | + function zincr($name, $key, $score=1){ |
|
| 225 | + $args = func_get_args(); |
|
| 226 | + return $this->__call(__FUNCTION__, $args); |
|
| 227 | + } |
|
| 228 | + function zdecr($name, $key, $score=1){ |
|
| 229 | + $args = func_get_args(); |
|
| 230 | + return $this->__call(__FUNCTION__, $args); |
|
| 231 | + } |
|
| 232 | + function zadd($key, $score, $value){ |
|
| 233 | + $args = array($key, $value, $score); |
|
| 234 | + return $this->__call('zset', $args); |
|
| 235 | + } |
|
| 236 | + function zRevRank($name, $key){ |
|
| 237 | + $args = func_get_args(); |
|
| 238 | + return $this->__call("zrrank", $args); |
|
| 239 | + } |
|
| 240 | + function zRevRange($name, $offset, $limit){ |
|
| 241 | + $args = func_get_args(); |
|
| 242 | + return $this->__call("zrrange", $args); |
|
| 243 | + } |
|
| 244 | + function hincr($name, $key, $val=1){ |
|
| 245 | + $args = func_get_args(); |
|
| 246 | + return $this->__call(__FUNCTION__, $args); |
|
| 247 | + } |
|
| 248 | + function hdecr($name, $key, $val=1){ |
|
| 249 | + $args = func_get_args(); |
|
| 250 | + return $this->__call(__FUNCTION__, $args); |
|
| 251 | + } |
|
| 252 | + private function send_req($cmd, $params){ |
|
| 253 | + $req = array($cmd); |
|
| 254 | + foreach($params as $p){ |
|
| 255 | + if(is_array($p)){ |
|
| 256 | + $req = array_merge($req, $p); |
|
| 257 | + }else{ |
|
| 258 | + $req[] = $p; |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + return $this->send($req); |
|
| 262 | + } |
|
| 263 | + private function recv_resp($cmd, $params){ |
|
| 264 | + $resp = $this->recv(); |
|
| 265 | + if($resp === false){ |
|
| 266 | + return new SSDB_Response('error', 'Unknown error'); |
|
| 267 | + }else if(!$resp){ |
|
| 268 | + return new SSDB_Response('disconnected', 'Connection closed'); |
|
| 269 | + } |
|
| 270 | + if($resp[0] == 'noauth'){ |
|
| 271 | + $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 272 | + return new SSDB_Response($resp[0], $errmsg); |
|
| 273 | + } |
|
| 274 | + switch($cmd){ |
|
| 275 | + case 'dbsize': |
|
| 276 | + case 'ping': |
|
| 277 | + case 'qset': |
|
| 278 | + case 'getbit': |
|
| 279 | + case 'setbit': |
|
| 280 | + case 'countbit': |
|
| 281 | + case 'strlen': |
|
| 282 | + case 'set': |
|
| 283 | + case 'setx': |
|
| 284 | + case 'setnx': |
|
| 285 | + case 'zset': |
|
| 286 | + case 'hset': |
|
| 287 | + case 'qpush': |
|
| 288 | + case 'qpush_front': |
|
| 289 | + case 'qpush_back': |
|
| 290 | + case 'qtrim_front': |
|
| 291 | + case 'qtrim_back': |
|
| 292 | + case 'del': |
|
| 293 | + case 'zdel': |
|
| 294 | + case 'hdel': |
|
| 295 | + case 'hsize': |
|
| 296 | + case 'zsize': |
|
| 297 | + case 'qsize': |
|
| 298 | + case 'hclear': |
|
| 299 | + case 'zclear': |
|
| 300 | + case 'qclear': |
|
| 301 | + case 'multi_set': |
|
| 302 | + case 'multi_del': |
|
| 303 | + case 'multi_hset': |
|
| 304 | + case 'multi_hdel': |
|
| 305 | + case 'multi_zset': |
|
| 306 | + case 'multi_zdel': |
|
| 307 | + case 'incr': |
|
| 308 | + case 'decr': |
|
| 309 | + case 'zincr': |
|
| 310 | + case 'zdecr': |
|
| 311 | + case 'hincr': |
|
| 312 | + case 'hdecr': |
|
| 313 | + case 'zget': |
|
| 314 | + case 'zrank': |
|
| 315 | + case 'zrrank': |
|
| 316 | + case 'zcount': |
|
| 317 | + case 'zsum': |
|
| 318 | + case 'zremrangebyrank': |
|
| 319 | + case 'zremrangebyscore': |
|
| 320 | + case 'ttl': |
|
| 321 | + case 'expire': |
|
| 322 | + if($resp[0] == 'ok'){ |
|
| 323 | + $val = isset($resp[1])? intval($resp[1]) : 0; |
|
| 324 | + return new SSDB_Response($resp[0], $val); |
|
| 325 | + }else{ |
|
| 326 | + $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 327 | + return new SSDB_Response($resp[0], $errmsg); |
|
| 328 | + } |
|
| 329 | + case 'zavg': |
|
| 330 | + if($resp[0] == 'ok'){ |
|
| 331 | + $val = isset($resp[1])? floatval($resp[1]) : (float)0; |
|
| 332 | + return new SSDB_Response($resp[0], $val); |
|
| 333 | + }else{ |
|
| 334 | + $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 335 | + return new SSDB_Response($resp[0], $errmsg); |
|
| 336 | + } |
|
| 337 | + case 'get': |
|
| 338 | + case 'substr': |
|
| 339 | + case 'getset': |
|
| 340 | + case 'hget': |
|
| 341 | + case 'qget': |
|
| 342 | + case 'qfront': |
|
| 343 | + case 'qback': |
|
| 344 | + if($resp[0] == 'ok'){ |
|
| 345 | + if(count($resp) == 2){ |
|
| 346 | + return new SSDB_Response('ok', $resp[1]); |
|
| 347 | + }else{ |
|
| 348 | + return new SSDB_Response('server_error', 'Invalid response'); |
|
| 349 | + } |
|
| 350 | + }else{ |
|
| 351 | + $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 352 | + return new SSDB_Response($resp[0], $errmsg); |
|
| 353 | + } |
|
| 354 | + break; |
|
| 355 | + case 'qpop': |
|
| 356 | + case 'qpop_front': |
|
| 357 | + case 'qpop_back': |
|
| 358 | + if($resp[0] == 'ok'){ |
|
| 359 | + $size = 1; |
|
| 360 | + if(isset($params[1])){ |
|
| 361 | + $size = intval($params[1]); |
|
| 362 | + } |
|
| 363 | + if($size <= 1){ |
|
| 364 | + if(count($resp) == 2){ |
|
| 365 | + return new SSDB_Response('ok', $resp[1]); |
|
| 366 | + }else{ |
|
| 367 | + return new SSDB_Response('server_error', 'Invalid response'); |
|
| 368 | + } |
|
| 369 | + }else{ |
|
| 370 | + $data = array_slice($resp, 1); |
|
| 371 | + return new SSDB_Response('ok', $data); |
|
| 372 | + } |
|
| 373 | + }else{ |
|
| 374 | + $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 375 | + return new SSDB_Response($resp[0], $errmsg); |
|
| 376 | + } |
|
| 377 | + break; |
|
| 378 | + case 'keys': |
|
| 379 | + case 'zkeys': |
|
| 380 | + case 'hkeys': |
|
| 381 | + case 'hlist': |
|
| 382 | + case 'zlist': |
|
| 383 | + case 'qslice': |
|
| 384 | + if($resp[0] == 'ok'){ |
|
| 385 | + $data = array(); |
|
| 386 | + if($resp[0] == 'ok'){ |
|
| 387 | + $data = array_slice($resp, 1); |
|
| 388 | + } |
|
| 389 | + return new SSDB_Response($resp[0], $data); |
|
| 390 | + }else{ |
|
| 391 | + $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 392 | + return new SSDB_Response($resp[0], $errmsg); |
|
| 393 | + } |
|
| 394 | + case 'auth': |
|
| 395 | + case 'exists': |
|
| 396 | + case 'hexists': |
|
| 397 | + case 'zexists': |
|
| 398 | + if($resp[0] == 'ok'){ |
|
| 399 | + if(count($resp) == 2){ |
|
| 400 | + return new SSDB_Response('ok', (bool)$resp[1]); |
|
| 401 | + }else{ |
|
| 402 | + return new SSDB_Response('server_error', 'Invalid response'); |
|
| 403 | + } |
|
| 404 | + }else{ |
|
| 405 | + $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 406 | + return new SSDB_Response($resp[0], $errmsg); |
|
| 407 | + } |
|
| 408 | + break; |
|
| 409 | + case 'multi_exists': |
|
| 410 | + case 'multi_hexists': |
|
| 411 | + case 'multi_zexists': |
|
| 412 | + if($resp[0] == 'ok'){ |
|
| 413 | + if(count($resp) % 2 == 1){ |
|
| 414 | + $data = array(); |
|
| 415 | + for($i=1; $i<count($resp); $i+=2){ |
|
| 416 | + $data[$resp[$i]] = (bool)$resp[$i + 1]; |
|
| 417 | + } |
|
| 418 | + return new SSDB_Response('ok', $data); |
|
| 419 | + }else{ |
|
| 420 | + return new SSDB_Response('server_error', 'Invalid response'); |
|
| 421 | + } |
|
| 422 | + }else{ |
|
| 423 | + $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 424 | + return new SSDB_Response($resp[0], $errmsg); |
|
| 425 | + } |
|
| 426 | + break; |
|
| 427 | + case 'scan': |
|
| 428 | + case 'rscan': |
|
| 429 | + case 'zscan': |
|
| 430 | + case 'zrscan': |
|
| 431 | + case 'zrange': |
|
| 432 | + case 'zrrange': |
|
| 433 | + case 'hscan': |
|
| 434 | + case 'hrscan': |
|
| 435 | + case 'hgetall': |
|
| 436 | + case 'multi_hsize': |
|
| 437 | + case 'multi_zsize': |
|
| 438 | + case 'multi_get': |
|
| 439 | + case 'multi_hget': |
|
| 440 | + case 'multi_zget': |
|
| 441 | + case 'zpop_front': |
|
| 442 | + case 'zpop_back': |
|
| 443 | + if($resp[0] == 'ok'){ |
|
| 444 | + if(count($resp) % 2 == 1){ |
|
| 445 | + $data = array(); |
|
| 446 | + for($i=1; $i<count($resp); $i+=2){ |
|
| 447 | + if($cmd[0] == 'z'){ |
|
| 448 | + $data[$resp[$i]] = intval($resp[$i + 1]); |
|
| 449 | + }else{ |
|
| 450 | + $data[$resp[$i]] = $resp[$i + 1]; |
|
| 451 | + } |
|
| 452 | + } |
|
| 453 | + return new SSDB_Response('ok', $data); |
|
| 454 | + }else{ |
|
| 455 | + return new SSDB_Response('server_error', 'Invalid response'); |
|
| 456 | + } |
|
| 457 | + }else{ |
|
| 458 | + $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 459 | + return new SSDB_Response($resp[0], $errmsg); |
|
| 460 | + } |
|
| 461 | + break; |
|
| 462 | + default: |
|
| 463 | + return new SSDB_Response($resp[0], array_slice($resp, 1)); |
|
| 464 | + } |
|
| 465 | + return new SSDB_Response('error', 'Unknown command: $cmd'); |
|
| 466 | + } |
|
| 467 | + private function send($data){ |
|
| 468 | + $ps = array(); |
|
| 469 | + foreach($data as $p){ |
|
| 470 | + $ps[] = strlen($p); |
|
| 471 | + $ps[] = $p; |
|
| 472 | + } |
|
| 473 | + $s = join("\n", $ps) . "\n\n"; |
|
| 474 | + if($this->debug){ |
|
| 475 | + echo '> ' . str_replace(array("\r", "\n"), array('\r', '\n'), $s) . "\n"; |
|
| 476 | + } |
|
| 477 | + try{ |
|
| 478 | + while(true){ |
|
| 479 | + $ret = @fwrite($this->sock, $s); |
|
| 480 | + if($ret === false || $ret === 0){ |
|
| 481 | + $this->close(); |
|
| 482 | + throw new SSDBException('Connection lost'); |
|
| 483 | + } |
|
| 484 | + $s = substr($s, $ret); |
|
| 485 | + if(strlen($s) == 0){ |
|
| 486 | + break; |
|
| 487 | + } |
|
| 488 | + @fflush($this->sock); |
|
| 489 | + } |
|
| 490 | + }catch(Exception $e){ |
|
| 491 | + $this->close(); |
|
| 492 | + throw new SSDBException($e->getMessage()); |
|
| 493 | + } |
|
| 494 | + return $ret; |
|
| 495 | + } |
|
| 496 | + private function recv(){ |
|
| 497 | + $this->step = self::STEP_SIZE; |
|
| 498 | + while(true){ |
|
| 499 | + $ret = $this->parse(); |
|
| 500 | + if($ret === null){ |
|
| 501 | + try{ |
|
| 502 | + $data = @fread($this->sock, 1024 * 1024); |
|
| 503 | + if($this->debug){ |
|
| 504 | + echo '< ' . str_replace(array("\r", "\n"), array('\r', '\n'), $data) . "\n"; |
|
| 505 | + } |
|
| 506 | + }catch(Exception $e){ |
|
| 507 | + $data = ''; |
|
| 508 | + } |
|
| 509 | + if($data === false || $data === ''){ |
|
| 510 | + if(feof($this->sock)){ |
|
| 511 | + $this->close(); |
|
| 512 | + throw new SSDBException('Connection lost'); |
|
| 513 | + }else{ |
|
| 514 | + throw new SSDBTimeoutException('Connection timeout'); |
|
| 515 | + } |
|
| 516 | + } |
|
| 517 | + $this->recv_buf .= $data; |
|
| 518 | 518 | # echo "read " . strlen($data) . " total: " . strlen($this->recv_buf) . "\n"; |
| 519 | - }else{ |
|
| 520 | - return $ret; |
|
| 521 | - } |
|
| 522 | - } |
|
| 523 | - } |
|
| 524 | - const STEP_SIZE = 0; |
|
| 525 | - const STEP_DATA = 1; |
|
| 526 | - public $resp = array(); |
|
| 527 | - public $step; |
|
| 528 | - public $block_size; |
|
| 529 | - private function parse(){ |
|
| 530 | - $spos = 0; |
|
| 531 | - $epos = 0; |
|
| 532 | - $buf_size = strlen($this->recv_buf); |
|
| 533 | - // performance issue for large reponse |
|
| 534 | - //$this->recv_buf = ltrim($this->recv_buf); |
|
| 535 | - while(true){ |
|
| 536 | - $spos = $epos; |
|
| 537 | - if($this->step === self::STEP_SIZE){ |
|
| 538 | - $epos = strpos($this->recv_buf, "\n", $spos); |
|
| 539 | - if($epos === false){ |
|
| 540 | - break; |
|
| 541 | - } |
|
| 542 | - $epos += 1; |
|
| 543 | - $line = substr($this->recv_buf, $spos, $epos - $spos); |
|
| 544 | - $spos = $epos; |
|
| 545 | - $line = trim($line); |
|
| 546 | - if(strlen($line) == 0){ // head end |
|
| 547 | - $this->recv_buf = substr($this->recv_buf, $spos); |
|
| 548 | - $ret = $this->resp; |
|
| 549 | - $this->resp = array(); |
|
| 550 | - return $ret; |
|
| 551 | - } |
|
| 552 | - $this->block_size = intval($line); |
|
| 553 | - $this->step = self::STEP_DATA; |
|
| 554 | - } |
|
| 555 | - if($this->step === self::STEP_DATA){ |
|
| 556 | - $epos = $spos + $this->block_size; |
|
| 557 | - if($epos <= $buf_size){ |
|
| 558 | - $n = strpos($this->recv_buf, "\n", $epos); |
|
| 559 | - if($n !== false){ |
|
| 560 | - $data = substr($this->recv_buf, $spos, $epos - $spos); |
|
| 561 | - $this->resp[] = $data; |
|
| 562 | - $epos = $n + 1; |
|
| 563 | - $this->step = self::STEP_SIZE; |
|
| 564 | - continue; |
|
| 565 | - } |
|
| 566 | - } |
|
| 567 | - break; |
|
| 568 | - } |
|
| 569 | - } |
|
| 570 | - // packet not ready |
|
| 571 | - if($spos > 0){ |
|
| 572 | - $this->recv_buf = substr($this->recv_buf, $spos); |
|
| 573 | - } |
|
| 574 | - return null; |
|
| 575 | - } |
|
| 519 | + }else{ |
|
| 520 | + return $ret; |
|
| 521 | + } |
|
| 522 | + } |
|
| 523 | + } |
|
| 524 | + const STEP_SIZE = 0; |
|
| 525 | + const STEP_DATA = 1; |
|
| 526 | + public $resp = array(); |
|
| 527 | + public $step; |
|
| 528 | + public $block_size; |
|
| 529 | + private function parse(){ |
|
| 530 | + $spos = 0; |
|
| 531 | + $epos = 0; |
|
| 532 | + $buf_size = strlen($this->recv_buf); |
|
| 533 | + // performance issue for large reponse |
|
| 534 | + //$this->recv_buf = ltrim($this->recv_buf); |
|
| 535 | + while(true){ |
|
| 536 | + $spos = $epos; |
|
| 537 | + if($this->step === self::STEP_SIZE){ |
|
| 538 | + $epos = strpos($this->recv_buf, "\n", $spos); |
|
| 539 | + if($epos === false){ |
|
| 540 | + break; |
|
| 541 | + } |
|
| 542 | + $epos += 1; |
|
| 543 | + $line = substr($this->recv_buf, $spos, $epos - $spos); |
|
| 544 | + $spos = $epos; |
|
| 545 | + $line = trim($line); |
|
| 546 | + if(strlen($line) == 0){ // head end |
|
| 547 | + $this->recv_buf = substr($this->recv_buf, $spos); |
|
| 548 | + $ret = $this->resp; |
|
| 549 | + $this->resp = array(); |
|
| 550 | + return $ret; |
|
| 551 | + } |
|
| 552 | + $this->block_size = intval($line); |
|
| 553 | + $this->step = self::STEP_DATA; |
|
| 554 | + } |
|
| 555 | + if($this->step === self::STEP_DATA){ |
|
| 556 | + $epos = $spos + $this->block_size; |
|
| 557 | + if($epos <= $buf_size){ |
|
| 558 | + $n = strpos($this->recv_buf, "\n", $epos); |
|
| 559 | + if($n !== false){ |
|
| 560 | + $data = substr($this->recv_buf, $spos, $epos - $spos); |
|
| 561 | + $this->resp[] = $data; |
|
| 562 | + $epos = $n + 1; |
|
| 563 | + $this->step = self::STEP_SIZE; |
|
| 564 | + continue; |
|
| 565 | + } |
|
| 566 | + } |
|
| 567 | + break; |
|
| 568 | + } |
|
| 569 | + } |
|
| 570 | + // packet not ready |
|
| 571 | + if($spos > 0){ |
|
| 572 | + $this->recv_buf = substr($this->recv_buf, $spos); |
|
| 573 | + } |
|
| 574 | + return null; |
|
| 575 | + } |
|
| 576 | 576 | } |
| 577 | 577 | \ No newline at end of file |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | class SimpleSSDB extends SSDB |
| 21 | 21 | { |
| 22 | - function __construct($host, $port, $timeout_ms=2000){ |
|
| 22 | + function __construct($host, $port, $timeout_ms = 2000) { |
|
| 23 | 23 | parent::__construct($host, $port, $timeout_ms); |
| 24 | 24 | $this->easy(); |
| 25 | 25 | } |
@@ -30,26 +30,26 @@ discard block |
||
| 30 | 30 | public $code; |
| 31 | 31 | public $data = null; |
| 32 | 32 | public $message; |
| 33 | - function __construct($code='ok', $data_or_message=null){ |
|
| 33 | + function __construct($code = 'ok', $data_or_message = null) { |
|
| 34 | 34 | $this->code = $code; |
| 35 | - if($code == 'ok'){ |
|
| 35 | + if ($code == 'ok') { |
|
| 36 | 36 | $this->data = $data_or_message; |
| 37 | - }else{ |
|
| 37 | + } else { |
|
| 38 | 38 | $this->message = $data_or_message; |
| 39 | 39 | } |
| 40 | 40 | } |
| 41 | - function __toString(){ |
|
| 42 | - if($this->code == 'ok'){ |
|
| 43 | - $s = $this->data === null? '' : json_encode($this->data); |
|
| 44 | - }else{ |
|
| 41 | + function __toString() { |
|
| 42 | + if ($this->code == 'ok') { |
|
| 43 | + $s = $this->data === null ? '' : json_encode($this->data); |
|
| 44 | + } else { |
|
| 45 | 45 | $s = $this->message; |
| 46 | 46 | } |
| 47 | 47 | return sprintf('%-13s %12s %s', $this->cmd, $this->code, $s); |
| 48 | 48 | } |
| 49 | - function ok(){ |
|
| 49 | + function ok() { |
|
| 50 | 50 | return $this->code == 'ok'; |
| 51 | 51 | } |
| 52 | - function not_found(){ |
|
| 52 | + function not_found() { |
|
| 53 | 53 | return $this->code == 'not_found'; |
| 54 | 54 | } |
| 55 | 55 | } |
@@ -61,22 +61,22 @@ discard block |
||
| 61 | 61 | private $recv_buf = ''; |
| 62 | 62 | private $_easy = false; |
| 63 | 63 | public $last_resp = null; |
| 64 | - function __construct($host, $port, $timeout_ms=2000){ |
|
| 65 | - $timeout_f = (float)$timeout_ms/1000; |
|
| 64 | + function __construct($host, $port, $timeout_ms = 2000) { |
|
| 65 | + $timeout_f = (float) $timeout_ms / 1000; |
|
| 66 | 66 | $this->sock = @stream_socket_client("$host:$port", $errno, $errstr, $timeout_f); |
| 67 | - if(!$this->sock){ |
|
| 67 | + if (!$this->sock) { |
|
| 68 | 68 | throw new SSDBException("$errno: $errstr"); |
| 69 | 69 | } |
| 70 | - $timeout_sec = intval($timeout_ms/1000); |
|
| 70 | + $timeout_sec = intval($timeout_ms / 1000); |
|
| 71 | 71 | $timeout_usec = ($timeout_ms - $timeout_sec * 1000) * 1000; |
| 72 | 72 | @stream_set_timeout($this->sock, $timeout_sec, $timeout_usec); |
| 73 | - if(function_exists('stream_set_chunk_size')){ |
|
| 73 | + if (function_exists('stream_set_chunk_size')) { |
|
| 74 | 74 | @stream_set_chunk_size($this->sock, 1024 * 1024); |
| 75 | 75 | } |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - function set_timeout($timeout_ms){ |
|
| 79 | - $timeout_sec = intval($timeout_ms/1000); |
|
| 78 | + function set_timeout($timeout_ms) { |
|
| 79 | + $timeout_sec = intval($timeout_ms / 1000); |
|
| 80 | 80 | $timeout_usec = ($timeout_ms - $timeout_sec * 1000) * 1000; |
| 81 | 81 | @stream_set_timeout($this->sock, $timeout_sec, $timeout_usec); |
| 82 | 82 | } |
@@ -87,36 +87,36 @@ discard block |
||
| 87 | 87 | * And some certain methods like get/zget will return false |
| 88 | 88 | * when response is not ok(not_found, etc) |
| 89 | 89 | */ |
| 90 | - function easy(){ |
|
| 90 | + function easy() { |
|
| 91 | 91 | $this->_easy = true; |
| 92 | 92 | } |
| 93 | - function close(){ |
|
| 94 | - if(!$this->_closed){ |
|
| 93 | + function close() { |
|
| 94 | + if (!$this->_closed) { |
|
| 95 | 95 | @fclose($this->sock); |
| 96 | 96 | $this->_closed = true; |
| 97 | 97 | $this->sock = null; |
| 98 | 98 | } |
| 99 | 99 | } |
| 100 | - function closed(){ |
|
| 100 | + function closed() { |
|
| 101 | 101 | return $this->_closed; |
| 102 | 102 | } |
| 103 | 103 | private $batch_mode = false; |
| 104 | 104 | private $batch_cmds = array(); |
| 105 | - function batch(){ |
|
| 105 | + function batch() { |
|
| 106 | 106 | $this->batch_mode = true; |
| 107 | 107 | $this->batch_cmds = array(); |
| 108 | 108 | return $this; |
| 109 | 109 | } |
| 110 | - function multi(){ |
|
| 110 | + function multi() { |
|
| 111 | 111 | return $this->batch(); |
| 112 | 112 | } |
| 113 | - function exec(){ |
|
| 113 | + function exec() { |
|
| 114 | 114 | $ret = array(); |
| 115 | - foreach($this->batch_cmds as $op){ |
|
| 115 | + foreach ($this->batch_cmds as $op) { |
|
| 116 | 116 | list($cmd, $params) = $op; |
| 117 | 117 | $this->send_req($cmd, $params); |
| 118 | 118 | } |
| 119 | - foreach($this->batch_cmds as $op){ |
|
| 119 | + foreach ($this->batch_cmds as $op) { |
|
| 120 | 120 | list($cmd, $params) = $op; |
| 121 | 121 | $resp = $this->recv_resp($cmd, $params); |
| 122 | 122 | $resp = $this->check_easy_resp($cmd, $resp); |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | return $ret; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | - function request(){ |
|
| 130 | + function request() { |
|
| 131 | 131 | $args = func_get_args(); |
| 132 | 132 | $cmd = array_shift($args); |
| 133 | 133 | return $this->__call($cmd, $args); |
@@ -135,38 +135,38 @@ discard block |
||
| 135 | 135 | |
| 136 | 136 | private $async_auth_password = null; |
| 137 | 137 | |
| 138 | - function auth($password){ |
|
| 138 | + function auth($password) { |
|
| 139 | 139 | $this->async_auth_password = $password; |
| 140 | 140 | return null; |
| 141 | 141 | } |
| 142 | - function __call($cmd, $params=array()){ |
|
| 142 | + function __call($cmd, $params = array()) { |
|
| 143 | 143 | $cmd = strtolower($cmd); |
| 144 | - if($this->async_auth_password !== null){ |
|
| 144 | + if ($this->async_auth_password !== null) { |
|
| 145 | 145 | $pass = $this->async_auth_password; |
| 146 | 146 | $this->async_auth_password = null; |
| 147 | 147 | $auth = $this->__call('auth', array($pass)); |
| 148 | - if($auth !== true){ |
|
| 148 | + if ($auth !== true) { |
|
| 149 | 149 | throw new Exception("Authentication failed"); |
| 150 | 150 | } |
| 151 | 151 | } |
| 152 | - if($this->batch_mode){ |
|
| 152 | + if ($this->batch_mode) { |
|
| 153 | 153 | $this->batch_cmds[] = array($cmd, $params); |
| 154 | 154 | return $this; |
| 155 | 155 | } |
| 156 | - try{ |
|
| 157 | - if($this->send_req($cmd, $params) === false){ |
|
| 156 | + try { |
|
| 157 | + if ($this->send_req($cmd, $params) === false) { |
|
| 158 | 158 | $resp = new SSDB_Response('error', 'send error'); |
| 159 | - }else{ |
|
| 159 | + } else { |
|
| 160 | 160 | $resp = $this->recv_resp($cmd, $params); |
| 161 | 161 | } |
| 162 | - }catch(SSDBException $e){ |
|
| 163 | - if($this->_easy){ |
|
| 162 | + } catch (SSDBException $e) { |
|
| 163 | + if ($this->_easy) { |
|
| 164 | 164 | throw $e; |
| 165 | - }else{ |
|
| 165 | + } else { |
|
| 166 | 166 | $resp = new SSDB_Response('error', $e->getMessage()); |
| 167 | 167 | } |
| 168 | 168 | } |
| 169 | - if($resp->code == 'noauth'){ |
|
| 169 | + if ($resp->code == 'noauth') { |
|
| 170 | 170 | $msg = $resp->message; |
| 171 | 171 | throw new Exception($msg); |
| 172 | 172 | } |
@@ -174,104 +174,104 @@ discard block |
||
| 174 | 174 | $resp = $this->check_easy_resp($cmd, $resp); |
| 175 | 175 | return $resp; |
| 176 | 176 | } |
| 177 | - private function check_easy_resp($cmd, $resp){ |
|
| 177 | + private function check_easy_resp($cmd, $resp) { |
|
| 178 | 178 | $this->last_resp = $resp; |
| 179 | - if($this->_easy){ |
|
| 180 | - if($resp->not_found()){ |
|
| 179 | + if ($this->_easy) { |
|
| 180 | + if ($resp->not_found()) { |
|
| 181 | 181 | return NULL; |
| 182 | - }else if(!$resp->ok() && !is_array($resp->data)){ |
|
| 182 | + } else if (!$resp->ok() && !is_array($resp->data)) { |
|
| 183 | 183 | return false; |
| 184 | - }else{ |
|
| 184 | + } else { |
|
| 185 | 185 | return $resp->data; |
| 186 | 186 | } |
| 187 | - }else{ |
|
| 187 | + } else { |
|
| 188 | 188 | $resp->cmd = $cmd; |
| 189 | 189 | return $resp; |
| 190 | 190 | } |
| 191 | 191 | } |
| 192 | - function multi_set($kvs=array()){ |
|
| 192 | + function multi_set($kvs = array()) { |
|
| 193 | 193 | $args = array(); |
| 194 | - foreach($kvs as $k=>$v){ |
|
| 194 | + foreach ($kvs as $k=>$v) { |
|
| 195 | 195 | $args[] = $k; |
| 196 | 196 | $args[] = $v; |
| 197 | 197 | } |
| 198 | 198 | return $this->__call(__FUNCTION__, $args); |
| 199 | 199 | } |
| 200 | - function multi_hset($name, $kvs=array()){ |
|
| 200 | + function multi_hset($name, $kvs = array()) { |
|
| 201 | 201 | $args = array($name); |
| 202 | - foreach($kvs as $k=>$v){ |
|
| 202 | + foreach ($kvs as $k=>$v) { |
|
| 203 | 203 | $args[] = $k; |
| 204 | 204 | $args[] = $v; |
| 205 | 205 | } |
| 206 | 206 | return $this->__call(__FUNCTION__, $args); |
| 207 | 207 | } |
| 208 | - function multi_zset($name, $kvs=array()){ |
|
| 208 | + function multi_zset($name, $kvs = array()) { |
|
| 209 | 209 | $args = array($name); |
| 210 | - foreach($kvs as $k=>$v){ |
|
| 210 | + foreach ($kvs as $k=>$v) { |
|
| 211 | 211 | $args[] = $k; |
| 212 | 212 | $args[] = $v; |
| 213 | 213 | } |
| 214 | 214 | return $this->__call(__FUNCTION__, $args); |
| 215 | 215 | } |
| 216 | - function incr($key, $val=1){ |
|
| 216 | + function incr($key, $val = 1) { |
|
| 217 | 217 | $args = func_get_args(); |
| 218 | 218 | return $this->__call(__FUNCTION__, $args); |
| 219 | 219 | } |
| 220 | - function decr($key, $val=1){ |
|
| 220 | + function decr($key, $val = 1) { |
|
| 221 | 221 | $args = func_get_args(); |
| 222 | 222 | return $this->__call(__FUNCTION__, $args); |
| 223 | 223 | } |
| 224 | - function zincr($name, $key, $score=1){ |
|
| 224 | + function zincr($name, $key, $score = 1) { |
|
| 225 | 225 | $args = func_get_args(); |
| 226 | 226 | return $this->__call(__FUNCTION__, $args); |
| 227 | 227 | } |
| 228 | - function zdecr($name, $key, $score=1){ |
|
| 228 | + function zdecr($name, $key, $score = 1) { |
|
| 229 | 229 | $args = func_get_args(); |
| 230 | 230 | return $this->__call(__FUNCTION__, $args); |
| 231 | 231 | } |
| 232 | - function zadd($key, $score, $value){ |
|
| 232 | + function zadd($key, $score, $value) { |
|
| 233 | 233 | $args = array($key, $value, $score); |
| 234 | 234 | return $this->__call('zset', $args); |
| 235 | 235 | } |
| 236 | - function zRevRank($name, $key){ |
|
| 236 | + function zRevRank($name, $key) { |
|
| 237 | 237 | $args = func_get_args(); |
| 238 | 238 | return $this->__call("zrrank", $args); |
| 239 | 239 | } |
| 240 | - function zRevRange($name, $offset, $limit){ |
|
| 240 | + function zRevRange($name, $offset, $limit) { |
|
| 241 | 241 | $args = func_get_args(); |
| 242 | 242 | return $this->__call("zrrange", $args); |
| 243 | 243 | } |
| 244 | - function hincr($name, $key, $val=1){ |
|
| 244 | + function hincr($name, $key, $val = 1) { |
|
| 245 | 245 | $args = func_get_args(); |
| 246 | 246 | return $this->__call(__FUNCTION__, $args); |
| 247 | 247 | } |
| 248 | - function hdecr($name, $key, $val=1){ |
|
| 248 | + function hdecr($name, $key, $val = 1) { |
|
| 249 | 249 | $args = func_get_args(); |
| 250 | 250 | return $this->__call(__FUNCTION__, $args); |
| 251 | 251 | } |
| 252 | - private function send_req($cmd, $params){ |
|
| 252 | + private function send_req($cmd, $params) { |
|
| 253 | 253 | $req = array($cmd); |
| 254 | - foreach($params as $p){ |
|
| 255 | - if(is_array($p)){ |
|
| 254 | + foreach ($params as $p) { |
|
| 255 | + if (is_array($p)) { |
|
| 256 | 256 | $req = array_merge($req, $p); |
| 257 | - }else{ |
|
| 257 | + } else { |
|
| 258 | 258 | $req[] = $p; |
| 259 | 259 | } |
| 260 | 260 | } |
| 261 | 261 | return $this->send($req); |
| 262 | 262 | } |
| 263 | - private function recv_resp($cmd, $params){ |
|
| 263 | + private function recv_resp($cmd, $params) { |
|
| 264 | 264 | $resp = $this->recv(); |
| 265 | - if($resp === false){ |
|
| 265 | + if ($resp === false) { |
|
| 266 | 266 | return new SSDB_Response('error', 'Unknown error'); |
| 267 | - }else if(!$resp){ |
|
| 267 | + } else if (!$resp) { |
|
| 268 | 268 | return new SSDB_Response('disconnected', 'Connection closed'); |
| 269 | 269 | } |
| 270 | - if($resp[0] == 'noauth'){ |
|
| 271 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 270 | + if ($resp[0] == 'noauth') { |
|
| 271 | + $errmsg = isset($resp[1]) ? $resp[1] : ''; |
|
| 272 | 272 | return new SSDB_Response($resp[0], $errmsg); |
| 273 | 273 | } |
| 274 | - switch($cmd){ |
|
| 274 | + switch ($cmd) { |
|
| 275 | 275 | case 'dbsize': |
| 276 | 276 | case 'ping': |
| 277 | 277 | case 'qset': |
@@ -319,19 +319,19 @@ discard block |
||
| 319 | 319 | case 'zremrangebyscore': |
| 320 | 320 | case 'ttl': |
| 321 | 321 | case 'expire': |
| 322 | - if($resp[0] == 'ok'){ |
|
| 323 | - $val = isset($resp[1])? intval($resp[1]) : 0; |
|
| 322 | + if ($resp[0] == 'ok') { |
|
| 323 | + $val = isset($resp[1]) ? intval($resp[1]) : 0; |
|
| 324 | 324 | return new SSDB_Response($resp[0], $val); |
| 325 | - }else{ |
|
| 326 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 325 | + } else { |
|
| 326 | + $errmsg = isset($resp[1]) ? $resp[1] : ''; |
|
| 327 | 327 | return new SSDB_Response($resp[0], $errmsg); |
| 328 | 328 | } |
| 329 | 329 | case 'zavg': |
| 330 | - if($resp[0] == 'ok'){ |
|
| 331 | - $val = isset($resp[1])? floatval($resp[1]) : (float)0; |
|
| 330 | + if ($resp[0] == 'ok') { |
|
| 331 | + $val = isset($resp[1]) ? floatval($resp[1]) : (float) 0; |
|
| 332 | 332 | return new SSDB_Response($resp[0], $val); |
| 333 | - }else{ |
|
| 334 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 333 | + } else { |
|
| 334 | + $errmsg = isset($resp[1]) ? $resp[1] : ''; |
|
| 335 | 335 | return new SSDB_Response($resp[0], $errmsg); |
| 336 | 336 | } |
| 337 | 337 | case 'get': |
@@ -341,37 +341,37 @@ discard block |
||
| 341 | 341 | case 'qget': |
| 342 | 342 | case 'qfront': |
| 343 | 343 | case 'qback': |
| 344 | - if($resp[0] == 'ok'){ |
|
| 345 | - if(count($resp) == 2){ |
|
| 344 | + if ($resp[0] == 'ok') { |
|
| 345 | + if (count($resp) == 2) { |
|
| 346 | 346 | return new SSDB_Response('ok', $resp[1]); |
| 347 | - }else{ |
|
| 347 | + } else { |
|
| 348 | 348 | return new SSDB_Response('server_error', 'Invalid response'); |
| 349 | 349 | } |
| 350 | - }else{ |
|
| 351 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 350 | + } else { |
|
| 351 | + $errmsg = isset($resp[1]) ? $resp[1] : ''; |
|
| 352 | 352 | return new SSDB_Response($resp[0], $errmsg); |
| 353 | 353 | } |
| 354 | 354 | break; |
| 355 | 355 | case 'qpop': |
| 356 | 356 | case 'qpop_front': |
| 357 | 357 | case 'qpop_back': |
| 358 | - if($resp[0] == 'ok'){ |
|
| 358 | + if ($resp[0] == 'ok') { |
|
| 359 | 359 | $size = 1; |
| 360 | - if(isset($params[1])){ |
|
| 360 | + if (isset($params[1])) { |
|
| 361 | 361 | $size = intval($params[1]); |
| 362 | 362 | } |
| 363 | - if($size <= 1){ |
|
| 364 | - if(count($resp) == 2){ |
|
| 363 | + if ($size <= 1) { |
|
| 364 | + if (count($resp) == 2) { |
|
| 365 | 365 | return new SSDB_Response('ok', $resp[1]); |
| 366 | - }else{ |
|
| 366 | + } else { |
|
| 367 | 367 | return new SSDB_Response('server_error', 'Invalid response'); |
| 368 | 368 | } |
| 369 | - }else{ |
|
| 369 | + } else { |
|
| 370 | 370 | $data = array_slice($resp, 1); |
| 371 | 371 | return new SSDB_Response('ok', $data); |
| 372 | 372 | } |
| 373 | - }else{ |
|
| 374 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 373 | + } else { |
|
| 374 | + $errmsg = isset($resp[1]) ? $resp[1] : ''; |
|
| 375 | 375 | return new SSDB_Response($resp[0], $errmsg); |
| 376 | 376 | } |
| 377 | 377 | break; |
@@ -381,46 +381,46 @@ discard block |
||
| 381 | 381 | case 'hlist': |
| 382 | 382 | case 'zlist': |
| 383 | 383 | case 'qslice': |
| 384 | - if($resp[0] == 'ok'){ |
|
| 384 | + if ($resp[0] == 'ok') { |
|
| 385 | 385 | $data = array(); |
| 386 | - if($resp[0] == 'ok'){ |
|
| 386 | + if ($resp[0] == 'ok') { |
|
| 387 | 387 | $data = array_slice($resp, 1); |
| 388 | 388 | } |
| 389 | 389 | return new SSDB_Response($resp[0], $data); |
| 390 | - }else{ |
|
| 391 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 390 | + } else { |
|
| 391 | + $errmsg = isset($resp[1]) ? $resp[1] : ''; |
|
| 392 | 392 | return new SSDB_Response($resp[0], $errmsg); |
| 393 | 393 | } |
| 394 | 394 | case 'auth': |
| 395 | 395 | case 'exists': |
| 396 | 396 | case 'hexists': |
| 397 | 397 | case 'zexists': |
| 398 | - if($resp[0] == 'ok'){ |
|
| 399 | - if(count($resp) == 2){ |
|
| 400 | - return new SSDB_Response('ok', (bool)$resp[1]); |
|
| 401 | - }else{ |
|
| 398 | + if ($resp[0] == 'ok') { |
|
| 399 | + if (count($resp) == 2) { |
|
| 400 | + return new SSDB_Response('ok', (bool) $resp[1]); |
|
| 401 | + } else { |
|
| 402 | 402 | return new SSDB_Response('server_error', 'Invalid response'); |
| 403 | 403 | } |
| 404 | - }else{ |
|
| 405 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 404 | + } else { |
|
| 405 | + $errmsg = isset($resp[1]) ? $resp[1] : ''; |
|
| 406 | 406 | return new SSDB_Response($resp[0], $errmsg); |
| 407 | 407 | } |
| 408 | 408 | break; |
| 409 | 409 | case 'multi_exists': |
| 410 | 410 | case 'multi_hexists': |
| 411 | 411 | case 'multi_zexists': |
| 412 | - if($resp[0] == 'ok'){ |
|
| 413 | - if(count($resp) % 2 == 1){ |
|
| 412 | + if ($resp[0] == 'ok') { |
|
| 413 | + if (count($resp) % 2 == 1) { |
|
| 414 | 414 | $data = array(); |
| 415 | - for($i=1; $i<count($resp); $i+=2){ |
|
| 416 | - $data[$resp[$i]] = (bool)$resp[$i + 1]; |
|
| 415 | + for ($i = 1; $i < count($resp); $i += 2) { |
|
| 416 | + $data[$resp[$i]] = (bool) $resp[$i + 1]; |
|
| 417 | 417 | } |
| 418 | 418 | return new SSDB_Response('ok', $data); |
| 419 | - }else{ |
|
| 419 | + } else { |
|
| 420 | 420 | return new SSDB_Response('server_error', 'Invalid response'); |
| 421 | 421 | } |
| 422 | - }else{ |
|
| 423 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 422 | + } else { |
|
| 423 | + $errmsg = isset($resp[1]) ? $resp[1] : ''; |
|
| 424 | 424 | return new SSDB_Response($resp[0], $errmsg); |
| 425 | 425 | } |
| 426 | 426 | break; |
@@ -440,22 +440,22 @@ discard block |
||
| 440 | 440 | case 'multi_zget': |
| 441 | 441 | case 'zpop_front': |
| 442 | 442 | case 'zpop_back': |
| 443 | - if($resp[0] == 'ok'){ |
|
| 444 | - if(count($resp) % 2 == 1){ |
|
| 443 | + if ($resp[0] == 'ok') { |
|
| 444 | + if (count($resp) % 2 == 1) { |
|
| 445 | 445 | $data = array(); |
| 446 | - for($i=1; $i<count($resp); $i+=2){ |
|
| 447 | - if($cmd[0] == 'z'){ |
|
| 446 | + for ($i = 1; $i < count($resp); $i += 2) { |
|
| 447 | + if ($cmd[0] == 'z') { |
|
| 448 | 448 | $data[$resp[$i]] = intval($resp[$i + 1]); |
| 449 | - }else{ |
|
| 449 | + } else { |
|
| 450 | 450 | $data[$resp[$i]] = $resp[$i + 1]; |
| 451 | 451 | } |
| 452 | 452 | } |
| 453 | 453 | return new SSDB_Response('ok', $data); |
| 454 | - }else{ |
|
| 454 | + } else { |
|
| 455 | 455 | return new SSDB_Response('server_error', 'Invalid response'); |
| 456 | 456 | } |
| 457 | - }else{ |
|
| 458 | - $errmsg = isset($resp[1])? $resp[1] : ''; |
|
| 457 | + } else { |
|
| 458 | + $errmsg = isset($resp[1]) ? $resp[1] : ''; |
|
| 459 | 459 | return new SSDB_Response($resp[0], $errmsg); |
| 460 | 460 | } |
| 461 | 461 | break; |
@@ -464,59 +464,59 @@ discard block |
||
| 464 | 464 | } |
| 465 | 465 | return new SSDB_Response('error', 'Unknown command: $cmd'); |
| 466 | 466 | } |
| 467 | - private function send($data){ |
|
| 467 | + private function send($data) { |
|
| 468 | 468 | $ps = array(); |
| 469 | - foreach($data as $p){ |
|
| 469 | + foreach ($data as $p) { |
|
| 470 | 470 | $ps[] = strlen($p); |
| 471 | 471 | $ps[] = $p; |
| 472 | 472 | } |
| 473 | 473 | $s = join("\n", $ps) . "\n\n"; |
| 474 | - if($this->debug){ |
|
| 474 | + if ($this->debug) { |
|
| 475 | 475 | echo '> ' . str_replace(array("\r", "\n"), array('\r', '\n'), $s) . "\n"; |
| 476 | 476 | } |
| 477 | - try{ |
|
| 478 | - while(true){ |
|
| 477 | + try { |
|
| 478 | + while (true) { |
|
| 479 | 479 | $ret = @fwrite($this->sock, $s); |
| 480 | - if($ret === false || $ret === 0){ |
|
| 480 | + if ($ret === false || $ret === 0) { |
|
| 481 | 481 | $this->close(); |
| 482 | 482 | throw new SSDBException('Connection lost'); |
| 483 | 483 | } |
| 484 | 484 | $s = substr($s, $ret); |
| 485 | - if(strlen($s) == 0){ |
|
| 485 | + if (strlen($s) == 0) { |
|
| 486 | 486 | break; |
| 487 | 487 | } |
| 488 | 488 | @fflush($this->sock); |
| 489 | 489 | } |
| 490 | - }catch(Exception $e){ |
|
| 490 | + } catch (Exception $e) { |
|
| 491 | 491 | $this->close(); |
| 492 | 492 | throw new SSDBException($e->getMessage()); |
| 493 | 493 | } |
| 494 | 494 | return $ret; |
| 495 | 495 | } |
| 496 | - private function recv(){ |
|
| 496 | + private function recv() { |
|
| 497 | 497 | $this->step = self::STEP_SIZE; |
| 498 | - while(true){ |
|
| 498 | + while (true) { |
|
| 499 | 499 | $ret = $this->parse(); |
| 500 | - if($ret === null){ |
|
| 501 | - try{ |
|
| 500 | + if ($ret === null) { |
|
| 501 | + try { |
|
| 502 | 502 | $data = @fread($this->sock, 1024 * 1024); |
| 503 | - if($this->debug){ |
|
| 503 | + if ($this->debug) { |
|
| 504 | 504 | echo '< ' . str_replace(array("\r", "\n"), array('\r', '\n'), $data) . "\n"; |
| 505 | 505 | } |
| 506 | - }catch(Exception $e){ |
|
| 506 | + } catch (Exception $e) { |
|
| 507 | 507 | $data = ''; |
| 508 | 508 | } |
| 509 | - if($data === false || $data === ''){ |
|
| 510 | - if(feof($this->sock)){ |
|
| 509 | + if ($data === false || $data === '') { |
|
| 510 | + if (feof($this->sock)) { |
|
| 511 | 511 | $this->close(); |
| 512 | 512 | throw new SSDBException('Connection lost'); |
| 513 | - }else{ |
|
| 513 | + } else { |
|
| 514 | 514 | throw new SSDBTimeoutException('Connection timeout'); |
| 515 | 515 | } |
| 516 | 516 | } |
| 517 | 517 | $this->recv_buf .= $data; |
| 518 | 518 | # echo "read " . strlen($data) . " total: " . strlen($this->recv_buf) . "\n"; |
| 519 | - }else{ |
|
| 519 | + } else { |
|
| 520 | 520 | return $ret; |
| 521 | 521 | } |
| 522 | 522 | } |
@@ -526,24 +526,24 @@ discard block |
||
| 526 | 526 | public $resp = array(); |
| 527 | 527 | public $step; |
| 528 | 528 | public $block_size; |
| 529 | - private function parse(){ |
|
| 529 | + private function parse() { |
|
| 530 | 530 | $spos = 0; |
| 531 | 531 | $epos = 0; |
| 532 | 532 | $buf_size = strlen($this->recv_buf); |
| 533 | 533 | // performance issue for large reponse |
| 534 | 534 | //$this->recv_buf = ltrim($this->recv_buf); |
| 535 | - while(true){ |
|
| 535 | + while (true) { |
|
| 536 | 536 | $spos = $epos; |
| 537 | - if($this->step === self::STEP_SIZE){ |
|
| 537 | + if ($this->step === self::STEP_SIZE) { |
|
| 538 | 538 | $epos = strpos($this->recv_buf, "\n", $spos); |
| 539 | - if($epos === false){ |
|
| 539 | + if ($epos === false) { |
|
| 540 | 540 | break; |
| 541 | 541 | } |
| 542 | 542 | $epos += 1; |
| 543 | 543 | $line = substr($this->recv_buf, $spos, $epos - $spos); |
| 544 | 544 | $spos = $epos; |
| 545 | 545 | $line = trim($line); |
| 546 | - if(strlen($line) == 0){ // head end |
|
| 546 | + if (strlen($line) == 0) { // head end |
|
| 547 | 547 | $this->recv_buf = substr($this->recv_buf, $spos); |
| 548 | 548 | $ret = $this->resp; |
| 549 | 549 | $this->resp = array(); |
@@ -552,11 +552,11 @@ discard block |
||
| 552 | 552 | $this->block_size = intval($line); |
| 553 | 553 | $this->step = self::STEP_DATA; |
| 554 | 554 | } |
| 555 | - if($this->step === self::STEP_DATA){ |
|
| 555 | + if ($this->step === self::STEP_DATA) { |
|
| 556 | 556 | $epos = $spos + $this->block_size; |
| 557 | - if($epos <= $buf_size){ |
|
| 557 | + if ($epos <= $buf_size) { |
|
| 558 | 558 | $n = strpos($this->recv_buf, "\n", $epos); |
| 559 | - if($n !== false){ |
|
| 559 | + if ($n !== false) { |
|
| 560 | 560 | $data = substr($this->recv_buf, $spos, $epos - $spos); |
| 561 | 561 | $this->resp[] = $data; |
| 562 | 562 | $epos = $n + 1; |
@@ -568,7 +568,7 @@ discard block |
||
| 568 | 568 | } |
| 569 | 569 | } |
| 570 | 570 | // packet not ready |
| 571 | - if($spos > 0){ |
|
| 571 | + if ($spos > 0) { |
|
| 572 | 572 | $this->recv_buf = substr($this->recv_buf, $spos); |
| 573 | 573 | } |
| 574 | 574 | return null; |
@@ -34,14 +34,14 @@ discard block |
||
| 34 | 34 | $this->code = $code; |
| 35 | 35 | if($code == 'ok'){ |
| 36 | 36 | $this->data = $data_or_message; |
| 37 | - }else{ |
|
| 37 | + } else{ |
|
| 38 | 38 | $this->message = $data_or_message; |
| 39 | 39 | } |
| 40 | 40 | } |
| 41 | 41 | function __toString(){ |
| 42 | 42 | if($this->code == 'ok'){ |
| 43 | 43 | $s = $this->data === null? '' : json_encode($this->data); |
| 44 | - }else{ |
|
| 44 | + } else{ |
|
| 45 | 45 | $s = $this->message; |
| 46 | 46 | } |
| 47 | 47 | return sprintf('%-13s %12s %s', $this->cmd, $this->code, $s); |
@@ -156,13 +156,13 @@ discard block |
||
| 156 | 156 | try{ |
| 157 | 157 | if($this->send_req($cmd, $params) === false){ |
| 158 | 158 | $resp = new SSDB_Response('error', 'send error'); |
| 159 | - }else{ |
|
| 159 | + } else{ |
|
| 160 | 160 | $resp = $this->recv_resp($cmd, $params); |
| 161 | 161 | } |
| 162 | - }catch(SSDBException $e){ |
|
| 162 | + } catch(SSDBException $e){ |
|
| 163 | 163 | if($this->_easy){ |
| 164 | 164 | throw $e; |
| 165 | - }else{ |
|
| 165 | + } else{ |
|
| 166 | 166 | $resp = new SSDB_Response('error', $e->getMessage()); |
| 167 | 167 | } |
| 168 | 168 | } |
@@ -179,12 +179,12 @@ discard block |
||
| 179 | 179 | if($this->_easy){ |
| 180 | 180 | if($resp->not_found()){ |
| 181 | 181 | return NULL; |
| 182 | - }else if(!$resp->ok() && !is_array($resp->data)){ |
|
| 182 | + } else if(!$resp->ok() && !is_array($resp->data)){ |
|
| 183 | 183 | return false; |
| 184 | - }else{ |
|
| 184 | + } else{ |
|
| 185 | 185 | return $resp->data; |
| 186 | 186 | } |
| 187 | - }else{ |
|
| 187 | + } else{ |
|
| 188 | 188 | $resp->cmd = $cmd; |
| 189 | 189 | return $resp; |
| 190 | 190 | } |
@@ -254,7 +254,7 @@ discard block |
||
| 254 | 254 | foreach($params as $p){ |
| 255 | 255 | if(is_array($p)){ |
| 256 | 256 | $req = array_merge($req, $p); |
| 257 | - }else{ |
|
| 257 | + } else{ |
|
| 258 | 258 | $req[] = $p; |
| 259 | 259 | } |
| 260 | 260 | } |
@@ -264,7 +264,7 @@ discard block |
||
| 264 | 264 | $resp = $this->recv(); |
| 265 | 265 | if($resp === false){ |
| 266 | 266 | return new SSDB_Response('error', 'Unknown error'); |
| 267 | - }else if(!$resp){ |
|
| 267 | + } else if(!$resp){ |
|
| 268 | 268 | return new SSDB_Response('disconnected', 'Connection closed'); |
| 269 | 269 | } |
| 270 | 270 | if($resp[0] == 'noauth'){ |
@@ -322,7 +322,7 @@ discard block |
||
| 322 | 322 | if($resp[0] == 'ok'){ |
| 323 | 323 | $val = isset($resp[1])? intval($resp[1]) : 0; |
| 324 | 324 | return new SSDB_Response($resp[0], $val); |
| 325 | - }else{ |
|
| 325 | + } else{ |
|
| 326 | 326 | $errmsg = isset($resp[1])? $resp[1] : ''; |
| 327 | 327 | return new SSDB_Response($resp[0], $errmsg); |
| 328 | 328 | } |
@@ -330,7 +330,7 @@ discard block |
||
| 330 | 330 | if($resp[0] == 'ok'){ |
| 331 | 331 | $val = isset($resp[1])? floatval($resp[1]) : (float)0; |
| 332 | 332 | return new SSDB_Response($resp[0], $val); |
| 333 | - }else{ |
|
| 333 | + } else{ |
|
| 334 | 334 | $errmsg = isset($resp[1])? $resp[1] : ''; |
| 335 | 335 | return new SSDB_Response($resp[0], $errmsg); |
| 336 | 336 | } |
@@ -344,10 +344,10 @@ discard block |
||
| 344 | 344 | if($resp[0] == 'ok'){ |
| 345 | 345 | if(count($resp) == 2){ |
| 346 | 346 | return new SSDB_Response('ok', $resp[1]); |
| 347 | - }else{ |
|
| 347 | + } else{ |
|
| 348 | 348 | return new SSDB_Response('server_error', 'Invalid response'); |
| 349 | 349 | } |
| 350 | - }else{ |
|
| 350 | + } else{ |
|
| 351 | 351 | $errmsg = isset($resp[1])? $resp[1] : ''; |
| 352 | 352 | return new SSDB_Response($resp[0], $errmsg); |
| 353 | 353 | } |
@@ -363,14 +363,14 @@ discard block |
||
| 363 | 363 | if($size <= 1){ |
| 364 | 364 | if(count($resp) == 2){ |
| 365 | 365 | return new SSDB_Response('ok', $resp[1]); |
| 366 | - }else{ |
|
| 366 | + } else{ |
|
| 367 | 367 | return new SSDB_Response('server_error', 'Invalid response'); |
| 368 | 368 | } |
| 369 | - }else{ |
|
| 369 | + } else{ |
|
| 370 | 370 | $data = array_slice($resp, 1); |
| 371 | 371 | return new SSDB_Response('ok', $data); |
| 372 | 372 | } |
| 373 | - }else{ |
|
| 373 | + } else{ |
|
| 374 | 374 | $errmsg = isset($resp[1])? $resp[1] : ''; |
| 375 | 375 | return new SSDB_Response($resp[0], $errmsg); |
| 376 | 376 | } |
@@ -387,7 +387,7 @@ discard block |
||
| 387 | 387 | $data = array_slice($resp, 1); |
| 388 | 388 | } |
| 389 | 389 | return new SSDB_Response($resp[0], $data); |
| 390 | - }else{ |
|
| 390 | + } else{ |
|
| 391 | 391 | $errmsg = isset($resp[1])? $resp[1] : ''; |
| 392 | 392 | return new SSDB_Response($resp[0], $errmsg); |
| 393 | 393 | } |
@@ -398,10 +398,10 @@ discard block |
||
| 398 | 398 | if($resp[0] == 'ok'){ |
| 399 | 399 | if(count($resp) == 2){ |
| 400 | 400 | return new SSDB_Response('ok', (bool)$resp[1]); |
| 401 | - }else{ |
|
| 401 | + } else{ |
|
| 402 | 402 | return new SSDB_Response('server_error', 'Invalid response'); |
| 403 | 403 | } |
| 404 | - }else{ |
|
| 404 | + } else{ |
|
| 405 | 405 | $errmsg = isset($resp[1])? $resp[1] : ''; |
| 406 | 406 | return new SSDB_Response($resp[0], $errmsg); |
| 407 | 407 | } |
@@ -416,10 +416,10 @@ discard block |
||
| 416 | 416 | $data[$resp[$i]] = (bool)$resp[$i + 1]; |
| 417 | 417 | } |
| 418 | 418 | return new SSDB_Response('ok', $data); |
| 419 | - }else{ |
|
| 419 | + } else{ |
|
| 420 | 420 | return new SSDB_Response('server_error', 'Invalid response'); |
| 421 | 421 | } |
| 422 | - }else{ |
|
| 422 | + } else{ |
|
| 423 | 423 | $errmsg = isset($resp[1])? $resp[1] : ''; |
| 424 | 424 | return new SSDB_Response($resp[0], $errmsg); |
| 425 | 425 | } |
@@ -446,15 +446,15 @@ discard block |
||
| 446 | 446 | for($i=1; $i<count($resp); $i+=2){ |
| 447 | 447 | if($cmd[0] == 'z'){ |
| 448 | 448 | $data[$resp[$i]] = intval($resp[$i + 1]); |
| 449 | - }else{ |
|
| 449 | + } else{ |
|
| 450 | 450 | $data[$resp[$i]] = $resp[$i + 1]; |
| 451 | 451 | } |
| 452 | 452 | } |
| 453 | 453 | return new SSDB_Response('ok', $data); |
| 454 | - }else{ |
|
| 454 | + } else{ |
|
| 455 | 455 | return new SSDB_Response('server_error', 'Invalid response'); |
| 456 | 456 | } |
| 457 | - }else{ |
|
| 457 | + } else{ |
|
| 458 | 458 | $errmsg = isset($resp[1])? $resp[1] : ''; |
| 459 | 459 | return new SSDB_Response($resp[0], $errmsg); |
| 460 | 460 | } |
@@ -487,7 +487,7 @@ discard block |
||
| 487 | 487 | } |
| 488 | 488 | @fflush($this->sock); |
| 489 | 489 | } |
| 490 | - }catch(Exception $e){ |
|
| 490 | + } catch(Exception $e){ |
|
| 491 | 491 | $this->close(); |
| 492 | 492 | throw new SSDBException($e->getMessage()); |
| 493 | 493 | } |
@@ -503,20 +503,20 @@ discard block |
||
| 503 | 503 | if($this->debug){ |
| 504 | 504 | echo '< ' . str_replace(array("\r", "\n"), array('\r', '\n'), $data) . "\n"; |
| 505 | 505 | } |
| 506 | - }catch(Exception $e){ |
|
| 506 | + } catch(Exception $e){ |
|
| 507 | 507 | $data = ''; |
| 508 | 508 | } |
| 509 | 509 | if($data === false || $data === ''){ |
| 510 | 510 | if(feof($this->sock)){ |
| 511 | 511 | $this->close(); |
| 512 | 512 | throw new SSDBException('Connection lost'); |
| 513 | - }else{ |
|
| 513 | + } else{ |
|
| 514 | 514 | throw new SSDBTimeoutException('Connection timeout'); |
| 515 | 515 | } |
| 516 | 516 | } |
| 517 | 517 | $this->recv_buf .= $data; |
| 518 | 518 | # echo "read " . strlen($data) . " total: " . strlen($this->recv_buf) . "\n"; |
| 519 | - }else{ |
|
| 519 | + } else{ |
|
| 520 | 520 | return $ret; |
| 521 | 521 | } |
| 522 | 522 | } |
@@ -541,7 +541,7 @@ discard block |
||
| 541 | 541 | |
| 542 | 542 | |
| 543 | 543 | /** |
| 544 | - * @return mixed |
|
| 544 | + * @return DriverAbstract |
|
| 545 | 545 | */ |
| 546 | 546 | protected function backup() |
| 547 | 547 | { |
@@ -549,7 +549,7 @@ discard block |
||
| 549 | 549 | } |
| 550 | 550 | |
| 551 | 551 | /** |
| 552 | - * @param $name |
|
| 552 | + * @param string $name |
|
| 553 | 553 | * @return void |
| 554 | 554 | */ |
| 555 | 555 | protected function required_extension($name) |
@@ -559,7 +559,7 @@ discard block |
||
| 559 | 559 | |
| 560 | 560 | |
| 561 | 561 | /** |
| 562 | - * @param $file |
|
| 562 | + * @param string $file |
|
| 563 | 563 | * @return string |
| 564 | 564 | * @throws \Exception |
| 565 | 565 | */ |
@@ -599,7 +599,7 @@ discard block |
||
| 599 | 599 | |
| 600 | 600 | /** |
| 601 | 601 | * Object for Files & SQLite |
| 602 | - * @param $data |
|
| 602 | + * @param string $data |
|
| 603 | 603 | * @return string |
| 604 | 604 | */ |
| 605 | 605 | protected function encode($data) |
@@ -678,7 +678,7 @@ discard block |
||
| 678 | 678 | * @param integer $time |
| 679 | 679 | * @param array $tags |
| 680 | 680 | * @param array $option | $option = array("tags" => array("a","b","c") |
| 681 | - * @return mixed |
|
| 681 | + * @return boolean|null |
|
| 682 | 682 | */ |
| 683 | 683 | public function setTags($keyword, $value = '', $time = 0, $tags = array(), $option = array()) |
| 684 | 684 | { |
@@ -689,6 +689,9 @@ discard block |
||
| 689 | 689 | return $this->set($keyword, $value, $time, $option); |
| 690 | 690 | } |
| 691 | 691 | |
| 692 | + /** |
|
| 693 | + * @param integer $time |
|
| 694 | + */ |
|
| 692 | 695 | protected function _handleTags($keyword, $time, $tags) |
| 693 | 696 | { |
| 694 | 697 | foreach ($tags as $tag) { |
@@ -747,7 +750,7 @@ discard block |
||
| 747 | 750 | * @param array $tags | array("a","b","c") |
| 748 | 751 | * @param int $time |
| 749 | 752 | * @param array $options |
| 750 | - * @return mixed |
|
| 753 | + * @return boolean |
|
| 751 | 754 | * @internal param array $option | $option = array("tags" => array("a","b","c") |
| 752 | 755 | */ |
| 753 | 756 | public function touchTags($tags = array(), $time = 300, $options = array()) |
@@ -767,7 +770,7 @@ discard block |
||
| 767 | 770 | /** |
| 768 | 771 | * @param array $tags | array("a","b","c") |
| 769 | 772 | * @param array $option | $option = array("tags" => array("a","b","c") |
| 770 | - * @return mixed |
|
| 773 | + * @return boolean |
|
| 771 | 774 | */ |
| 772 | 775 | public function deleteTags($tags = array(), $option = array()) |
| 773 | 776 | { |
@@ -788,7 +791,7 @@ discard block |
||
| 788 | 791 | * @param array $tags | array("a","b","c") |
| 789 | 792 | * @param integer |
| 790 | 793 | * @param array $option | $option = array("tags" => array("a","b","c") |
| 791 | - * @return mixed |
|
| 794 | + * @return boolean |
|
| 792 | 795 | */ |
| 793 | 796 | public function incrementTags($tags = array(), $step = 1, $option = array()) |
| 794 | 797 | { |
@@ -808,7 +811,7 @@ discard block |
||
| 808 | 811 | * @param array $tags | array("a","b","c") |
| 809 | 812 | * @param integer |
| 810 | 813 | * @param array $option | $option = array("tags" => array("a","b","c") |
| 811 | - * @return mixed |
|
| 814 | + * @return boolean |
|
| 812 | 815 | */ |
| 813 | 816 | public function decrementTags($tags = array(), $step = 1, $option = array()) |
| 814 | 817 | { |
@@ -836,6 +839,9 @@ discard block |
||
| 836 | 839 | */ |
| 837 | 840 | } |
| 838 | 841 | |
| 842 | + /** |
|
| 843 | + * @param string $index |
|
| 844 | + */ |
|
| 839 | 845 | public function _hit($index, $step = 1) |
| 840 | 846 | { |
| 841 | 847 | $instance = $this->config[ 'instance' ]; |
@@ -99,11 +99,11 @@ discard block |
||
| 99 | 99 | return false; |
| 100 | 100 | } |
| 101 | 101 | $object = array( |
| 102 | - "value" => $value, |
|
| 103 | - "write_time" => time(), |
|
| 104 | - "expired_in" => $time, |
|
| 105 | - "expired_time" => time() + (int)$time, |
|
| 106 | - "size" => (is_array($value) || is_object($value)) ? strlen(serialize($value)) : strlen((String)$value), |
|
| 102 | + "value" => $value, |
|
| 103 | + "write_time" => time(), |
|
| 104 | + "expired_in" => $time, |
|
| 105 | + "expired_time" => time() + (int)$time, |
|
| 106 | + "size" => (is_array($value) || is_object($value)) ? strlen(serialize($value)) : strlen((String)$value), |
|
| 107 | 107 | ); |
| 108 | 108 | |
| 109 | 109 | // handle search |
@@ -376,7 +376,7 @@ discard block |
||
| 376 | 376 | { |
| 377 | 377 | foreach ($list as $array) { |
| 378 | 378 | $this->set($array[ 0 ], isset($array[ 1 ]) ? $array[ 1 ] : 0, |
| 379 | - isset($array[ 2 ]) ? $array[ 2 ] : array()); |
|
| 379 | + isset($array[ 2 ]) ? $array[ 2 ] : array()); |
|
| 380 | 380 | } |
| 381 | 381 | } |
| 382 | 382 | |
@@ -390,7 +390,7 @@ discard block |
||
| 390 | 390 | foreach ($list as $array) { |
| 391 | 391 | $name = $array[ 0 ]; |
| 392 | 392 | $res[ $name ] = $this->get($name, |
| 393 | - isset($array[ 1 ]) ? $array[ 1 ] : array()); |
|
| 393 | + isset($array[ 1 ]) ? $array[ 1 ] : array()); |
|
| 394 | 394 | } |
| 395 | 395 | return $res; |
| 396 | 396 | } |
@@ -405,7 +405,7 @@ discard block |
||
| 405 | 405 | foreach ($list as $array) { |
| 406 | 406 | $name = $array[ 0 ]; |
| 407 | 407 | $res[ $name ] = $this->getInfo($name, |
| 408 | - isset($array[ 1 ]) ? $array[ 1 ] : array()); |
|
| 408 | + isset($array[ 1 ]) ? $array[ 1 ] : array()); |
|
| 409 | 409 | } |
| 410 | 410 | return $res; |
| 411 | 411 | } |
@@ -447,7 +447,7 @@ discard block |
||
| 447 | 447 | foreach ($list as $array) { |
| 448 | 448 | $name = $array[ 0 ]; |
| 449 | 449 | $res[ $name ] = $this->increment($name, $array[ 1 ], |
| 450 | - isset($array[ 2 ]) ? $array[ 2 ] : array()); |
|
| 450 | + isset($array[ 2 ]) ? $array[ 2 ] : array()); |
|
| 451 | 451 | } |
| 452 | 452 | return $res; |
| 453 | 453 | } |
@@ -462,7 +462,7 @@ discard block |
||
| 462 | 462 | foreach ($list as $array) { |
| 463 | 463 | $name = $array[ 0 ]; |
| 464 | 464 | $res[ $name ] = $this->decrement($name, $array[ 1 ], |
| 465 | - isset($array[ 2 ]) ? $array[ 2 ] : array()); |
|
| 465 | + isset($array[ 2 ]) ? $array[ 2 ] : array()); |
|
| 466 | 466 | } |
| 467 | 467 | return $res; |
| 468 | 468 | } |
@@ -477,7 +477,7 @@ discard block |
||
| 477 | 477 | foreach ($list as $array) { |
| 478 | 478 | $name = $array[ 0 ]; |
| 479 | 479 | $res[ $name ] = $this->touch($name, $array[ 1 ], |
| 480 | - isset($array[ 2 ]) ? $array[ 2 ] : array()); |
|
| 480 | + isset($array[ 2 ]) ? $array[ 2 ] : array()); |
|
| 481 | 481 | } |
| 482 | 482 | return $res; |
| 483 | 483 | } |
@@ -528,7 +528,7 @@ discard block |
||
| 528 | 528 | { |
| 529 | 529 | if (isset($v[ 1 ]) && is_scalar($v[ 1 ])) { |
| 530 | 530 | return $this->set($name, $v[ 0 ], $v[ 1 ], |
| 531 | - isset($v[ 2 ]) ? $v[ 2 ] : array()); |
|
| 531 | + isset($v[ 2 ]) ? $v[ 2 ] : array()); |
|
| 532 | 532 | } else { |
| 533 | 533 | throw new phpFastCacheDriverException("Example ->$name = array('VALUE', 300);", 98); |
| 534 | 534 | } |
@@ -665,10 +665,10 @@ discard block |
||
| 665 | 665 | protected function _tagCaching() |
| 666 | 666 | { |
| 667 | 667 | return CacheManager::Sqlite( |
| 668 | - array( |
|
| 668 | + array( |
|
| 669 | 669 | "path" => $this->config[ 'path' ], |
| 670 | 670 | "cache_method" => 3, |
| 671 | - ) |
|
| 671 | + ) |
|
| 672 | 672 | ); |
| 673 | 673 | } |
| 674 | 674 | |
@@ -53,8 +53,8 @@ discard block |
||
| 53 | 53 | public function __destruct() |
| 54 | 54 | { |
| 55 | 55 | // clean up the memory and don't want for PHP clean for caching method "phpfastcache" |
| 56 | - if (isset($this->config[ 'instance' ]) && (int)$this->config[ 'cache_method' ] === 3) { |
|
| 57 | - CacheManager::cleanCachingMethod($this->config[ 'instance' ]); |
|
| 56 | + if (isset($this->config['instance']) && (int) $this->config['cache_method'] === 3) { |
|
| 57 | + CacheManager::cleanCachingMethod($this->config['instance']); |
|
| 58 | 58 | } |
| 59 | 59 | } |
| 60 | 60 | |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | * Infinity Time |
| 84 | 84 | * Khoa. B |
| 85 | 85 | */ |
| 86 | - if ((int)$time <= 0) { |
|
| 86 | + if ((int) $time <= 0) { |
|
| 87 | 87 | /** |
| 88 | 88 | * 5 years, however memcached or memory cached will gone when u restart it |
| 89 | 89 | * just recommended for sqlite. files |
@@ -102,24 +102,24 @@ discard block |
||
| 102 | 102 | "value" => $value, |
| 103 | 103 | "write_time" => time(), |
| 104 | 104 | "expired_in" => $time, |
| 105 | - "expired_time" => time() + (int)$time, |
|
| 106 | - "size" => (is_array($value) || is_object($value)) ? strlen(serialize($value)) : strlen((String)$value), |
|
| 105 | + "expired_time" => time() + (int) $time, |
|
| 106 | + "size" => (is_array($value) || is_object($value)) ? strlen(serialize($value)) : strlen((String) $value), |
|
| 107 | 107 | ); |
| 108 | 108 | |
| 109 | 109 | // handle search |
| 110 | - if (isset($this->config[ 'allow_search' ]) && $this->config[ 'allow_search' ] == true) { |
|
| 111 | - $option[ 'tags' ][] = "search"; |
|
| 110 | + if (isset($this->config['allow_search']) && $this->config['allow_search'] == true) { |
|
| 111 | + $option['tags'][] = "search"; |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | // handle tags |
| 115 | - if (isset($option[ 'tags' ])) { |
|
| 116 | - $this->_handleTags($keyword, $time, $option[ 'tags' ]); |
|
| 115 | + if (isset($option['tags'])) { |
|
| 116 | + $this->_handleTags($keyword, $time, $option['tags']); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | // handle method |
| 120 | - if ((int)$this->config[ 'cache_method' ] > 1 && isset($object[ 'size' ]) && (int)$object[ 'size' ] <= (int)$this->config[ 'limited_memory_each_object' ]) { |
|
| 121 | - CacheManager::$memory[ $this->config[ 'instance' ] ][ $keyword ] = $object; |
|
| 122 | - if (in_array((int)$this->config[ 'cache_method' ], array(3, 4))) { |
|
| 120 | + if ((int) $this->config['cache_method'] > 1 && isset($object['size']) && (int) $object['size'] <= (int) $this->config['limited_memory_each_object']) { |
|
| 121 | + CacheManager::$memory[$this->config['instance']][$keyword] = $object; |
|
| 122 | + if (in_array((int) $this->config['cache_method'], array(3, 4))) { |
|
| 123 | 123 | return true; |
| 124 | 124 | } |
| 125 | 125 | } |
@@ -145,9 +145,9 @@ discard block |
||
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | // handle method |
| 148 | - if ((int)$this->config[ 'cache_method' ] > 1) { |
|
| 149 | - if (isset(CacheManager::$memory[ $this->config[ 'instance' ] ][ $keyword ])) { |
|
| 150 | - $object = CacheManager::$memory[ $this->config[ 'instance' ] ][ $keyword ]; |
|
| 148 | + if ((int) $this->config['cache_method'] > 1) { |
|
| 149 | + if (isset(CacheManager::$memory[$this->config['instance']][$keyword])) { |
|
| 150 | + $object = CacheManager::$memory[$this->config['instance']][$keyword]; |
|
| 151 | 151 | } |
| 152 | 152 | } |
| 153 | 153 | |
@@ -156,8 +156,8 @@ discard block |
||
| 156 | 156 | $object = $this->driver_get($keyword, $option); |
| 157 | 157 | |
| 158 | 158 | // handle method |
| 159 | - if ((int)$this->config[ 'cache_method' ] > 1 && isset($object[ 'size' ]) && (int)$object[ 'size' ] <= (int)$this->config[ 'limited_memory_each_object' ]) { |
|
| 160 | - CacheManager::$memory[ $this->config[ 'instance' ] ][ $keyword ] = $object; |
|
| 159 | + if ((int) $this->config['cache_method'] > 1 && isset($object['size']) && (int) $object['size'] <= (int) $this->config['limited_memory_each_object']) { |
|
| 160 | + CacheManager::$memory[$this->config['instance']][$keyword] = $object; |
|
| 161 | 161 | } |
| 162 | 162 | // end handle method |
| 163 | 163 | } |
@@ -166,8 +166,8 @@ discard block |
||
| 166 | 166 | return null; |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | - $value = isset($object[ 'value' ]) ? $object[ 'value' ] : null; |
|
| 170 | - return isset($option[ 'all_keys' ]) && $option[ 'all_keys' ] ? $object : $value; |
|
| 169 | + $value = isset($object['value']) ? $object['value'] : null; |
|
| 170 | + return isset($option['all_keys']) && $option['all_keys'] ? $object : $value; |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /** |
@@ -177,9 +177,9 @@ discard block |
||
| 177 | 177 | */ |
| 178 | 178 | public function getInfo($keyword, $option = array()) |
| 179 | 179 | { |
| 180 | - if ((int)$this->config[ 'cache_method' ] > 1) { |
|
| 181 | - if (isset(CacheManager::$memory[ $this->config[ 'instance' ] ][ $keyword ])) { |
|
| 182 | - $object = CacheManager::$memory[ $this->config[ 'instance' ] ][ $keyword ]; |
|
| 180 | + if ((int) $this->config['cache_method'] > 1) { |
|
| 181 | + if (isset(CacheManager::$memory[$this->config['instance']][$keyword])) { |
|
| 182 | + $object = CacheManager::$memory[$this->config['instance']][$keyword]; |
|
| 183 | 183 | } |
| 184 | 184 | } |
| 185 | 185 | if (!isset($object)) { |
@@ -199,9 +199,9 @@ discard block |
||
| 199 | 199 | public function delete($keyword, array $option = array()) |
| 200 | 200 | { |
| 201 | 201 | // handle method |
| 202 | - if ((int)$this->config[ 'cache_method' ] > 1) { |
|
| 202 | + if ((int) $this->config['cache_method'] > 1) { |
|
| 203 | 203 | // use memory |
| 204 | - unset(CacheManager::$memory[ $this->config[ 'instance' ] ][ $keyword ]); |
|
| 204 | + unset(CacheManager::$memory[$this->config['instance']][$keyword]); |
|
| 205 | 205 | } |
| 206 | 206 | // end handle method |
| 207 | 207 | return $this->driver_delete($keyword, $option); |
@@ -223,9 +223,9 @@ discard block |
||
| 223 | 223 | public function clean(array $option = array()) |
| 224 | 224 | { |
| 225 | 225 | // handle method |
| 226 | - if ((int)$this->config[ 'cache_method' ] > 1) { |
|
| 226 | + if ((int) $this->config['cache_method'] > 1) { |
|
| 227 | 227 | // use memory |
| 228 | - CacheManager::$memory[ $this->config[ 'instance' ] ] = array(); |
|
| 228 | + CacheManager::$memory[$this->config['instance']] = array(); |
|
| 229 | 229 | } |
| 230 | 230 | // end handle method |
| 231 | 231 | return $this->driver_clean($option); |
@@ -259,12 +259,12 @@ discard block |
||
| 259 | 259 | */ |
| 260 | 260 | public function search($query_as_regex_or_string, $search_in_value = false) |
| 261 | 261 | { |
| 262 | - if ($this->config[ 'allow_search' ] != true) { |
|
| 262 | + if ($this->config['allow_search'] != true) { |
|
| 263 | 263 | throw new phpFastCacheDriverException('Please setup allow_search = true'); |
| 264 | 264 | } else { |
| 265 | 265 | $list = $this->getTags("search", $search_in_value); |
| 266 | 266 | $tmp = explode("/", $query_as_regex_or_string, 2); |
| 267 | - $regex = isset($tmp[ 1 ]) ? true : false; |
|
| 267 | + $regex = isset($tmp[1]) ? true : false; |
|
| 268 | 268 | $return_list = array(); |
| 269 | 269 | foreach ($list as $tag) { |
| 270 | 270 | foreach ($tag as $keyword => $value) { |
@@ -275,13 +275,13 @@ discard block |
||
| 275 | 275 | |
| 276 | 276 | if ($regex == true && $gotcha == false) { // look in keyword |
| 277 | 277 | if (preg_match($query_as_regex_or_string, $keyword)) { |
| 278 | - $return_list[ $keyword ] = $value; |
|
| 278 | + $return_list[$keyword] = $value; |
|
| 279 | 279 | $gotcha = true; |
| 280 | 280 | } |
| 281 | 281 | } |
| 282 | 282 | if ($gotcha == false) { |
| 283 | 283 | if (strpos($keyword, $query_as_regex_or_string) !== false) { |
| 284 | - $return_list[ $keyword ] = $value; |
|
| 284 | + $return_list[$keyword] = $value; |
|
| 285 | 285 | $gotcha = true; |
| 286 | 286 | } |
| 287 | 287 | } |
@@ -289,13 +289,13 @@ discard block |
||
| 289 | 289 | if ($search_in_value == true && $gotcha == false) { // value search |
| 290 | 290 | if ($regex == true && $gotcha == false) { |
| 291 | 291 | if (preg_match($query_as_regex_or_string, $value)) { |
| 292 | - $return_list[ $keyword ] = $value; |
|
| 292 | + $return_list[$keyword] = $value; |
|
| 293 | 293 | $gotcha = true; |
| 294 | 294 | } |
| 295 | 295 | } |
| 296 | 296 | if ($gotcha == false) { |
| 297 | 297 | if (strpos($value, $query_as_regex_or_string) !== false) { |
| 298 | - $return_list[ $keyword ] = $value; |
|
| 298 | + $return_list[$keyword] = $value; |
|
| 299 | 299 | $gotcha = true; |
| 300 | 300 | } |
| 301 | 301 | } |
@@ -318,8 +318,8 @@ discard block |
||
| 318 | 318 | if ($object == null) { |
| 319 | 319 | return false; |
| 320 | 320 | } else { |
| 321 | - $value = (int)$object[ 'value' ] + (int)$step; |
|
| 322 | - $time = $object[ 'expired_time' ] - time(); |
|
| 321 | + $value = (int) $object['value'] + (int) $step; |
|
| 322 | + $time = $object['expired_time'] - time(); |
|
| 323 | 323 | $this->set($keyword, $value, $time, $option); |
| 324 | 324 | return true; |
| 325 | 325 | } |
@@ -337,8 +337,8 @@ discard block |
||
| 337 | 337 | if ($object == null) { |
| 338 | 338 | return false; |
| 339 | 339 | } else { |
| 340 | - $value = (int)$object[ 'value' ] - (int)$step; |
|
| 341 | - $time = $object[ 'expired_time' ] - time(); |
|
| 340 | + $value = (int) $object['value'] - (int) $step; |
|
| 341 | + $time = $object['expired_time'] - time(); |
|
| 342 | 342 | $this->set($keyword, $value, $time, $option); |
| 343 | 343 | return true; |
| 344 | 344 | } |
@@ -357,8 +357,8 @@ discard block |
||
| 357 | 357 | if ($object == null) { |
| 358 | 358 | return false; |
| 359 | 359 | } else { |
| 360 | - $value = $object[ 'value' ]; |
|
| 361 | - $time = $object[ 'expired_time' ] - time() + $time; |
|
| 360 | + $value = $object['value']; |
|
| 361 | + $time = $object['expired_time'] - time() + $time; |
|
| 362 | 362 | $this->set($keyword, $value, $time, $option); |
| 363 | 363 | return true; |
| 364 | 364 | } |
@@ -375,8 +375,8 @@ discard block |
||
| 375 | 375 | public function setMulti(array $list = array()) |
| 376 | 376 | { |
| 377 | 377 | foreach ($list as $array) { |
| 378 | - $this->set($array[ 0 ], isset($array[ 1 ]) ? $array[ 1 ] : 0, |
|
| 379 | - isset($array[ 2 ]) ? $array[ 2 ] : array()); |
|
| 378 | + $this->set($array[0], isset($array[1]) ? $array[1] : 0, |
|
| 379 | + isset($array[2]) ? $array[2] : array()); |
|
| 380 | 380 | } |
| 381 | 381 | } |
| 382 | 382 | |
@@ -388,9 +388,9 @@ discard block |
||
| 388 | 388 | { |
| 389 | 389 | $res = array(); |
| 390 | 390 | foreach ($list as $array) { |
| 391 | - $name = $array[ 0 ]; |
|
| 392 | - $res[ $name ] = $this->get($name, |
|
| 393 | - isset($array[ 1 ]) ? $array[ 1 ] : array()); |
|
| 391 | + $name = $array[0]; |
|
| 392 | + $res[$name] = $this->get($name, |
|
| 393 | + isset($array[1]) ? $array[1] : array()); |
|
| 394 | 394 | } |
| 395 | 395 | return $res; |
| 396 | 396 | } |
@@ -403,9 +403,9 @@ discard block |
||
| 403 | 403 | { |
| 404 | 404 | $res = array(); |
| 405 | 405 | foreach ($list as $array) { |
| 406 | - $name = $array[ 0 ]; |
|
| 407 | - $res[ $name ] = $this->getInfo($name, |
|
| 408 | - isset($array[ 1 ]) ? $array[ 1 ] : array()); |
|
| 406 | + $name = $array[0]; |
|
| 407 | + $res[$name] = $this->getInfo($name, |
|
| 408 | + isset($array[1]) ? $array[1] : array()); |
|
| 409 | 409 | } |
| 410 | 410 | return $res; |
| 411 | 411 | } |
@@ -418,7 +418,7 @@ discard block |
||
| 418 | 418 | { |
| 419 | 419 | foreach ($list as $item) { |
| 420 | 420 | if (is_array($item) && count($item) === 2) { |
| 421 | - $this->delete($item[ 0 ], $item[ 1 ]); |
|
| 421 | + $this->delete($item[0], $item[1]); |
|
| 422 | 422 | } |
| 423 | 423 | } |
| 424 | 424 | } |
@@ -431,8 +431,8 @@ discard block |
||
| 431 | 431 | { |
| 432 | 432 | $res = array(); |
| 433 | 433 | foreach ($list as $array) { |
| 434 | - $name = $array[ 0 ]; |
|
| 435 | - $res[ $name ] = $this->isExisting($name); |
|
| 434 | + $name = $array[0]; |
|
| 435 | + $res[$name] = $this->isExisting($name); |
|
| 436 | 436 | } |
| 437 | 437 | return $res; |
| 438 | 438 | } |
@@ -445,9 +445,9 @@ discard block |
||
| 445 | 445 | { |
| 446 | 446 | $res = array(); |
| 447 | 447 | foreach ($list as $array) { |
| 448 | - $name = $array[ 0 ]; |
|
| 449 | - $res[ $name ] = $this->increment($name, $array[ 1 ], |
|
| 450 | - isset($array[ 2 ]) ? $array[ 2 ] : array()); |
|
| 448 | + $name = $array[0]; |
|
| 449 | + $res[$name] = $this->increment($name, $array[1], |
|
| 450 | + isset($array[2]) ? $array[2] : array()); |
|
| 451 | 451 | } |
| 452 | 452 | return $res; |
| 453 | 453 | } |
@@ -460,9 +460,9 @@ discard block |
||
| 460 | 460 | { |
| 461 | 461 | $res = array(); |
| 462 | 462 | foreach ($list as $array) { |
| 463 | - $name = $array[ 0 ]; |
|
| 464 | - $res[ $name ] = $this->decrement($name, $array[ 1 ], |
|
| 465 | - isset($array[ 2 ]) ? $array[ 2 ] : array()); |
|
| 463 | + $name = $array[0]; |
|
| 464 | + $res[$name] = $this->decrement($name, $array[1], |
|
| 465 | + isset($array[2]) ? $array[2] : array()); |
|
| 466 | 466 | } |
| 467 | 467 | return $res; |
| 468 | 468 | } |
@@ -475,9 +475,9 @@ discard block |
||
| 475 | 475 | { |
| 476 | 476 | $res = array(); |
| 477 | 477 | foreach ($list as $array) { |
| 478 | - $name = $array[ 0 ]; |
|
| 479 | - $res[ $name ] = $this->touch($name, $array[ 1 ], |
|
| 480 | - isset($array[ 2 ]) ? $array[ 2 ] : array()); |
|
| 478 | + $name = $array[0]; |
|
| 479 | + $res[$name] = $this->touch($name, $array[1], |
|
| 480 | + isset($array[2]) ? $array[2] : array()); |
|
| 481 | 481 | } |
| 482 | 482 | return $res; |
| 483 | 483 | } |
@@ -494,7 +494,7 @@ discard block |
||
| 494 | 494 | if (is_array($config_name)) { |
| 495 | 495 | $this->config = array_merge($this->config, $config_name); |
| 496 | 496 | } else { |
| 497 | - $this->config[ $config_name ] = $value; |
|
| 497 | + $this->config[$config_name] = $value; |
|
| 498 | 498 | } |
| 499 | 499 | } |
| 500 | 500 | |
@@ -526,9 +526,9 @@ discard block |
||
| 526 | 526 | */ |
| 527 | 527 | public function __set($name, $v) |
| 528 | 528 | { |
| 529 | - if (isset($v[ 1 ]) && is_scalar($v[ 1 ])) { |
|
| 530 | - return $this->set($name, $v[ 0 ], $v[ 1 ], |
|
| 531 | - isset($v[ 2 ]) ? $v[ 2 ] : array()); |
|
| 529 | + if (isset($v[1]) && is_scalar($v[1])) { |
|
| 530 | + return $this->set($name, $v[0], $v[1], |
|
| 531 | + isset($v[2]) ? $v[2] : array()); |
|
| 532 | 532 | } else { |
| 533 | 533 | throw new phpFastCacheDriverException("Example ->$name = array('VALUE', 300);", 98); |
| 534 | 534 | } |
@@ -545,7 +545,7 @@ discard block |
||
| 545 | 545 | */ |
| 546 | 546 | protected function backup() |
| 547 | 547 | { |
| 548 | - return phpFastCache(phpFastCache::$config[ 'fallback' ]); |
|
| 548 | + return phpFastCache(phpFastCache::$config['fallback']); |
|
| 549 | 549 | } |
| 550 | 550 | |
| 551 | 551 | /** |
@@ -666,7 +666,7 @@ discard block |
||
| 666 | 666 | { |
| 667 | 667 | return CacheManager::Sqlite( |
| 668 | 668 | array( |
| 669 | - "path" => $this->config[ 'path' ], |
|
| 669 | + "path" => $this->config['path'], |
|
| 670 | 670 | "cache_method" => 3, |
| 671 | 671 | ) |
| 672 | 672 | ); |
@@ -685,7 +685,7 @@ discard block |
||
| 685 | 685 | if (!is_array($tags)) { |
| 686 | 686 | $tags = array($tags); |
| 687 | 687 | } |
| 688 | - $option[ 'tags' ] = $tags; |
|
| 688 | + $option['tags'] = $tags; |
|
| 689 | 689 | return $this->set($keyword, $value, $time, $option); |
| 690 | 690 | } |
| 691 | 691 | |
@@ -696,7 +696,7 @@ discard block |
||
| 696 | 696 | if (is_null($list)) { |
| 697 | 697 | $list = array(); |
| 698 | 698 | } |
| 699 | - $list[ $keyword ] = time() + $time; |
|
| 699 | + $list[$keyword] = time() + $time; |
|
| 700 | 700 | $this->_tagCaching()->set($this->_getTagName($tag), $list, 3600 * 24 * 30); |
| 701 | 701 | } |
| 702 | 702 | } |
@@ -724,21 +724,21 @@ discard block |
||
| 724 | 724 | } |
| 725 | 725 | foreach ($list as $keyword => $time) { |
| 726 | 726 | if ($time <= time()) { |
| 727 | - unset($list[ $keyword ]); |
|
| 727 | + unset($list[$keyword]); |
|
| 728 | 728 | } else { |
| 729 | 729 | if ($tmp < $time) { |
| 730 | 730 | $tmp = $time; |
| 731 | 731 | } |
| 732 | 732 | if ($return_content == true) { |
| 733 | - $list_return[ $keyword ] = $this->get($keyword); |
|
| 733 | + $list_return[$keyword] = $this->get($keyword); |
|
| 734 | 734 | } else { |
| 735 | - $list_return[ $keyword ] = $time; |
|
| 735 | + $list_return[$keyword] = $time; |
|
| 736 | 736 | } |
| 737 | 737 | } |
| 738 | 738 | } |
| 739 | 739 | |
| 740 | 740 | $this->_tagCaching()->set($this->_getTagName($tag), $list, $tmp); |
| 741 | - $keywords[ $tag ] = $list_return; |
|
| 741 | + $keywords[$tag] = $list_return; |
|
| 742 | 742 | } |
| 743 | 743 | return $keywords; |
| 744 | 744 | } |
@@ -838,9 +838,9 @@ discard block |
||
| 838 | 838 | |
| 839 | 839 | public function _hit($index, $step = 1) |
| 840 | 840 | { |
| 841 | - $instance = $this->config[ 'instance' ]; |
|
| 842 | - $current = isset(CacheManager::$hit[ $instance ][ 'data' ][ $index ]) ? CacheManager::$hit[ $instance ][ 'data' ][ $index ] : 0; |
|
| 843 | - CacheManager::$hit[ $instance ][ 'data' ][ $index ] = $current + ($step); |
|
| 841 | + $instance = $this->config['instance']; |
|
| 842 | + $current = isset(CacheManager::$hit[$instance]['data'][$index]) ? CacheManager::$hit[$instance]['data'][$index] : 0; |
|
| 843 | + CacheManager::$hit[$instance]['data'][$index] = $current + ($step); |
|
| 844 | 844 | } |
| 845 | 845 | |
| 846 | 846 | } |
| 847 | 847 | \ No newline at end of file |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | |
| 230 | 230 | /** |
| 231 | 231 | * @param $filename |
| 232 | - * @return mixed |
|
| 232 | + * @return string |
|
| 233 | 233 | */ |
| 234 | 234 | public static function cleanFileName($filename) |
| 235 | 235 | { |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | } |
| 286 | 286 | |
| 287 | 287 | /** |
| 288 | - * @param $path |
|
| 288 | + * @param string $path |
|
| 289 | 289 | * @param bool $create |
| 290 | 290 | * @throws \Exception |
| 291 | 291 | */ |
@@ -47,42 +47,42 @@ discard block |
||
| 47 | 47 | * @var array |
| 48 | 48 | */ |
| 49 | 49 | public static $config = array( |
| 50 | - 'storage' => '', // blank for auto |
|
| 51 | - 'default_chmod' => 0777, // 0777 , 0666, 0644 |
|
| 50 | + 'storage' => '', // blank for auto |
|
| 51 | + 'default_chmod' => 0777, // 0777 , 0666, 0644 |
|
| 52 | 52 | |
| 53 | - 'overwrite' => "", // files, sqlite, etc it will overwrite ur storage and all other caching for waiting u fix ur server |
|
| 54 | - 'allow_search' => false, // turn to true will allow $method search("/regex/") |
|
| 53 | + 'overwrite' => "", // files, sqlite, etc it will overwrite ur storage and all other caching for waiting u fix ur server |
|
| 54 | + 'allow_search' => false, // turn to true will allow $method search("/regex/") |
|
| 55 | 55 | |
| 56 | - 'fallback' => 'files', //Fall back when old driver is not support |
|
| 56 | + 'fallback' => 'files', //Fall back when old driver is not support |
|
| 57 | 57 | |
| 58 | - 'securityKey' => 'auto', |
|
| 59 | - 'htaccess' => true, |
|
| 60 | - 'path' => '', |
|
| 58 | + 'securityKey' => 'auto', |
|
| 59 | + 'htaccess' => true, |
|
| 60 | + 'path' => '', |
|
| 61 | 61 | |
| 62 | - 'memcache' => array( |
|
| 62 | + 'memcache' => array( |
|
| 63 | 63 | array('127.0.0.1', 11211, 1), |
| 64 | - // array("new.host.ip",11211,1), |
|
| 65 | - ), |
|
| 64 | + // array("new.host.ip",11211,1), |
|
| 65 | + ), |
|
| 66 | 66 | |
| 67 | - 'redis' => array( |
|
| 67 | + 'redis' => array( |
|
| 68 | 68 | 'host' => '127.0.0.1', |
| 69 | 69 | 'port' => '', |
| 70 | 70 | 'password' => '', |
| 71 | 71 | 'database' => '', |
| 72 | 72 | 'timeout' => '', |
| 73 | - ), |
|
| 73 | + ), |
|
| 74 | 74 | |
| 75 | - 'ssdb' => array( |
|
| 75 | + 'ssdb' => array( |
|
| 76 | 76 | 'host' => '127.0.0.1', |
| 77 | 77 | 'port' => 8888, |
| 78 | 78 | 'password' => '', |
| 79 | 79 | 'timeout' => '', |
| 80 | - ), |
|
| 80 | + ), |
|
| 81 | 81 | |
| 82 | - 'extensions' => array(), |
|
| 83 | - "cache_method" => 1, // 1 = normal, 2 = phpfastcache, 3 = memory |
|
| 84 | - "limited_memory_each_object" => 4000, // maximum size (bytes) of object store in memory |
|
| 85 | - "compress_data" => false // compress stored data, if the backend supports it |
|
| 82 | + 'extensions' => array(), |
|
| 83 | + "cache_method" => 1, // 1 = normal, 2 = phpfastcache, 3 = memory |
|
| 84 | + "limited_memory_each_object" => 4000, // maximum size (bytes) of object store in memory |
|
| 85 | + "compress_data" => false // compress stored data, if the backend supports it |
|
| 86 | 86 | ); |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -177,12 +177,12 @@ discard block |
||
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | $securityKey = array_key_exists('securityKey', |
| 180 | - $config) ? $config[ 'securityKey' ] : ''; |
|
| 180 | + $config) ? $config[ 'securityKey' ] : ''; |
|
| 181 | 181 | if ($securityKey == "" || $securityKey == 'auto') { |
| 182 | 182 | $securityKey = self::$config[ 'securityKey' ]; |
| 183 | 183 | if ($securityKey == 'auto' || $securityKey == '') { |
| 184 | 184 | $securityKey = isset($_SERVER[ 'HTTP_HOST' ]) ? preg_replace('/^www./', |
| 185 | - '', strtolower($_SERVER[ 'HTTP_HOST' ])) : "default"; |
|
| 185 | + '', strtolower($_SERVER[ 'HTTP_HOST' ])) : "default"; |
|
| 186 | 186 | } |
| 187 | 187 | } |
| 188 | 188 | if ($securityKey != '') { |
@@ -234,9 +234,9 @@ discard block |
||
| 234 | 234 | public static function cleanFileName($filename) |
| 235 | 235 | { |
| 236 | 236 | $regex = array( |
| 237 | - '/[\?\[\]\/\\\=\<\>\:\;\,\'\"\&\$\#\*\(\)\|\~\`\!\{\}]/', |
|
| 238 | - '/\.$/', |
|
| 239 | - '/^\./', |
|
| 237 | + '/[\?\[\]\/\\\=\<\>\:\;\,\'\"\&\$\#\*\(\)\|\~\`\!\{\}]/', |
|
| 238 | + '/\.$/', |
|
| 239 | + '/^\./', |
|
| 240 | 240 | ); |
| 241 | 241 | $replace = array('-', '', ''); |
| 242 | 242 | return trim(preg_replace($regex, $replace, trim($filename)),'-'); |
@@ -261,10 +261,10 @@ discard block |
||
| 261 | 261 | protected static function getOS() |
| 262 | 262 | { |
| 263 | 263 | $os = array( |
| 264 | - 'os' => PHP_OS, |
|
| 265 | - 'php' => PHP_SAPI, |
|
| 266 | - 'system' => php_uname(), |
|
| 267 | - 'unique' => md5(php_uname() . PHP_OS . PHP_SAPI), |
|
| 264 | + 'os' => PHP_OS, |
|
| 265 | + 'php' => PHP_SAPI, |
|
| 266 | + 'system' => php_uname(), |
|
| 267 | + 'unique' => md5(php_uname() . PHP_OS . PHP_SAPI), |
|
| 268 | 268 | ); |
| 269 | 269 | return $os; |
| 270 | 270 | } |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | chmod($path, 0777); |
| 299 | 299 | } catch (phpFastCacheDriverException $e) { |
| 300 | 300 | throw new phpFastCacheDriverException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!', |
| 301 | - 92); |
|
| 301 | + 92); |
|
| 302 | 302 | } |
| 303 | 303 | } |
| 304 | 304 | |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | if (empty($config)) { |
| 106 | 106 | $config = phpFastCache::$config; |
| 107 | 107 | } |
| 108 | - $config[ 'storage' ] = $storage; |
|
| 108 | + $config['storage'] = $storage; |
|
| 109 | 109 | |
| 110 | 110 | $storage = strtolower($storage); |
| 111 | 111 | if ($storage == '' || $storage == 'auto') { |
@@ -158,31 +158,31 @@ discard block |
||
| 158 | 158 | { |
| 159 | 159 | $tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir(); |
| 160 | 160 | |
| 161 | - if (!isset($config[ 'path' ]) || $config[ 'path' ] == '') { |
|
| 161 | + if (!isset($config['path']) || $config['path'] == '') { |
|
| 162 | 162 | if (self::isPHPModule()) { |
| 163 | 163 | $path = $tmp_dir; |
| 164 | 164 | } else { |
| 165 | - $document_root_path = rtrim($_SERVER[ 'DOCUMENT_ROOT' ], '/') . '/../'; |
|
| 166 | - $path = isset($_SERVER[ 'DOCUMENT_ROOT' ]) && is_writable($document_root_path) |
|
| 165 | + $document_root_path = rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/../'; |
|
| 166 | + $path = isset($_SERVER['DOCUMENT_ROOT']) && is_writable($document_root_path) |
|
| 167 | 167 | ? $document_root_path |
| 168 | 168 | : rtrim(__DIR__, '/') . '/'; |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | - if (self::$config[ 'path' ] != '') { |
|
| 172 | - $path = $config[ 'path' ]; |
|
| 171 | + if (self::$config['path'] != '') { |
|
| 172 | + $path = $config['path']; |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | } else { |
| 176 | - $path = $config[ 'path' ]; |
|
| 176 | + $path = $config['path']; |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | $securityKey = array_key_exists('securityKey', |
| 180 | - $config) ? $config[ 'securityKey' ] : ''; |
|
| 180 | + $config) ? $config['securityKey'] : ''; |
|
| 181 | 181 | if ($securityKey == "" || $securityKey == 'auto') { |
| 182 | - $securityKey = self::$config[ 'securityKey' ]; |
|
| 182 | + $securityKey = self::$config['securityKey']; |
|
| 183 | 183 | if ($securityKey == 'auto' || $securityKey == '') { |
| 184 | - $securityKey = isset($_SERVER[ 'HTTP_HOST' ]) ? preg_replace('/^www./', |
|
| 185 | - '', strtolower($_SERVER[ 'HTTP_HOST' ])) : "default"; |
|
| 184 | + $securityKey = isset($_SERVER['HTTP_HOST']) ? preg_replace('/^www./', |
|
| 185 | + '', strtolower($_SERVER['HTTP_HOST'])) : "default"; |
|
| 186 | 186 | } |
| 187 | 187 | } |
| 188 | 188 | if ($securityKey != '') { |
@@ -191,11 +191,11 @@ discard block |
||
| 191 | 191 | |
| 192 | 192 | $securityKey = self::cleanFileName($securityKey); |
| 193 | 193 | |
| 194 | - $full_path = rtrim($path,'/') . '/' . $securityKey; |
|
| 194 | + $full_path = rtrim($path, '/') . '/' . $securityKey; |
|
| 195 | 195 | $full_pathx = md5($full_path); |
| 196 | 196 | |
| 197 | 197 | |
| 198 | - if ($skip_create_path == false && !isset(self::$tmp[ $full_pathx ])) { |
|
| 198 | + if ($skip_create_path == false && !isset(self::$tmp[$full_pathx])) { |
|
| 199 | 199 | |
| 200 | 200 | if (!@file_exists($full_path) || !@is_writable($full_path)) { |
| 201 | 201 | if (!@file_exists($full_path)) { |
@@ -204,9 +204,9 @@ discard block |
||
| 204 | 204 | if (!@is_writable($full_path)) { |
| 205 | 205 | @chmod($full_path, self::__setChmodAuto($config)); |
| 206 | 206 | } |
| 207 | - if(!@is_writable($full_path)) { |
|
| 207 | + if (!@is_writable($full_path)) { |
|
| 208 | 208 | // switch back to tmp dir again if the path is not writeable |
| 209 | - $full_path = rtrim($tmp_dir,'/') . '/' . $securityKey; |
|
| 209 | + $full_path = rtrim($tmp_dir, '/') . '/' . $securityKey; |
|
| 210 | 210 | if (!@file_exists($full_path)) { |
| 211 | 211 | @mkdir($full_path, self::__setChmodAuto($config)); |
| 212 | 212 | } |
@@ -219,8 +219,8 @@ discard block |
||
| 219 | 219 | } |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | - self::$tmp[ $full_pathx ] = true; |
|
| 223 | - self::htaccessGen($full_path, array_key_exists('htaccess', $config) ? $config[ 'htaccess' ] : false); |
|
| 222 | + self::$tmp[$full_pathx] = true; |
|
| 223 | + self::htaccessGen($full_path, array_key_exists('htaccess', $config) ? $config['htaccess'] : false); |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | return realpath($full_path); |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | '/^\./', |
| 240 | 240 | ); |
| 241 | 241 | $replace = array('-', '', ''); |
| 242 | - return trim(preg_replace($regex, $replace, trim($filename)),'-'); |
|
| 242 | + return trim(preg_replace($regex, $replace, trim($filename)), '-'); |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | /** |
@@ -248,10 +248,10 @@ discard block |
||
| 248 | 248 | */ |
| 249 | 249 | public static function __setChmodAuto($config) |
| 250 | 250 | { |
| 251 | - if (!isset($config[ 'default_chmod' ]) || $config[ 'default_chmod' ] == '' || is_null($config[ 'default_chmod' ])) { |
|
| 251 | + if (!isset($config['default_chmod']) || $config['default_chmod'] == '' || is_null($config['default_chmod'])) { |
|
| 252 | 252 | return 0777; |
| 253 | 253 | } else { |
| 254 | - return $config[ 'default_chmod' ]; |
|
| 254 | + return $config['default_chmod']; |
|
| 255 | 255 | } |
| 256 | 256 | } |
| 257 | 257 | |
@@ -302,7 +302,7 @@ discard block |
||
| 302 | 302 | } |
| 303 | 303 | } |
| 304 | 304 | |
| 305 | - if(!file_exists($path."/.htaccess")) { |
|
| 305 | + if (!file_exists($path . "/.htaccess")) { |
|
| 306 | 306 | // echo "write me"; |
| 307 | 307 | $html = "order deny, allow \r\n |
| 308 | 308 | deny from all \r\n |
@@ -328,9 +328,9 @@ discard block |
||
| 328 | 328 | public static function setup($name, $value = '') |
| 329 | 329 | { |
| 330 | 330 | if (is_array($name)) { |
| 331 | - self::$config = array_merge(self::$config,$name); |
|
| 331 | + self::$config = array_merge(self::$config, $name); |
|
| 332 | 332 | } else { |
| 333 | - self::$config[ $name ] = $value; |
|
| 333 | + self::$config[$name] = $value; |
|
| 334 | 334 | } |
| 335 | 335 | } |
| 336 | 336 | |