一、Memcache 應用一 php與memcache
1、PHP的memcache的客戶端
PHP有兩個memcache客戶端:php memcache和php memcached。php memcache獨立用php實現,是老客戶端,從我們實踐中已發現有多個問題,而且功能少,屬性也可設置的少;php memcached是基於原生的c的libmemcached的擴展,更加完善,建議替換爲php memcached。
2、PHP memcache的問題
(1).分佈式問題
php memcache默認會自動切換實例,所以有時取到老數據,並且value飄忽不定
(2).高併發下穩定性問題
php memcache換成php memcached,在高併發下穩定下極大提高;
(3).秒超時間隔沒法修改問題
php memcache客戶端有個1秒超時間隔沒法修改問題:bool Memcache::connect ( string $host [, int $port [, int $timeout ]] )第三個參數本來可設置timeout,單位秒,但無法修改。
測試了以下三種修改timeout的方法都無效:
用memcache api Memcache::setServerParams不能修改;
改memcache 源代碼vi php_memcache.h宏定義不能修改;
php.ini內這個配置:default_socket_timeout = 60對本timeout無效。
(4).memcache和memcached對比
注,PHP memcache這個老客戶端在屬性設置方面可設置的很少,出錯碼粒度很粗,出錯後難以定位,而且功能欠缺一些,如下圖:
注,綜合上述建議大家使用php memcached。還有更多知識點請參考官方文
3、PHP 安裝memcached擴展
[root@hpf-linux ~]# yum -y install gcc+ gcc-c++
解決memcached的依賴關係需要安裝libmemcached,注意這裏最好安裝1.0.16版本,我試過17、18版本一直無法裝上:
[root@hpf-linux ~]# tar -xf libmemcached-1.0.16.tar.gz [root@hpf-linux ~]# cd libmemcached-1.0.16 [root@hpf-linux libmemcached-1.0.16]# mkdir /usr/local/libmemcached [root@hpf-linux libmemcached-1.0.16]# ./configure -prefix=/usr/local/libmemcached --with-memcached [root@hpf-linux libmemcached-1.0.16]# make [root@hpf-linux libmemcached-1.0.16]# make install
安裝PHP memcached擴展:
[root@hpf-linux ~]# tar -xf memcached-2.2.0.tgz [root@hpf-linux ~]# cd memcached-2.2.0 [root@hpf-linux memcached-2.2.0]# /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 [root@hpf-linux memcached-2.2.0]# ./configure --enable-memcached --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl [root@hpf-linux memcached-2.2.0]# make && make install Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
添加配置文件:
[root@hpf-linux ~]# vim /etc/php.d/memcached.ini extension = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/memcached.so"
添加測試頁:
[root@hpf-linux ~]# cat /www/a.com/index.php <?php phpinfo(); ?>
瀏覽器測試:
4、PHP 安裝memcache擴展
PECL 簡介
PECL 的全稱是 The PHP Extension Community Library ,是一個開放的並通過 PEAR(PHP Extension and Application Repository,PHP 擴展和應用倉庫)打包格式來打包安裝的 PHP 擴展庫倉庫。通過 PEAR 的 Package Manager 的安裝管理方式,可以對 PECL 模塊進行下載和安裝。與以往的多數 PEAR 包不同的是,PECL 擴展包含的是可以編譯進 PHP Core 的 C 語言代碼,因此可以將 PECL 擴展庫編譯成爲可動態加載的 .so 共享庫,或者採用靜態編譯方式與 PHP 源代碼編譯爲一體的方法進行擴展。PECL 擴展庫包含了對於 XML 解析,數據庫訪問,郵件解析,嵌入式的 Perl 以及 Pthyon 腳本解釋器等諸多的 PHP 擴展模塊,因此從某種意義上來說,在運行效率上 PECL 要高於以往諸多的 PEAR 擴展庫。
PECL常用選項:
search 查找一下模塊
info 查看模塊信息
install 安裝查找到的模塊
使用PECL安裝memcache模塊:
[root@hpf-linux ~]# /usr/local/php/bin/pecl install memcache Build process completed successfully Installing '/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/memcache.so' install ok: channel://pecl.php.net/memcache-2.2.7 configuration option "php_ini" is not set to php.ini location You should add "extension=memcache.so" to php.ini
接下來我們來增加php memcache的配置文件:
[root@hpf-linux ~]# vim /etc/php.d/memcache.ini extension = "memcache.so" [root@hpf-linux ~]# service php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done
下面我們來查看一下php的模塊:
[root@hpf-linux ~]# /usr/local/php/bin/php -m |grep memcache memcache memcached
瀏覽器測試:
添加Memcached的測試腳本:
[root@hpf-linux a.com]# cat memcached.php <?php $mem = new Memcache; $mem->connect("192.168.1.6", 11211) or die("Could not connect"); $version = $mem->getVersion(); echo "Server's version: ".$version."<br/>\n"; $mem->set('hellokey', 'Hello World', 0, 600) or die("Failed to save data at the memcached server"); echo "Store data in the cache (data will expire in 600 seconds)<br/>\n"; $get_result = $mem->get('hellokey'); echo "$get_result is from memcached server."; ?>
[root@hpf-linux ~]# telnet 192.168.1.6 11211 Trying 192.168.1.6... Connected to 192.168.1.6. Escape character is '^]'. get hellokey VALUE hellokey 0 11 Hello World END
二、Memcache 應用二 nginx與memcache
1、memcached_module模塊
nginx的memcached_module模塊可以直接從memcached服務器中讀取內容後輸出,後續的請求不再經過應用程序處理,如php-fpm、django,大大的提升動態頁面的速度。nginx只負責從memcached服務器中讀取數據,要往memcached寫入數據還得需要後臺的應用程序來完成,主動的將要緩存的頁面緩存到memcached中,可以通過404重定向到後端去處理的。ngx_http_memcached_module可以操作任何兼用memcached協議的軟件。如ttserver、membase等。
結構圖如下:
memcached的key可以通過memcached_key變量來設置,如以$uri。如果命中,那麼直接輸出內容,沒有命中就意味着nginx需要從應用程序請求頁面。同時,我們還希望該應用程序將鍵值對寫入到memcached,以便下一個請求可以直接從memcached獲取。如果鍵值不存在,nginx將報告not found錯誤。最好的方法是使用error_page指定和location請求處理。同時包含”Bad Gateway”錯誤和”Gateway Timeout”錯誤,如:error_page 404 502 504 = @app;。注意:需要設置default_type,否則可能會顯示不正常。
2、模塊說明
memcached_bind :指定從哪個IP來連接memcached服務器
語法: memcached_bind address | off;
默認值: none
配置段: http, server, location
memcached_buffer_size :讀取從memcached服務器接收到響應的緩衝大小。儘快的將響應同步傳給客戶端。
語法: memcached_buffer_size size;
默認值: 4k|8k;
配置段: http, server, location
memcached_connect_timeout :與memcached服務器建立連接的超時時間。通常不超過75s。
語法:memcached_connect_timeout time;
默認值:60s;
配置段:http, server, location
memcached_gzip_flag :測試memcached服務器響應標誌。如果設置了,將在響應頭部添加了Content-Encoding:gzip。
語法:memcached_gzip_flag flag;
默認值:none
配置段:http, server, location
memcached_next_upstream :指定在哪些狀態下請求將轉發到另外的負載均衡服務器上,僅當memcached_pass有兩個或兩個以上時使用。
語法: memcached_next_upstream error | timeout | invalid_response | not_found | off …;
默認值: error timeout;
配置段: http, server, location
memcached_pass :指定memcached服務器地址。使用變量$memcached_key爲key查詢值,如果沒有相應的值則返回error_page 404。
語法:memcached_pass address:port or socket;
默認值:none
配置段:location, if in location
memcached_read_timeout :定義從memcached服務器讀取響應超時時間。
語法:memcached_read_timeout time;
默認值:60s;
配置段:http, server, location
memcached_send_timeout:設置發送請求到memcached服務器的超時時間。
語法:memcached_send_timeout
默認值:60s
配置段:http, server, location
用法:$memcached_key變量:memcached key的值。
3、nginx memcached的增強版ngx_http_enhanced_memcached_module基於nginx memcached 模塊的,添加的新特性有:
自定義HTTP頭,如Content-Type, Last-Modified。
hash鍵可超過250個字符,memcached受限。
通過HTTP請求將數據存儲到memcached。
通過HTTP請求從memcached刪除數據。
通過HTTP請求清除所有memcached緩存數據。
通過HTTP請求獲取memcached狀態數據。
鍵名空間管理,來部分刷新緩存。
緩存通過If-Modified-Since頭和內容Last-Modified來回復304Not Modified請求。
4、nginx整合memcache
server { listen 80; server_name www.test.com; #charset koi8-r; #access_log logs/host.access.log main; location / { set $memcached_key $uri; memcached_pass 192.168.1.6:11211; default_type text/html; error_page 404 @fallback; } location @fallback { proxy_pass http://192.168.1.9; } }
從上面的配置文件我們可以看出,一個請求到達後,會其uri作爲key去Memcached服務器192.168.1.6:11211上查找value,如果沒有命中,則返回404。這時通過error_page將404接收轉到@fallback,返回給Web服務去處理請求。
三、Memcache 應用三 session與memcache
1、配置node1與node2安裝php memcache擴展
注,上面已經演示過怎麼安裝php memcache擴展,這裏就不再演示。
2、安裝Memcache服務器
注,同上大家自己安裝一下。
3、修改php配置文件
[root@hpf-linux ~]# vim /etc/php.ini session.save_handler = memcache session.save_path = "tcp://192.168.1.6:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
4、增加測試頁面
新建php頁面setsess.php,爲客戶端設置啓用session。
<?php session_start(); if (!isset($_SESSION['www.example.com'])) { $_SESSION['www.example.com'] = time(); } print $_SESSION['www.example.com']; print "<br><br>"; print "Session ID: " . session_id(); ?>
說明:新建php頁面showsess.php,獲取當前用戶的會話ID。
<?php session_start(); $memcache_obj = new Memcache; $memcache_obj->connect('192.168.1.6', 11211); $mysess=session_id(); var_dump($memcache_obj->get($mysess)); $memcache_obj->close(); ?>
四、memcache 圖形管理工具
1、解壓memadmin
[root@hpf-linux ~]# tar -xf memadmin-1.0.12.tar.gz [root@hpf-linux ~]# cd memadmin
2、移動memadmin到頁面目錄下
[root@hpf-linux memadmin]# mv ../memadmin /www/a.com/ [root@hpf-linux memadmin]# cd /www/a.com/
3、查看一下配置文件
[root@hpf-linux a.com]# vim memadmin/config.php $config['user'] = "admin"; // your username $config['passwd'] = "admin"; // your password
默認的用戶名是admin,密碼是admin。
4、瀏覽器測試