MemCache对PHP页面的缓存加速优化
1.memcache简介
memcache是一个高性能的分布式的内存对象缓存系统,用于动态Web应用以减轻数据库负担。
memcache通过在内存里维护一个统一的巨大的hash表,来存储经常被读写的一些数组与文件,从而极大的提高网站的运行效率。
memcache是一种内存缓存技术,是一种缓存手段,要看情况来使用。
对于频繁读取,每次读取重复率高,数据更新频度低的数据,用memcache可以优化你的系统响应速度。
2.配置安装
(1)官网下载安装包并解压
[[email protected] ~]# tar zxf memcache-2.2.5.tgz
[[email protected] ~]# ls
(2)将前边php编译完成的二进制命令加入环境变量中,保证可以直接调用php命令
vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
source ~/.bash_profile
效果:php命令现在可以自动补齐(tab)
[[email protected] ~]# php
php php-cgi php-config phpize
(3)创建一个预编译环境并对memcache的源码包进行编译
phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块,比如你想在原来编译好的php中加入memcached或者ImageMagick等扩展模块,可以使用phpize。
phpize工具是在php安装目录下,基于这点phpize对应了当时的php环境,所以是要根据该php的配置情况生成对应的configure文件,建立一个configure文件。必须在一个目录下去运行phpize,那么phpize就知道你的的环境是哪个目录,并且configure文件建立在该目录下。
在memcache目录下创建预编译环境
[[email protected] memcache-2.2.5]# phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
[[email protected] memcache-2.2.5]# ls
编译安装
[[email protected] memcache-2.2.5]# ./configure
[[email protected] memcache-2.2.5]# make && make install
[[email protected] memcache-2.2.5]# ls
(4)在php文件中添加memcache模块
[[email protected] memcache-2.2.5]# cd
[[email protected] ~]# cd /usr/local/lnmp/php/etc/
[[email protected] etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default php.ini
[[email protected] etc]# vim php.ini
873 extension=memcache.so ##开启memcache模块
重新加载php-fpm服务
[[email protected] etc]# /etc/init.d/php-fpm reload
Reload service php-fpm done
(5)安装memcache工具,开启memcache服务
[[email protected] etc]# cd
[[email protected] ~]# yum install -y memcache-2.2.5
[[email protected] ~]# /etc/init.d/memcached start
Starting memcached: [ OK ]
[[email protected] ~]# netstat -tnlp
(6)安装telnet工具,测试
[[email protected] ~]# yum install -y telnet
[[email protected] ~]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
创建westos,查看,没有失效,删除,再次查看失效,退出(quit)
0 0 6
编号 缓存时间 限制的字符数
[[email protected] ~]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
set name 0 5 6 ##设置缓存时间为5秒
westos ##名字
STORED
get name ##5秒内查看
VALUE name 0 6
westos
END
get name ##5秒后查看
END
quit ##退出
Connection closed by foreign host.
(7)将测试文件复制到/usr/local/lnmp/nginx/html/文件下
[[email protected] memcache-2.2.5]# cp example.php memcache.php /usr/local/lnmp/nginx/html/
[[email protected] memcache-2.2.5]# cd /usr/local/lnmp/nginx/html/
[[email protected] html]# ls
修改memcache的密码和访问地址
[[email protected] html]# vim memcache.php
22 define('ADMIN_USERNAME','memcache'); // Admin Username用户名
23 define('ADMIN_PASSWORD','redhat'); // Admin Password密码
28 $MEMCACHE_SERVERS[] = '172.25.26.1:11211'; // add more as an array访问地址
nginx -s reload ##重新加载服务
浏览器访问http://172.25.26.1/memcache.php 要输入用户和密码,可以看到现在命中率是50%然后重新打开一个浏览器页面,输入172.2.26.1/example.php ,不断刷新
当访问多次example.php后,查看命中情况会不断提高,最后将会达到百分之百。 如下图:
(8)安装ab命令,压力测试,模拟5000请求量
加速页面example.php和没加速index.php的对比(错误率对比,相应时间对比)
[[email protected] html]# yum whatprovides *ab
[[email protected] html]# yum install -y httpd-tools-2.2.15-29.el6_4.x86_64
[[email protected] ~]# ab -c 10 -n 5000 http://172.25.26.1/index.php
[[email protected] ~]# ab -c 10 -n 5000 http://172.25.26.1/example.php