memcache对php页面访问的加速

一、Memcache简介

内存缓存技术:memcache是实现php语言 对内存 进行操作的中间介质

MemCache的工作流程如下:先检查客户端的请求数据是否在memcached中,如有,直接把请求数据返回,不再对数据库进行任何操作;如果请求的数据不在memcached中,就去查数据库,把从数据库中获取的数据返回给客户端,同时把数据缓存一份到 memcached中(memcached客户端不负责,需要程序明确实现);每次更新数据库的同时更新memcached中的数据,保证一致性;当分配给memcached内存空间用完之后,会使用LRU(Least Recently Used,最近最少使用)策略加上到期失效策略,失效数据首先被替换,然后再替换掉最近未使用的数据

二、Memcache和memcached的区别

Memcache是这个项目的名称,而memcached是它服务器端的主程序文件名。

三、Memcache的服务器端和客户端安装(向php中添加模块):

1.解压memcache源码包:

[root@server1 ~]# tar zxf memcache-2.2.5.tgz

2.将前边php编译完成的二进制命令加入环境变量中,保证可以直接调用php命令

[root@server1 ~]# vim ~/.bash_profile   添加环境变量
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
[root@server1 ~]# source ~/.bash_profile  刷新环境变量

[root@server1 ~]# cd /usr/local/lnmp/php/  查看
[root@server1 php]# ls
bin  etc  include  lib  php  sbin  var
[root@server1 php]# cd bin/
[root@server1 bin]# ls
pear  peardev  pecl  phar  phar.phar  php  php-cgi  php-config  phpize

3.创建一个预编译环境并进行编译汇编memcache源码包
phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块,比如你想在原来编译好的php中加入memcached或者ImageMagick等扩展模块,可以使phpize。
或者说,phpize的作用可以这样理解:侦测环境(phpize工具是在php安装目录下,基于这点phpize对应了当时的php环境,所以是要根据该php的配置情况生成对应的configure文件),建立一个configure文件。必须在一个目录下去运行phpize。那么phpize就知道你的的环境是哪个目录,并且configure文件建立在该目录下。  
 

[root@server1 ~]# cd memcache-2.2.5
[root@server1 memcache-2.2.5]# ls
config9.m4   memcache.c                  memcache_queue.h
config.m4    memcache_consistent_hash.c  memcache_session.c
config.w32   memcache.dsp                memcache_standard_hash.c
CREDITS      memcache.php                php_memcache.h
example.php  memcache_queue.c            README
[root@server1 memcache-2.2.5]# phpize
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
[root@server1 memcache-2.2.5]# ls
acinclude.m4    configure.in                memcache.php
aclocal.m4      config.w32                  memcache_queue.c
autom4te.cache  CREDITS                     memcache_queue.h
build           example.php                 memcache_session.c
config9.m4      install-sh                  memcache_standard_hash.c
config.guess    ltmain.sh                   missing
config.h.in     Makefile.global             mkinstalldirs
config.m4       memcache.c                  php_memcache.h
config.sub      memcache_consistent_hash.c  README
configure       memcache.dsp                run-tests.php

4.源码编译安装

[root@server1 memcache-2.2.5]# ./configure   # 源码编译,已经安装好了php,我们就不用再指定新的php扩展模块路径
[root@server1 memcache-2.2.5]# make && make install

5.编译完成后,提示我们进入php/extensions/no-debug-non-zts-20131226/目录,在这个目录中的我们可以 查看php中已经添加的模块,发现还没有我们的memcache模块,因此我们去到php的配置文件中添加memcache模块。

[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226/
[root@server1 no-debug-non-zts-20131226]# ls
memcache.so  opcache.a  opcache.so
[root@server1 no-debug-non-zts-20131226]# php -m | grep memcache   过滤memcache模块,发现现在还没有
[root@server1 no-debug-non-zts-20131226]# cd /usr/local/lnmp/php/etc/
[root@server1 etc]# vim php.ini        # 编辑php的配置文件
873 extension=memcache.so    # 将873行memcache模块的注释打开
[root@server1 etc]# /etc/init.d/php-fpm reload   # 重载php
Reload service php-fpm  done
[root@server1 etc]# php -m | grep memcache   # 再次过滤memcache模块
memcache

6.安装memcache服务端

yum install memcached           # 安装memcached服务端
/etc/init.d/memcached start     # 打开memcached服务
netstat -antlp   # memcached监听11211端口

测试:

[root@server1 ~]# telnet localhost 11211
set name 0 0 6
westos
STORED
get name
VALUE name 0 6
westos
END
delete name
DELETED
get name
END
quit  退出

7.设置memcached的密码

root@server1 ~]# cd memcache-2.2.5
[root@server1 memcache-2.2.5]# cp memcache.php example.php /usr/local/lnmp/nginx/html/
# 将memcache.php example.php复制到nginx的默认发布目录
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/nginx/html/
[root@server1 html]# ls
50x.html  bbs  example.php  index.html  index.php  memcache.php  readme  utility
[root@server1 html]# vim example.php     # 仅仅查看,是php代码
[root@server1 html]# vim memcache.php 

访问example.php:

访问memcache.php:

输入密码后:(右边可以看到HIT命中率)

(3)在浏览器处多次访问example.php,查看命中情况为99.5%。

8.访问测试:(真机测试)

(1)访问 index.php后,我们可以分析一下浏览器的并发行和出错情况

(2)按照同样的测试方法,我们测试访问example.php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值