MemCache对PHP和Nginx的缓存加速优化

一、MemCache

[1]. 简介

memcache是一个高性能的分布式的内存对象缓存系统,用于动态Web应用以减轻数据库负担。
memcache通过在内存里维护一个统一的巨大的hash表,来存储经常被读写的一些数组与文件,从而极大的提高网站的运行效率。
memcache是一种内存缓存技术,是一种缓存手段,要看情况来使用。
对于频繁读取,每次读取重复率高,数据更新频度低的数据,用memcache可以优化你的系统响应速度。

[2]. 操作流程

MemCache对PHP和Nginx的缓存加速优化_第1张图片
  1、检查客户端的请求数据是否在memcached中,如有,直接把请求数据返回,不再对数据库进行任何操作,路径操作为①②③⑦。
  2、如果请求的数据不在memcached中,就去查数据库,把从数据库中获取的数据返回给客户端,同时把数据缓存一份到memcached中(memcached客户端不负责,需要程序明确实现),路径操作为①②④⑤⑦⑥。
  3、每次更新数据库的同时更新memcached中的数据,保证一致性。
  4、当分配给memcached内存空间用完之后,会使用LRU(Least Recently Used,最近最少使用)策略加上到期失效策略,失效数据首先被替换,然后再替换掉最近未使用的数据。

[3]. memcache使用场景:
  1. 访问频繁的字典数据
  2. 大量的hot数据
  3. 页面缓存
  4. 频繁的查询条件和结果
  5. 临时处理的数据

二、memcache对php访问页面的加速优化

[1].设php命令的路径
[root@server1 ~]# vim   .bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/nginx/sbin/:/usr/local/lnmp/php/bin
[root@server1 ~]# source .bash_profile
[2]. 预编译环境
[root@server1 ~]# tar zxf  memcache-2.2.5.tgz 
[root@server1 ~]# cd  memcache-2.2.5
[root@server1 memcache-2.2.5]# phpize 
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
[3]. 源码编译
[root@server1 memcache-2.2.5]# ./configure --prefix=/usr/local/lnmp/php/memcache
[root@server1 memcache-2.2.5]# make && make install
[4].给php添加memcache
[root@server1 memcache-2.2.5]# php -m |grep memcache  #此时无结果
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/php/etc/
[root@server1 etc]# vim  php.ini 
[root@server1 etc]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
[root@server1 etc]# php -m |grep memcache  #此时支持memcache
memcache

MemCache对PHP和Nginx的缓存加速优化_第2张图片

[5].安装memcache工具并启动memcache
[root@server1 memcache-2.2.5 ]# yum install -y memcached
[root@server1 memcache-2.2.5 ]# /etc/init.d/memcached start
Starting memcached:                                        [  OK  ]

MemCache对PHP和Nginx的缓存加速优化_第3张图片

[6].测试文件cp到/usr/local/lnmp/nginx/html
[root@server1 memcache-2.2.5]# cp  memcache.php   example.php /usr/local/lnmp/nginx/html/
[root@server1 memcache-2.2.5]# cd  /usr/local/lnmp/nginx/html/
[root@server1 html]# vim  memcache.php 

MemCache对PHP和Nginx的缓存加速优化_第4张图片

[7].测试:172.25.254.1/memcache.php

MemCache对PHP和Nginx的缓存加速优化_第5张图片
MemCache对PHP和Nginx的缓存加速优化_第6张图片

当访问多次example.php后,再访问memcache.php命中率会提高
在这里插入图片描述

MemCache对PHP和Nginx的缓存加速优化_第7张图片

[8].压力测试:加速页面example.php和没加速index.php的对比(错误率对比,相应时间对比)
[root@foundation96 lamp]# ab -c 10 -n 1000 http://172.25.254.1/example.php

MemCache对PHP和Nginx的缓存加速优化_第8张图片


[root@foundation96 lamp]# ab -c 10 -n 1000 http://172.25.254.1/index.php

MemCache对PHP和Nginx的缓存加速优化_第9张图片

三、memcache对nginx访问页面的加速优化

[1]memcache原理

nginx本身具有高并发的特点,如果将数据缓存放在php后面,则客户请求发给nginx,nginx给php-fpm处理,然后将获取的数据缓存到memcache上,则nginx只有在等待php-fpm处理结束后,必定会影响数据传输速率,如果将memcache直接与nginx连接,当客户发出请求时,nginx直接从memcahce中将数据出给客户端,即可提高速率,这里我们使用openresty来实现。
MemCache对PHP和Nginx的缓存加速优化_第10张图片
在php+memcache模块中,即使memcahce命中,还是要进入memcahce生命周期,效率并不高,因此一种更高效的缓存策略是nginx直接访问memcache即使用openresty,并用 u r i 和 uri和 uriargs等nginx内置变量设定缓存key规则,这样当命中缓存时,nginx可以跳过通过fastcgi和php通信过程,直接从memcache中获取数据并返回,提高效率。
MemCache对PHP和Nginx的缓存加速优化_第11张图片

[2]. 解压、源码编译:
[root@server1 conf]# nginx -s stop  # 关闭之前的nginx
[root@server1 ~]# tar  zxf  openresty-1.13.6.1.tar.gz
[root@server1 ~]# cd openresty-1.13.6.1
[root@server1 openresty-1.13.6.1]# ./configure –prefix=/opt/nginx –with-http_ssl_module –with-http_stub_status_module –user=nginx –group=nginx –with-threads –with-file-aio
[root@server1 openresty-1.13.6.1]# gmake && gmake install
[3]. 修改openresty中的nginx的配置文件
[root@server1 openresty-1.13.6.1]# cd  /usr/local/lnmp/openresty/nginx/conf/
[root@server1 conf]# vim  nginx.conf
 12 events {
 13     worker_connections  65535;
 14 }
 17 http {
 18         upstream memcache {
 19         server localhost:11211;
 20         keepalive 512 ;
 21         }
 22     include       mime.types;
 23     default_type  application/octet-stream;

 51         location /memc {
 52         internal;
 53         memc_connect_timeout 100ms;
 54         memc_send_timeout 100ms;
 55         memc_read_timeout 100ms;
 56         set $memc_key $query_string;
 57         set $memc_exptime 300;
 58         memc_pass memcache;
 59         }
 77         location ~ \.php$ {
 78             set $key $uri$args;
 79             srcache_fetch GET /memc $key;
 80             srcache_store PUT /memc $key;
 81             root           html;
 82             fastcgi_pass   127.0.0.1:9000;
 83             fastcgi_index  index.php;
 84             # fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 85             include        fastcgi.conf;
 86         }
[root@server1 conf]#  /opt/nginx/nginx/sbin/nginx  -t
nginx: the configuration file /usr/local/lnmp/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/openresty/nginx/conf/nginx.conf test is successful
[root@server1 conf]# /opt/nginx/nginx/sbin/nginx  -s reload
[root@server1 conf]# /opt/nginx/nginx/sbin/nginx 

MemCache对PHP和Nginx的缓存加速优化_第12张图片

[4]. 将之前的测试文件拷贝到当前
[root@server1 html]# cp /usr/local/lnmp/nginx/html/example.php .
[root@server1 html]# cp /usr/local/lnmp/nginx/html/index.php .
[5]. 压力测试:(example.php比index.php好很多)
[root@foundation96 lamp]# ab -c 10 -n 1000 http://172.25.254.1/index.php

MemCache对PHP和Nginx的缓存加速优化_第13张图片

[root@foundation96 lamp]# ab -c 10 -n 1000 http://172.25.254.1/example.php

MemCache对PHP和Nginx的缓存加速优化_第14张图片

你可能感兴趣的:(MemCache对PHP和Nginx的缓存加速优化)