memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问速度提升效果十分显著 。这是一套开放源代码软件,以BSD license授权发布。
MemCache的工作流程如下:
先检查客户端的请求数据是否在memcached中,如有,直接把请求数据返回,不再对数据库进行任何操作;如果请求的数据不在memcached中,就去查数据库,把从数据库中获取的数据返回给客户端,同时把数据缓存一份到memcached中(memcached客户端不负责,需要程序明确实现);每次更新数据库的同时更新memcached中的数据,保证一致性;当分配给memcached内存空间用完之后,会使用LRU(Least Recently Used,最近最少使用)策略加上到期失效策略,失效数据首先被替换,然后再替换掉最近未使用的数据。
安装memcache
1.获取安装包,配置环境变量
[root@server1 ~]# vim ~/.bash_profile
PATH=$PATH:$HOME/bin
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
[root@server1 ~]# source ~/.bash_profile
2.解压
tar zxf memcache-2.2.5.tgz
3.编译,安装
cd memcache-2.2.5
phpize #生成configure
./configure
make && make install
4.修改文件
cd /usr/local/lnmp/php/etc/
vim php.ini
873 extension=memcache.so
/etc/init.d/php-fpm reload
5.查看可使用的模块
php -m
[root@server1 etc]# php -m | grep mysql
mysql
mysqli
mysqlnd
pdo_mysql
[root@server1 etc]# php -m | grep memcache
memcache
6.安装memcached
yum install memcached -y
7.查看支持那个主机的端口
vim /etc/sysconfig/memcached
8.开启服务,查看端口
[root@server1 ~]# /etc/init.d/memcached start
Starting memcached: [ OK ]
[root@server1 ~]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:11211 0.0.0.0:*
8.查看端口状态
telnet localhost 11211
stats
没有服务就下载
yum install -y telnet-0.17-47.el6_3.1.x86_64
端口可用
9.将发布页复制
cd memcache-2.2.5
cp example.php memcache.php /usr/local/lnmp/nginx/html/
10.修改发布页
vim memcache.php
21
22 define('ADMIN_USERNAME','memcache'); // Admin Username
23 define('ADMIN_PASSWORD','redhat'); // Admin Password
24 define('DATE_FORMAT','Y/m/d H:i:s');
25 define('GRAPH_SIZE',200);
26 define('MAX_ITEM_DUMP',50);
27
28 $MEMCACHE_SERVERS[] = '172.25.80.1:11211'; // add more as an array
29
11.测试
在浏览器
http://172.25.80.1/memcache.php
查看命中率
http://172.25.80.1/example.php
多刷新这个页面,再测试
可以看出名中率非常高
在客户端
ab -c 10 -n 5000 http://172.25.80.1/index.php #不使用memcache,慢
[root@foundation80 ~]# ab -c 10 -n 5000 http://172.25.80.1/index.php
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 172.25.80.1 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software: nginx/
Server Hostname: 172.25.80.1
Server Port: 80
Document Path: /index.php
Document Length: 84786 bytes
Concurrency Level: 10
Time taken for tests: 33.882 seconds
Complete requests: 5000
Failed requests: 510
(Connect: 0, Receive: 0, Length: 510, Exceptions: 0)
Write errors: 0
Total transferred: 424714433 bytes
HTML transferred: 423929433 bytes
Requests per second: 147.57 [#/sec] (mean)
Time per request: 67.764 [ms] (mean)
Time per request: 6.776 [ms] (mean, across all concurrent requests)
Transfer rate: 12241.28 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 14 110.0 3 3008
Processing: 8 54 32.4 48 780
Waiting: 2 13 15.6 8 227
Total: 8 68 115.3 52 3089
Percentage of the requests served within a certain time (ms)
50% 52
66% 59
75% 65
80% 71
90% 89
95% 115
98% 172
99% 1034
100% 3089 (longest request)
ab -c 10 -n 5000 http://172.25.80.1/example.php #使用memcache,快
[root@foundation80 ~]# ab -c 10 -n 5000 http://172.25.80.1/example.php
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 172.25.80.1 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software: nginx/
Server Hostname: 172.25.80.1
Server Port: 80
Document Path: /example.php
Document Length: 123 bytes
Concurrency Level: 10
Time taken for tests: 3.790 seconds
Complete requests: 5000
Failed requests: 0
Write errors: 0
Total transferred: 1400000 bytes
HTML transferred: 615000 bytes
Requests per second: 1319.10 [#/sec] (mean)
Time per request: 7.581 [ms] (mean)
Time per request: 0.758 [ms] (mean, across all concurrent requests)
Transfer rate: 360.69 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.6 0 6
Processing: 1 7 4.2 6 44
Waiting: 1 7 4.0 6 43
Total: 2 8 4.2 7 44
Percentage of the requests served within a certain time (ms)
50% 7
66% 8
75% 9
80% 9
90% 12
95% 15
98% 20
99% 26
100% 44 (longest request)