@@ -26,7 +26,7 @@ |
||
26 | 26 | |
27 | 27 | // Setup File Path on your config files |
28 | 28 | CacheManager::setup(array( |
29 | - // "path" => sys_get_temp_dir(), // or in windows "C:/tmp/" |
|
29 | + // "path" => sys_get_temp_dir(), // or in windows "C:/tmp/" |
|
30 | 30 | )); |
31 | 31 | // our unique method of caching, faster than traditional caching which shared everywhere on internet like 7-10 times |
32 | 32 | // reduce high load CPU, reduce I/O from files open |
@@ -7,7 +7,7 @@ |
||
7 | 7 | * All you need is put this line on your config. |
8 | 8 | */ |
9 | 9 | |
10 | - // In your Setting / Config.php or Index.php |
|
10 | + // In your Setting / Config.php or Index.php |
|
11 | 11 | define("PHPFASTCACHE_LEGACY",true); |
12 | 12 | |
13 | 13 | // If you use composer, then it auto included our "src/phpFastCache/phpFastCache.php" on vendor folder already, you don't need to do anything else |
@@ -6,12 +6,12 @@ |
||
6 | 6 | * @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
7 | 7 | */ |
8 | 8 | |
9 | - // In your config files |
|
9 | + // In your config files |
|
10 | 10 | require_once("../../src/autoload.php"); |
11 | 11 | |
12 | - // now it's time to call the cache "anywhere" on your project |
|
12 | + // now it's time to call the cache "anywhere" on your project |
|
13 | 13 | |
14 | - $cache = phpFastCache("redis"); |
|
14 | + $cache = phpFastCache("redis"); |
|
15 | 15 | |
16 | 16 | if ($cache->fallback === true) { |
17 | 17 | echo " USE BACK UP DRIVER = " . phpFastCache::$config[ 'fallback' ] . " <br>"; |
@@ -1,38 +1,38 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | - /* |
|
3 | + /* |
|
4 | 4 | * For Legacy Coding |
5 | 5 | * You can do require_once ("phpFastCache/phpFastCache.php"); |
6 | 6 | * and call it right away |
7 | 7 | */ |
8 | 8 | |
9 | - // In your config files |
|
9 | + // In your config files |
|
10 | 10 | require_once("../../src/autoload.php"); |
11 | 11 | |
12 | - // now it's time to call the cache "anywhere" on your project |
|
12 | + // now it's time to call the cache "anywhere" on your project |
|
13 | 13 | |
14 | - $cache = phpFastCache(); |
|
15 | - /* phpFastCache support more drivers |
|
14 | + $cache = phpFastCache(); |
|
15 | + /* phpFastCache support more drivers |
|
16 | 16 | $files_cache = phpFastCache("files"); |
17 | 17 | $memcache_cache = phpFastCache("memcache"); |
18 | 18 | */ |
19 | 19 | |
20 | - /** |
|
21 | - * Try to get $products from Caching First |
|
22 | - * product_page is "identity keyword"; |
|
23 | - */ |
|
24 | - $key = "product_page"; |
|
25 | - $CachedString = $cache->get($key); |
|
20 | + /** |
|
21 | + * Try to get $products from Caching First |
|
22 | + * product_page is "identity keyword"; |
|
23 | + */ |
|
24 | + $key = "product_page"; |
|
25 | + $CachedString = $cache->get($key); |
|
26 | 26 | |
27 | - if (is_null($CachedString)) { |
|
28 | - $CachedString = "Files Cache --> Cache Enabled --> Well done !"; |
|
29 | - // Write products to Cache in 10 minutes with same keyword |
|
30 | - $InstanceCache->set($key, $CachedString, 600); |
|
27 | + if (is_null($CachedString)) { |
|
28 | + $CachedString = "Files Cache --> Cache Enabled --> Well done !"; |
|
29 | + // Write products to Cache in 10 minutes with same keyword |
|
30 | + $InstanceCache->set($key, $CachedString, 600); |
|
31 | 31 | |
32 | - echo "Files Cache --> Cached not enabled --> Reload page !"; |
|
32 | + echo "Files Cache --> Cached not enabled --> Reload page !"; |
|
33 | 33 | |
34 | - } else { |
|
35 | - echo $CachedString; |
|
36 | - } |
|
34 | + } else { |
|
35 | + echo $CachedString; |
|
36 | + } |
|
37 | 37 | |
38 | 38 |
@@ -11,8 +11,8 @@ discard block |
||
11 | 11 | */ |
12 | 12 | |
13 | 13 | $config = array( |
14 | - "storage" => "auto", // auto, files, sqlite, apc, cookie, memcache, memcached, predis, redis, wincache, xcache |
|
15 | - "default_chmod" => 0777, // For security, please use 0666 for module and 0644 for cgi. |
|
14 | + "storage" => "auto", // auto, files, sqlite, apc, cookie, memcache, memcached, predis, redis, wincache, xcache |
|
15 | + "default_chmod" => 0777, // For security, please use 0666 for module and 0644 for cgi. |
|
16 | 16 | |
17 | 17 | |
18 | 18 | /* |
@@ -22,35 +22,35 @@ discard block |
||
22 | 22 | // create .htaccess to protect cache folder |
23 | 23 | // By default the cache folder will try to create itself outside your public_html. |
24 | 24 | // However an htaccess also created in case. |
25 | - "htaccess" => true, |
|
25 | + "htaccess" => true, |
|
26 | 26 | |
27 | 27 | // path to cache folder, leave it blank for auto detect |
28 | - "path" => "", |
|
29 | - "securityKey" => "auto", // auto will use domain name, set it to 1 string if you use alias domain name |
|
28 | + "path" => "", |
|
29 | + "securityKey" => "auto", // auto will use domain name, set it to 1 string if you use alias domain name |
|
30 | 30 | |
31 | 31 | // MEMCACHE |
32 | 32 | |
33 | - "memcache" => array( |
|
33 | + "memcache" => array( |
|
34 | 34 | array("127.0.0.1", 11211, 1), |
35 | - // array("new.host.ip",11211,1), |
|
36 | - ), |
|
35 | + // array("new.host.ip",11211,1), |
|
36 | + ), |
|
37 | 37 | |
38 | 38 | // REDIS |
39 | - "redis" => array( |
|
39 | + "redis" => array( |
|
40 | 40 | "host" => "127.0.0.1", |
41 | 41 | "port" => "", |
42 | 42 | "password" => "", |
43 | 43 | "database" => "", |
44 | 44 | "timeout" => "", |
45 | - ), |
|
45 | + ), |
|
46 | 46 | |
47 | - "extensions" => array(), |
|
47 | + "extensions" => array(), |
|
48 | 48 | |
49 | 49 | |
50 | 50 | /* |
51 | 51 | * Fall back when old driver is not support |
52 | 52 | */ |
53 | - "fallback" => "files", |
|
53 | + "fallback" => "files", |
|
54 | 54 | ); |
55 | 55 | |
56 | 56 | phpFastCache::setup($config); |
@@ -77,6 +77,6 @@ discard block |
||
77 | 77 | |
78 | 78 | $cache2 = CacheManager::getInstance("memcache"); // this will use memcache |
79 | 79 | $server = array( |
80 | - array("127.0.0.1", 11211, 1), |
|
80 | + array("127.0.0.1", 11211, 1), |
|
81 | 81 | ); |
82 | 82 | $cache2->setup("memcache", $server); |
83 | 83 | \ No newline at end of file |
@@ -44,26 +44,26 @@ |
||
44 | 44 | $list = $cache->getInfoMulti(array("key1", "key2", "key3")); |
45 | 45 | |
46 | 46 | $cache->setMulti(array("key1", "value1", 300), |
47 | - array("key2", "value2", 600), |
|
48 | - array("key3", "value3", 1800)); |
|
47 | + array("key2", "value2", 600), |
|
48 | + array("key3", "value3", 1800)); |
|
49 | 49 | |
50 | 50 | $cache->deleteMulti(array("key1", "key2", "key3")); |
51 | 51 | |
52 | 52 | $cache->isExistingMulti(array("key1", "key2", "key3")); |
53 | 53 | |
54 | 54 | $cache->touchMulti(array( |
55 | - array("key", 300), |
|
56 | - array("key2", 400), |
|
55 | + array("key", 300), |
|
56 | + array("key2", 400), |
|
57 | 57 | )); |
58 | 58 | |
59 | 59 | $cache->incrementMulti(array( |
60 | - array("key", 1), |
|
61 | - array("key2", 2), |
|
60 | + array("key", 1), |
|
61 | + array("key2", 2), |
|
62 | 62 | )); |
63 | 63 | |
64 | 64 | $cache->decrementMulti(array( |
65 | - array("key", 1), |
|
66 | - array("key2", 2), |
|
65 | + array("key", 1), |
|
66 | + array("key2", 2), |
|
67 | 67 | )); |
68 | 68 | |
69 | 69 |
@@ -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 |
@@ -43,7 +43,7 @@ |
||
43 | 43 | } |
44 | 44 | } else { |
45 | 45 | echo "Received the following message from {$message->channel}:", |
46 | - PHP_EOL, " {$message->payload}", PHP_EOL, PHP_EOL; |
|
46 | + PHP_EOL, " {$message->payload}", PHP_EOL, PHP_EOL; |
|
47 | 47 | } |
48 | 48 | break; |
49 | 49 | } |
@@ -30,15 +30,15 @@ |
||
30 | 30 | |
31 | 31 | $multiple_servers = array( |
32 | 32 | array( |
33 | - 'host' => '127.0.0.1', |
|
34 | - 'port' => 6379, |
|
35 | - 'database' => 15, |
|
36 | - 'alias' => 'first', |
|
33 | + 'host' => '127.0.0.1', |
|
34 | + 'port' => 6379, |
|
35 | + 'database' => 15, |
|
36 | + 'alias' => 'first', |
|
37 | 37 | ), |
38 | 38 | array( |
39 | - 'host' => '127.0.0.1', |
|
40 | - 'port' => 6380, |
|
41 | - 'database' => 15, |
|
42 | - 'alias' => 'second', |
|
39 | + 'host' => '127.0.0.1', |
|
40 | + 'port' => 6380, |
|
41 | + 'database' => 15, |
|
42 | + 'alias' => 'second', |
|
43 | 43 | ), |
44 | 44 | ); |