PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/vendors/redis.php

https://github.com/jlarmstrong/cake-redisdb
PHP | 374 lines | 303 code | 51 blank | 20 comment | 31 complexity | a991d8b892e182e41339270b551e2244 MD5 | raw file
  1. <?php
  2. /*******************************************************************************
  3. * Redis PHP Bindings - http://code.google.com/p/redis/
  4. *
  5. * Copyright 2009 Ludovico Magnocavallo
  6. * Copyright 2009 Salvatore Sanfilippo (ported it to PHP5, fixed some bug)
  7. * Released under the same license as Redis.
  8. *
  9. * Version: 0.1
  10. *
  11. * $Revision: 139 $
  12. * $Date: 2009-03-15 22:59:40 +0100 (Dom, 15 Mar 2009) $
  13. *
  14. ******************************************************************************/
  15. class Redis {
  16. public $server;
  17. public $port;
  18. private $_sock;
  19. public function __construct($host='localhost', $port=6379) {
  20. $this->host = $host;
  21. $this->port = $port;
  22. }
  23. public function connect() {
  24. if ($this->_sock) return;
  25. if ($sock = fsockopen($this->host, $this->port, $errno, $errstr)) {
  26. $this->_sock = $sock;
  27. return;
  28. }
  29. $msg = "Cannot open socket to {$this->host}:{$this->port}";
  30. if ($errno || $errmsg)
  31. $msg .= "," . ($errno ? " error $errno" : "") .
  32. ($errmsg ? " $errmsg" : "");
  33. trigger_error("$msg.", E_USER_ERROR);
  34. }
  35. public function disconnect() {
  36. if ($this->_sock) @fclose($this->_sock);
  37. $this->_sock = null;
  38. }
  39. public function ping() {
  40. $this->connect();
  41. $this->write("PING\r\n");
  42. return $this->get_response();
  43. }
  44. public function do_echo($s) {
  45. $this->connect();
  46. $this->write("ECHO " . strlen($s) . "\r\n$s\r\n");
  47. return $this->get_response();
  48. }
  49. public function set($name, $value, $preserve=false) {
  50. $this->connect();
  51. $this->write(
  52. ($preserve ? 'SETNX' : 'SET') .
  53. " $name " . strlen($value) . "\r\n$value\r\n"
  54. );
  55. return $this->get_response();
  56. }
  57. public function get($name) {
  58. $this->connect();
  59. $this->write("GET $name\r\n");
  60. return $this->get_response();
  61. }
  62. public function mget($keys) {
  63. $this->connect();
  64. $this->write("MGET ".implode(" ",$keys)."\r\n");
  65. return $this->get_response();
  66. }
  67. public function incr($name, $amount=1) {
  68. $this->connect();
  69. if ($amount == 1)
  70. $this->write("INCR $name\r\n");
  71. else
  72. $this->write("INCRBY $name $amount\r\n");
  73. return $this->get_response();
  74. }
  75. public function decr($name, $amount=1) {
  76. $this->connect();
  77. if ($amount == 1)
  78. $this->write("DECR $name\r\n");
  79. else
  80. $this->write("DECRBY $name $amount\r\n");
  81. return $this->get_response();
  82. }
  83. public function exists($name) {
  84. $this->connect();
  85. $this->write("EXISTS $name\r\n");
  86. return $this->get_response();
  87. }
  88. public function delete($name) {
  89. $this->connect();
  90. $this->write("DEL $name\r\n");
  91. return $this->get_response();
  92. }
  93. public function keys($pattern) {
  94. $this->connect();
  95. $this->write("KEYS $pattern\r\n");
  96. return explode(' ', $this->get_response());
  97. }
  98. public function randomkey() {
  99. $this->connect();
  100. $this->write("RANDOMKEY\r\n");
  101. return $this->get_response();
  102. }
  103. public function rename($src, $dst) {
  104. $this->connect();
  105. $this->write("RENAME $src $dst\r\n");
  106. return $this->get_response();
  107. }
  108. public function renamenx($src, $dst) {
  109. $this->connect();
  110. $this->write("RENAMENX $src $dst\r\n");
  111. return $this->get_response();
  112. }
  113. public function expire($name, $time) {
  114. $this->connect();
  115. $this->write("EXPIRE $name $time\r\n");
  116. return $this->get_response();
  117. }
  118. public function push($name, $value, $tail=true) {
  119. // default is to append the element to the list
  120. $this->connect();
  121. $this->write(
  122. ($tail ? 'RPUSH' : 'LPUSH') .
  123. " $name " . strlen($value) . "\r\n$value\r\n"
  124. );
  125. return $this->get_response();
  126. }
  127. public function lpush($name, $value) {
  128. return $this->push($name, $value, false);
  129. }
  130. public function rpush($name, $value) {
  131. return $this->push($name, $value, true);
  132. }
  133. public function ltrim($name, $start, $end) {
  134. $this->connect();
  135. $this->write("LTRIM $name $start $end\r\n");
  136. return $this->get_response();
  137. }
  138. public function lindex($name, $index) {
  139. $this->connect();
  140. $this->write("LINDEX $name $index\r\n");
  141. return $this->get_response();
  142. }
  143. public function pop($name, $tail=true) {
  144. $this->connect();
  145. $this->write(
  146. ($tail ? 'RPOP' : 'LPOP') .
  147. " $name\r\n"
  148. );
  149. return $this->get_response();
  150. }
  151. public function lpop($name, $value) {
  152. return $this->pop($name, $value, false);
  153. }
  154. public function rpop($name, $value) {
  155. return $this->pop($name, $value, true);
  156. }
  157. public function llen($name) {
  158. $this->connect();
  159. $this->write("LLEN $name\r\n");
  160. return $this->get_response();
  161. }
  162. public function lrange($name, $start, $end) {
  163. $this->connect();
  164. $this->write("LRANGE $name $start $end\r\n");
  165. return $this->get_response();
  166. }
  167. public function sort($name, $query=false) {
  168. $this->connect();
  169. $this->write($query == false ? "SORT $name\r\n" : "SORT $name $query\r\n");
  170. return $this->get_response();
  171. }
  172. public function lset($name, $value, $index) {
  173. $this->connect();
  174. $this->write("LSET $name $index " . strlen($value) . "\r\n$value\r\n");
  175. return $this->get_response();
  176. }
  177. public function sadd($name, $value) {
  178. $this->connect();
  179. $this->write("SADD $name " . strlen($value) . "\r\n$value\r\n");
  180. return $this->get_response();
  181. }
  182. public function srem($name, $value) {
  183. $this->connect();
  184. $this->write("SREM $name " . strlen($value) . "\r\n$value\r\n");
  185. return $this->get_response();
  186. }
  187. public function sismember($name, $value) {
  188. $this->connect();
  189. $this->write("SISMEMBER $name " . strlen($value) . "\r\n$value\r\n");
  190. return $this->get_response();
  191. }
  192. public function sinter($sets) {
  193. $this->connect();
  194. $this->write('SINTER ' . implode(' ', $sets) . "\r\n");
  195. return $this->get_response();
  196. }
  197. public function smembers($name) {
  198. $this->connect();
  199. $this->write("SMEMBERS $name\r\n");
  200. return $this->get_response();
  201. }
  202. public function scard($name) {
  203. $this->connect();
  204. $this->write("SCARD $name\r\n");
  205. return $this->get_response();
  206. }
  207. public function select_db($name) {
  208. $this->connect();
  209. $this->write("SELECT $name\r\n");
  210. return $this->get_response();
  211. }
  212. public function move($name, $db) {
  213. $this->connect();
  214. $this->write("MOVE $name $db\r\n");
  215. return $this->get_response();
  216. }
  217. public function save($background=false) {
  218. $this->connect();
  219. $this->write(($background ? "BGSAVE\r\n" : "SAVE\r\n"));
  220. return $this->get_response();
  221. }
  222. public function bgsave($background=false) {
  223. return $this->save(true);
  224. }
  225. public function lastsave() {
  226. $this->connect();
  227. $this->write("LASTSAVE\r\n");
  228. return $this->get_response();
  229. }
  230. public function flushdb($all=false) {
  231. $this->connect();
  232. $this->write($all ? "FLUSHALL\r\n" : "FLUSHDB\r\n");
  233. return $this->get_response();
  234. }
  235. public function flushall() {
  236. return $this->flush(true);
  237. }
  238. public function info() {
  239. $this->connect();
  240. $this->write("INFO\r\n");
  241. $info = array();
  242. $data =& $this->get_response();
  243. foreach (explode("\r\n", $data) as $l) {
  244. if (!$l)
  245. continue;
  246. list($k, $v) = explode(':', $l, 2);
  247. $_v = strpos($v, '.') !== false ? (float)$v : (int)$v;
  248. $info[$k] = (string)$_v == $v ? $_v : $v;
  249. }
  250. return $info;
  251. }
  252. private function write($s) {
  253. while ($s) {
  254. $i = fwrite($this->_sock, $s);
  255. if ($i == 0) // || $i == strlen($s))
  256. break;
  257. $s = substr($s, $i);
  258. }
  259. }
  260. private function read($len=1024) {
  261. if ($s = fgets($this->_sock))
  262. return $s;
  263. $this->disconnect();
  264. trigger_error("Cannot read from socket.", E_USER_ERROR);
  265. }
  266. private function get_response() {
  267. $data = trim($this->read());
  268. $c = $data[0];
  269. $data = substr($data, 1);
  270. switch ($c) {
  271. case '-':
  272. trigger_error($data, E_USER_ERROR);
  273. break;
  274. case '+':
  275. return $data;
  276. case ':':
  277. $i = strpos($data, '.') !== false ? (int)$data : (float)$data;
  278. if ((string)$i != $data)
  279. trigger_error("Cannot convert data '$c$data' to integer", E_USER_ERROR);
  280. return $i;
  281. case '$':
  282. return $this->get_bulk_reply($c . $data);
  283. case '*':
  284. $num = (int)$data;
  285. if ((string)$num != $data)
  286. trigger_error("Cannot convert multi-response header '$data' to integer", E_USER_ERROR);
  287. $result = array();
  288. for ($i=0; $i<$num; $i++)
  289. $result[] =& $this->get_response();
  290. return $result;
  291. default:
  292. trigger_error("Invalid reply type byte: '$c'");
  293. }
  294. }
  295. private function get_bulk_reply($data=null) {
  296. if ($data === null)
  297. $data = trim($this->read());
  298. if ($data == '$-1')
  299. return null;
  300. $c = $data[0];
  301. $data = substr($data, 1);
  302. $bulklen = (int)$data;
  303. if ((string)$bulklen != $data)
  304. trigger_error("Cannot convert bulk read header '$c$data' to integer", E_USER_ERROR);
  305. if ($c != '$')
  306. trigger_error("Unkown response prefix for '$c$data'", E_USER_ERROR);
  307. $buffer = '';
  308. while ($bulklen) {
  309. $data = fread($this->_sock,$bulklen);
  310. $bulklen -= strlen($data);
  311. $buffer .= $data;
  312. }
  313. $crlf = fread($this->_sock,2);
  314. return $buffer;
  315. }
  316. }
  317. /*
  318. $r = new Redis();
  319. var_dump($r->set("foo","bar"));
  320. var_dump($r->get("foo"));
  321. var_dump($r->info());
  322. */
  323. ?>