偶然翻看openresty安裝文件的目錄,看到lualib/resty目錄下有一些用lua寫的模塊,其中有個memcached.lua,原來是memcache客戶端的源碼,突然想起memcached協議是基于命令行的。他這個模塊用到了ngx.socket.tcp模塊,粗略看了下,發現也不是特別復雜,就是用socket收發數據。于是心血來潮,百度了下memcached協議,來造個輪子。
memcached協議可參考:memcached協議中文版,講得挺詳細的。
廢話少說,上代碼(只實現了get,set,add,replace,flush_all共5個命令)。
/** 封裝的異常類 */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錯誤 * @return string 最后一次socket錯誤字符串 */ public function getSocketError() { return socket_strerror(socket_last_error($this->socket)); } /** * 拋出異常封裝函數 * @return MemcacheException MemcacheException實例 */ private function throwException() { throw new MemcacheException($this->getSocketError()); } /** * 鏈接memcached服務器 * @param string $host memcached監聽的ip * @param integer $port memcached監聽的端口 * @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; } } /** * 執行set|add|replace命令 * @param string $cmd 命令(set|add|replace) * @param string $key 鍵 * @param string $value 值 * @param nteger $ttl 生存時間 * @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); /** 讀取最后一個 /n 字符 */ socket_read($this->socket, 1, PHP_BINARY_READ); if ($response === false) { $this->throwException(); } /** 操作成功會返回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); } /** * 獲取一個鍵的值 * @param string $key 鍵 * @return string|boolean 值, false表示沒有這個鍵或者已過期 */ 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); /** 讀取最后一個 /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); /** 獲取數據長度 */ $dataLen = intval(end($arr)); /** 獲取數據 */ $response = socket_read($this->socket, $dataLen, PHP_BINARY_READ); /** 讀取最后7個字符 /r/nEND/r/n */ socket_read($this->socket, 7, PHP_BINARY_READ); if ($response === false) { $this->throwException(); } return $response; } else { return false; } } /** * 設置所有的鍵過期 * @return boolean success */ public function flushAll() { $data = "flush_all/r/n"; $result = socket_write($this->socket, $data, strlen($data)); /** 讀取返回結果,固定為 OK/r/n */ socket_read($this->socket, 4, PHP_BINARY_READ); return true; }}新聞熱點
疑難解答
圖片精選