国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

用PHP實(shí)現(xiàn)memcache客戶端

2019-11-08 20:14:10
字體:
供稿:網(wǎng)友

1、緣起

偶然翻看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è)輪子。

2、代碼

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; }}

3、測(cè)試

測(cè)試代碼try { $memcache = new MyMemcacheClient(); $memcache->connect(); $memcache->flushAll(); echo "a=", $memcache->get("a"), PHP_EOL; $memcache->set("a", "This is a"); $data = $memcache->get("a"); echo "a: ", $data, PHP_EOL; $memcache->set("a", "This is modified a"); echo "a: ", $memcache->get("a"), PHP_EOL; $memcache->add("b", "This is b"); echo "b: ", $memcache->get("b"), PHP_EOL; $memcache->replace("b", "This is replaced b"); echo "b: ", $memcache->get("b"), PHP_EOL;} catch (MemcacheException $e) { echo $e->getMessage();}測(cè)試結(jié)果a=a: This is aa: This is modified ab: This is bb: This is replaced b
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 边坝县| 灵璧县| 高密市| 即墨市| 醴陵市| 翼城县| 仲巴县| 青岛市| 阳泉市| 称多县| 云浮市| 洛阳市| 昭苏县| 望都县| 长兴县| 南岸区| 铜梁县| 马鞍山市| 黔江区| 兴城市| 宣武区| 太白县| 夏邑县| 龙泉市| 远安县| 同仁县| 庆元县| 遵义县| 吐鲁番市| 宣化县| 伊金霍洛旗| 崇信县| 迁安市| 勃利县| 临猗县| 秀山| 邵东县| 茌平县| 天祝| 玛纳斯县| 白山市|