简介
memcached是一套分布式的高速缓存系统,memcached缺乏认证以及安全管制,这代表应该将memcached服务器放置在防火墙后。memcached的API使用三十二比特的循环冗余校验(CRC-32)计算键值后,将数据分散在不同的机器上。当表格满了以后,接下来新增的数据会以LRU机制替换掉。由于memcached通常只是当作缓存系统使用,所以使用memcached的应用程序在写回较慢的系统时(像是后端的数据库)需要额外的代码更新memcached内的数据
特征
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 <?php phpinfo(); ?>
访问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 <?php $fn=dirname(__FILE__) . $_SERVER['REQUEST_URI']; if(file_exists($fn)){ $data=file_get_contents($fn); $m=new Memcached(); $server=array( array('192.168.29.133',11211) ); $m->addServers($server); $r=$m->set($_GET['key'],$data); header('Content-Length:'.filesize($fn)."\r\n"); header('Content-Type:file'."\r\n"); header('X-cache:MISS:'."\r\n"); echo $data; } #不存在demo文件夹则返回首页 else{ header('Location:../index.html'."\r\n"); } ?>
注: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| +----------------------------------------------------------------------+ */ //memcached图形化小工具 $VERSION='$Id: memcache.php,v 1.1.2.3 2008/08/28 18:07:54 mikl Exp $'; #设置用户名 define('ADMIN_USERNAME','admin'); #设置密码 define('ADMIN_PASSWORD','123456'); define('DATE_FORMAT','Y/m/d H:i:s'); define('GRAPH_SIZE',200); define('MAX_ITEM_DUMP',50); #设置memcached主机信息 $MEMCACHE_SERVERS[] = '192.168.29.133:11211'; ////////// END OF DEFAULT CONFIG AREA ///////////////////////////////////////////////////////////// ///////////////// Password protect //////////////////////////////////////////////////////////////// if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_USER'] != ADMIN_USERNAME ||$_SERVER['PHP_AUTH_PW'] != ADMIN_PASSWORD) { Header("WWW-Authenticate: Basic realm=\"Memcache Login\""); Header("HTTP/1.0 401 Unauthorized"); echo << Rejected!
Wrong Username or Password!