簡介
memcached是一套分佈式的高速緩存系統,memcached缺乏認證以及安全管制,這代表應該將memcached服務器放置在防火牆後。memcached的API使用三十二比特的循環冗餘校驗(CRC-32)計算鍵值後,將數據分散在不同的機器上。當表格滿了以後,接下來新增的數據會以LRU機制替換掉。由於memcached通常只是當作緩存系統使用,所以使用memcached的應用程序在寫回較慢的系統時(像是後端的數據庫)需要額外的代碼更新memcached內的數據
特徵
memcached作為高速運行的分佈式緩存服務器,具有以下的特點:
協議簡單
基於libevent的事件處理
內置內存存儲方式
memcached不互相通信的分佈式
前期準備
準備三臺Centos7虛擬機,配置IP地址和hostname,關閉防火牆和selinux,同步系統時間,修改IP地址和hostname映射
ip | hostname |
---|---|
192.168.29.132 | master |
192.168.29.138 | bak |
192.168.29.133 | mid |
master和bak機器部署Nginx和PHP
部署memcache
mid機器部署memcached客戶端
[root@mid ~]# yum install memcached -y #啟動服務 [root@mid ~]# systemctl start memcached.service #查看啟動情況,點擊回車出現ERROR則啟動成功 [root@master ~]# telnet 192.168.29.133 11211 Trying 192.168.29.133... Connected to 192.168.29.133. Escape character is '^]'. ERROR
master和mid機器部署PHP的memcached擴展
下載libmemcached和memcached壓縮包
#解壓並安裝libmemcached [root@master ~]#tar -xvf libmemcached-1.0.18.tar.gz [root@master ~]#cd libmemcached-1.0.18 #若編譯報錯,將clients/memflush.cc中報錯相應位置的false改為NULL [root@master ~]#./configure --prefix=/usr/local/libmemcached make && make install #解壓並安裝memcached [root@master ~]#tar -zxvf memcached-3.1.5.tgz [root@master ~]#cd memcached-3.1.5 [root@master ~]#phpize [root@master ~]#./configure --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl [root@master ~]#make && make install #完成後觀察php目錄下的lib/php/extensions/no-debug-zts-20170718/是否有擴展 memcached.so #添加擴展至php配置文件 [root@master ~]# vi /usr/local/php/etc/php.ini extension=memcached.so
測試驗證
[root@master ~]# vi /usr/local/nginx/html/test.php
訪問http://ip/test.php
注:bak機器進行相同操作
配置緩存
配置Nginx配置文件
[root@master ~]# cat /usr/local/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } #memcached緩存配置,有緩存則讀取,沒有緩存則報404錯誤 location /demo/ { set $memcached_key $request_uri; add_header X-mem-key $memcached_key; memcached_pass 192.168.29.133:11211; default_type text/html; #報錯後轉到特定Location error_page 404 502 504 = @mymemcached; } #配置重寫策略執行特定php文件 location @mymemcached { rewrite .* /demo.php?key=$request_uri; } location ~ .php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } }
編寫PHP文件設置memcached緩存
#創建demo文件夾 [root@master ~] mkdir /usr/local/nginx/html/demo #創建測試文件 [root@master ~] echo "123" >> /usr/local/nginx/html/demo/123.html [root@master ~]# vi /usr/local/nginx/html/demo.php addServers($server); $r=$m->set($_GET['key'],$data); header('Content-Length:'.filesize($fn)." "); header('Content-Type:file'." "); header('X-cache:MISS:'." "); echo $data; } #不存在demo文件夾則返回首頁 else{ header('Location:../index.html'." "); } ?>
注:bak機器進行相同的設置
測試驗證
#可看出第一次memcache中沒有緩存,第二次擊中緩存 [root@bak ~]# curl -I http://192.168.29.132/demo/123.html HTTP/1.1 200 OK Server: nginx/1.16.1 Date: Thu, 25 Jun 2020 02:23:00 GMT Content-Type: file Content-Length: 4 Connection: keep-alive X-Powered-By: PHP/7.2.26 X-cache: MISS: [root@bak ~]# curl -I http://192.168.29.132/demo/123.html HTTP/1.1 200 OK Server: nginx/1.16.1 Date: Thu, 25 Jun 2020 02:23:01 GMT Content-Type: text/html Content-Length: 4 Connection: keep-alive X-mem-key: /demo/123.html Accept-Ranges: bytes #當設置緩存後,訪問相同的緩存key時,即使發起訪問的機器不相同也同樣能擊中緩存 [root@master ~]# curl -I http://192.168.29.138/demo/123.html HTTP/1.1 200 OK Server: nginx/1.16.1 Date: Thu, 25 Jun 2020 02:29:46 GMT Content-Type: text/html Content-Length: 4 Connection: keep-alive X-mem-key: /demo/123.html Accept-Ranges: bytes
查看memcached緩存狀態
memcached監控文件
<?php /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2004 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.0 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_0.txt. | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Harun Yayli
[zmcjlove ] 基於Nginx的Mencached緩存配置詳解已經有301次圍觀