偶然翻看openresty安裝文件的目錄,看到lualib/resty目錄下有一些用lua寫的模塊,其中有個(gè)memcached.lua,原來是memcache客戶端的源碼,突然想起memcached協(xié)議是基于命令行的。他這個(gè)模塊用到了ngx.socket.tcp模塊,粗略看了下,發(fā)現(xiàn)也不是特別復(fù)雜,就是用socket收發(fā)數(shù)據(jù)。于是心血來潮,百度了下memcached協(xié)議,來造個(gè)輪子。
memcached協(xié)議可參考:memcached協(xié)議中文版,講得挺詳細(xì)的。
廢話少說,上代碼(只實(shí)現(xiàn)了get,set,add,replace,flush_all共5個(gè)命令)。
/** 封裝的異常類 */class MemcacheException extends Exception { public function __construct($message, $code = 0) { parent::__construct($message, $code); }}class MyMemcacheClient { PRivate $host; private $port; private $socket; public function __construct() { $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); } /** * 獲取最后一次的socket錯(cuò)誤 * @return string 最后一次socket錯(cuò)誤字符串 */ public function getSocketError() { return socket_strerror(socket_last_error($this->socket)); } /** * 拋出異常封裝函數(shù) * @return MemcacheException MemcacheException實(shí)例 */ private function throwException() { throw new MemcacheException($this->getSocketError()); } /** * 鏈接memcached服務(wù)器 * @param string $host memcached監(jiān)聽的ip * @param integer $port memcached監(jiān)聽的端口 * @return boolean true表示連接成功,false表示連接失敗 */ public function connect($host = '127.0.0.1', $port = 11211) { $this->host = $host; $this->port = $port; $result = socket_connect($this->socket, $host, $port); if ($result === false) { return false; } else { return true; } } /** * 執(zhí)行set|add|replace命令 * @param string $cmd 命令(set|add|replace) * @param string $key 鍵 * @param string $value 值 * @param nteger $ttl 生存時(shí)間 * @return boolean true for success, false for fail */ private function _set_add_replace($cmd, $key, $value, $ttl = 10) { $line1 = sprintf("$cmd %s 0 %d %d/r/n", $key, $ttl, strlen($value)); $line2 = $value . "/r/n"; $data = $line1 . $line2; $result = socket_write($this->socket, $data, strlen($data)); if ($result === false) { $this->throwException(); } $response = socket_read($this->socket, 1024, php_NORMAL_READ); /** 讀取最后一個(gè) /n 字符 */ socket_read($this->socket, 1, PHP_BINARY_READ); if ($response === false) { $this->throwException(); } /** 操作成功會(huì)返回STORED/r/n */ if (!strncmp($response, 'STORED', 6)) { return true; } return false; } public function set($key, $value, $ttl = 10) { return $this->_set_add_replace('set', $key, $value, $ttl); } public function add($key, $value, $ttl = 10) { return $this->_set_add_replace('add', $key, $value, $ttl); } public function replace($key, $value, $ttl = 10) { return $this->_set_add_replace('replace', $key, $value, $ttl); } /** * 獲取一個(gè)鍵的值 * @param string $key 鍵 * @return string|boolean 值, false表示沒有這個(gè)鍵或者已過期 */ public function get($key) { $data = sprintf("get %s/r/n", $key); $result = socket_write($this->socket, $data, strlen($data)); if ($result === false) { $this->throwException(); } $line1 = socket_read($this->socket, 1024, PHP_NORMAL_READ); /** 讀取最后一個(gè) /n 字符 */ socket_read($this->socket, 1, PHP_BINARY_READ); if (!$line1) { $this->throwException(); } /** 獲取成功,第一行返回 VALUE <key> <flags> <bytes>/r/n */ if (!strncmp($line1, "VALUE", 5)) { $line1 = rtrim($line1, "/r/n"); $arr = explode(' ', $line1); /** 獲取數(shù)據(jù)長(zhǎng)度 */ $dataLen = intval(end($arr)); /** 獲取數(shù)據(jù) */ $response = socket_read($this->socket, $dataLen, PHP_BINARY_READ); /** 讀取最后7個(gè)字符 /r/nEND/r/n */ socket_read($this->socket, 7, PHP_BINARY_READ); if ($response === false) { $this->throwException(); } return $response; } else { return false; } } /** * 設(shè)置所有的鍵過期 * @return boolean success */ public function flushAll() { $data = "flush_all/r/n"; $result = socket_write($this->socket, $data, strlen($data)); /** 讀取返回結(jié)果,固定為 OK/r/n */ socket_read($this->socket, 4, PHP_BINARY_READ); return true; }}新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注