MemcacheSASL.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <?php
  2. /**
  3. * 部份指令集
  4. * 0x00 Get
  5. * 0x01 Set
  6. * 0x02 Add
  7. * 0x03 Replace
  8. * 0x04 Delete
  9. * 0x05 Increment
  10. * 0x06 Decrement
  11. * 0x07 Quit
  12. * 0x08 Flush
  13. * 0x09 GetQ
  14. * 0x0A No-op
  15. * 0x0B Version
  16. * 0x0C GetK
  17. * 0x0D GetKQ
  18. * 0x0E Append
  19. * 0x0F Prepend
  20. * 0x10 Stat
  21. * 0x11 SetQ
  22. * 0x12 AddQ
  23. * 0x13 ReplaceQ
  24. * 0x14 DeleteQ
  25. * 0x15 IncrementQ
  26. * 0x16 DecrementQ
  27. * 0x17 QuitQ
  28. * 0x18 FlushQ
  29. * 0x19 AppendQ
  30. * 0x1A PrependQ
  31. * @author 云淡风轻
  32. *
  33. */
  34. class MemcacheSASL
  35. {
  36. protected $_request_format = 'CCnCCnNNNN';
  37. protected $_response_format = 'Cmagic/Copcode/nkeylength/Cextralength/Cdatatype/nstatus/Nbodylength/NOpaque/NCAS1/NCAS2';
  38. const OPT_COMPRESSION = -1001;
  39. const MEMC_VAL_TYPE_MASK = 0xf;
  40. const MEMC_VAL_IS_STRING = 0;
  41. const MEMC_VAL_IS_LONG = 1;
  42. const MEMC_VAL_IS_DOUBLE = 2;
  43. const MEMC_VAL_IS_BOOL = 3;
  44. const MEMC_VAL_IS_SERIALIZED = 4;
  45. const MEMC_VAL_COMPRESSED = 16; // 2^4
  46. protected function _build_request($data)
  47. {
  48. $valuelength = $extralength = $keylength = 0;
  49. if (array_key_exists('extra', $data)) {
  50. $extralength = strlen($data['extra']);
  51. }
  52. if (array_key_exists('key', $data)) {
  53. $keylength = strlen($data['key']);
  54. }
  55. if (array_key_exists('value', $data)) {
  56. $valuelength = strlen($data['value']);
  57. }
  58. $bodylength = $extralength + $keylength + $valuelength;
  59. $ret = pack($this->_request_format,
  60. 0x80,
  61. $data['opcode'],
  62. $keylength,
  63. $extralength,
  64. array_key_exists('datatype', $data) ? $data['datatype'] : null,
  65. array_key_exists('status', $data) ? $data['status'] : null,
  66. $bodylength,
  67. array_key_exists('Opaque', $data) ? $data['Opaque'] : null,
  68. array_key_exists('CAS1', $data) ? $data['CAS1'] : null,
  69. array_key_exists('CAS2', $data) ? $data['CAS2'] : null
  70. );
  71. if (array_key_exists('extra', $data)) {
  72. $ret .= $data['extra'];
  73. }
  74. if (array_key_exists('key', $data)) {
  75. $ret .= $data['key'];
  76. }
  77. if (array_key_exists('value', $data)) {
  78. $ret .= $data['value'];
  79. }
  80. return $ret;
  81. }
  82. protected function _show_request($data)
  83. {
  84. $array = unpack($this->_response_format, $data);
  85. return $array;
  86. }
  87. protected function _send($data)
  88. {
  89. $send_data = $this->_build_request($data);
  90. fwrite($this->_fp, $send_data);
  91. return $send_data;
  92. }
  93. protected function _recv()
  94. {
  95. $data = fread($this->_fp, 24);
  96. $array = $this->_show_request($data);
  97. if ($array['bodylength']) {
  98. $bodylength = $array['bodylength'];
  99. $data = '';
  100. while ($bodylength > 0) {
  101. $recv_data = fread($this->_fp, $bodylength);
  102. $bodylength -= strlen($recv_data);
  103. $data .= $recv_data;
  104. }
  105. if ($array['extralength']) {
  106. $extra_unpacked = unpack('Nint', substr($data, 0, $array['extralength']));
  107. $array['extra'] = $extra_unpacked['int'];
  108. }
  109. $array['key'] = substr($data, $array['extralength'], $array['keylength']);
  110. $array['body'] = substr($data, $array['extralength'] + $array['keylength']);
  111. }
  112. return $array;
  113. }
  114. public function __construct()
  115. {
  116. }
  117. public function listMechanisms()
  118. {
  119. $this->_send(array('opcode' => 0x20));
  120. $data = $this->_recv();
  121. return explode(" ", $data['body']);
  122. }
  123. public function setSaslAuthData($user, $password)
  124. {
  125. $this->_send(array(
  126. 'opcode' => 0x21,
  127. 'key' => 'PLAIN',
  128. 'value' => '' . chr(0) . $user . chr(0) . $password
  129. ));
  130. $data = $this->_recv();
  131. if ($data['status']) {
  132. throw new Exception($data['body'], $data['status']);
  133. }
  134. }
  135. public function addServer($host, $port, $weight = 0)
  136. {
  137. $this->_fp = stream_socket_client($host . ':' . $port);
  138. }
  139. public function addServers($servers)
  140. {
  141. for ($i = 0; $i < count($servers); $i++) {
  142. $s = $servers[$i];
  143. if (count($s) >= 2) {
  144. $this->addServer($s[0], $s[1]);
  145. } else {
  146. trigger_error("could not add entry #"
  147. .($i+1)." to the server list", E_USER_WARNING);
  148. }
  149. }
  150. }
  151. public function addServersByString($servers)
  152. {
  153. $servers = explode(",", $servers);
  154. for ($i = 0; $i < count($servers); $i++) {
  155. $servers[$i] = explode(":", $servers[$i]);
  156. }
  157. $this->addServers($servers);
  158. }
  159. public function get($key)
  160. {
  161. $sent = $this->_send(array(
  162. 'opcode' => 0x00,
  163. 'key' => $key,
  164. ));
  165. $data = $this->_recv();
  166. if (0 == $data['status']) {
  167. if ($data['extra'] & self::MEMC_VAL_COMPRESSED) {
  168. $body = gzuncompress($data['body']);
  169. } else {
  170. $body = $data['body'];
  171. }
  172. $type = $data['extra'] & self::MEMC_VAL_TYPE_MASK;
  173. switch ($type) {
  174. case self::MEMC_VAL_IS_STRING:
  175. $body = strval($body);
  176. break;
  177. case self::MEMC_VAL_IS_LONG:
  178. $body = intval($body);
  179. break;
  180. case self::MEMC_VAL_IS_DOUBLE:
  181. $body = floatval($body);
  182. break;
  183. case self::MEMC_VAL_IS_BOOL:
  184. $body = $body ? true : false;
  185. break;
  186. case self::MEMC_VAL_IS_SERIALIZED:
  187. $body = unserialize($body);
  188. break;
  189. }
  190. return $body;
  191. }
  192. return FALSE;
  193. }
  194. /**
  195. * process value and get flag
  196. *
  197. * @param int $flag
  198. * @param mixed $value
  199. * @access protected
  200. * @return array($flag, $processed_value)
  201. */
  202. protected function _processValue($flag, $value)
  203. {
  204. if (is_string($value)) {
  205. $flag |= self::MEMC_VAL_IS_STRING;
  206. } elseif (is_long($value)) {
  207. $flag |= self::MEMC_VAL_IS_LONG;
  208. } elseif (is_double($value)) {
  209. $flag |= self::MEMC_VAL_IS_DOUBLE;
  210. } elseif (is_bool($value)) {
  211. $flag |= self::MEMC_VAL_IS_BOOL;
  212. } else {
  213. $value = serialize($value);
  214. $flag |= self::MEMC_VAL_IS_SERIALIZED;
  215. }
  216. if (array_key_exists(self::OPT_COMPRESSION, $this->_options) and $this->_options[self::OPT_COMPRESSION]) {
  217. $flag |= self::MEMC_VAL_COMPRESSED;
  218. $value = gzcompress($value);
  219. }
  220. return array($flag, $value);
  221. }
  222. public function add($key, $value, $expiration = 0)
  223. {
  224. list($flag, $value) = $this->_processValue(0, $value);
  225. $extra = pack('NN', $flag, $expiration);
  226. $sent = $this->_send(array(
  227. 'opcode' => 0x02,
  228. 'key' => $key,
  229. 'value' => $value,
  230. 'extra' => $extra,
  231. ));
  232. $data = $this->_recv();
  233. if ($data['status'] == 0) {
  234. return TRUE;
  235. }
  236. return FALSE;
  237. }
  238. public function set($key, $value, $expiration = 0)
  239. {
  240. list($flag, $value) = $this->_processValue(0, $value);
  241. $extra = pack('NN', $flag, $expiration);
  242. $sent = $this->_send(array(
  243. 'opcode' => 0x01,
  244. 'key' => $key,
  245. 'value' => $value,
  246. 'extra' => $extra,
  247. ));
  248. $data = $this->_recv();
  249. if ($data['status'] == 0) {
  250. return TRUE;
  251. }
  252. return FALSE;
  253. }
  254. public function delete($key)
  255. {
  256. $sent = $this->_send(array(
  257. 'opcode' => 0x04,
  258. 'key' => $key,
  259. ));
  260. $data = $this->_recv();
  261. if ($data['status'] == 0) {
  262. return TRUE;
  263. }
  264. return FALSE;
  265. }
  266. public function flush($delay = 0)
  267. {
  268. $extra = pack('N', $delay);
  269. $sent = $this->_send(array(
  270. 'opcode' => 0x08,
  271. 'extra' => $extra,
  272. ));
  273. $data = $this->_recv();
  274. if ($data['status'] == 0) {
  275. return TRUE;
  276. }
  277. return FALSE;
  278. }
  279. public function replace($key, $value, $expiration = 0)
  280. {
  281. list($flag, $value) = $this->_processValue(0, $value);
  282. $extra = pack('NN', $flag, $expiration);
  283. $sent = $this->_send(array(
  284. 'opcode' => 0x03,
  285. 'key' => $key,
  286. 'value' => $value,
  287. 'extra' => $extra,
  288. ));
  289. $data = $this->_recv();
  290. if ($data['status'] == 0) {
  291. return TRUE;
  292. }
  293. return FALSE;
  294. }
  295. protected function _upper($num)
  296. {
  297. return $num << 32;
  298. }
  299. protected function _lower($num)
  300. {
  301. return $num % (2 << 32);
  302. }
  303. public function increment($key, $offset = 1)
  304. {
  305. $initial_value = 0;
  306. $extra = pack('N2N2N', $this->_upper($offset), $this->_lower($offset), $this->_upper($initial_value), $this->_lower($initial_value), $expiration);
  307. $sent = $this->_send(array(
  308. 'opcode' => 0x05,
  309. 'key' => $key,
  310. 'extra' => $extra,
  311. ));
  312. $data = $this->_recv();
  313. if ($data['status'] == 0) {
  314. return TRUE;
  315. }
  316. return FALSE;
  317. }
  318. public function decrement($key, $offset = 1)
  319. {
  320. $initial_value = 0;
  321. $extra = pack('N2N2N', $this->_upper($offset), $this->_lower($offset), $this->_upper($initial_value), $this->_lower($initial_value), $expiration);
  322. $sent = $this->_send(array(
  323. 'opcode' => 0x06,
  324. 'key' => $key,
  325. 'extra' => $extra,
  326. ));
  327. $data = $this->_recv();
  328. if ($data['status'] == 0) {
  329. return TRUE;
  330. }
  331. return FALSE;
  332. }
  333. /**
  334. * Get statistics of the server
  335. *
  336. * @param string $type The type of statistics to fetch. Valid values are
  337. * {reset, malloc, maps, cachedump, slabs, items,
  338. * sizes}. According to the memcached protocol spec
  339. * these additional arguments "are subject to change
  340. * for the convenience of memcache developers".
  341. *
  342. * @link http://code.google.com/p/memcached/wiki/BinaryProtocolRevamped#Stat
  343. * @access public
  344. * @return array Returns an associative array of server statistics or
  345. * FALSE on failure.
  346. */
  347. public function getStats($type = null)
  348. {
  349. $this->_send(
  350. array(
  351. 'opcode' => 0x10,
  352. 'key' => $type,
  353. )
  354. );
  355. $ret = array();
  356. while (true) {
  357. $item = $this->_recv();
  358. if (empty($item['key'])) {
  359. break;
  360. }
  361. $ret[$item['key']] = $item['body'];
  362. }
  363. return $ret;
  364. }
  365. public function append($key, $value)
  366. {
  367. // TODO: If the Memcached::OPT_COMPRESSION is enabled, the operation
  368. // should failed.
  369. $sent = $this->_send(array(
  370. 'opcode' => 0x0e,
  371. 'key' => $key,
  372. 'value' => $value,
  373. ));
  374. $data = $this->_recv();
  375. if ($data['status'] == 0) {
  376. return TRUE;
  377. }
  378. return FALSE;
  379. }
  380. public function prepend($key, $value)
  381. {
  382. // TODO: If the Memcached::OPT_COMPRESSION is enabled, the operation
  383. // should failed.
  384. $sent = $this->_send(array(
  385. 'opcode' => 0x0f,
  386. 'key' => $key,
  387. 'value' => $value,
  388. ));
  389. $data = $this->_recv();
  390. if ($data['status'] == 0) {
  391. return TRUE;
  392. }
  393. return FALSE;
  394. }
  395. public function getMulti(array $keys)
  396. {
  397. // TODO: from http://code.google.com/p/memcached/wiki/BinaryProtocolRevamped#Get,_Get_Quietly,_Get_Key,_Get_Key_Quietly
  398. // Clients should implement multi-get (still important for reducing network roundtrips!) as n pipelined requests ...
  399. $list = array();
  400. foreach ($keys as $key) {
  401. $value = $this->get($key);
  402. if (false !== $value) {
  403. $list[$key] = $value;
  404. }
  405. }
  406. return $list;
  407. }
  408. protected $_options = array();
  409. public function setOption($key, $value)
  410. {
  411. $this->_options[$key] = $value;
  412. }
  413. }
  414. ?>