2.18 memcached---php的memcached缓存,nginx的memcache缓存

一、memcache的简介

Memcached 是一个高性能的分布式内存对象缓存系统,用于动态 Web 应用以减轻
数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、
数据库驱动网站的速度。Memcached 基于一个存储键/值对的 hashmap。
Memcached 是一个自由、源码开放、高性能、分布式内存对象缓存系统,目的在
于通过减轻数据库负载来使动态 Web 应用程序提速。
Memcached 是一个在内存中对任意的数据(比如字符串,对象等)所使用的 key-value
存储。数据可以来自数据库调用,API 调用,或者页面渲染的结果。
Memcached 设计理念就是小而强大,它简单的设计促进了快速部署、易于开发,并
解决面对大规模的数据缓存的许多难题。所开放的 API 能用于大部分流行的程序语言

交叉存储

二、php的memcached缓存

1、解压memcache安装包

tar zxf memcache-2.2.5.tgz

2、添加环境变量

cd memcache-2.2.5
ls
vim ~/.bash_profile        ##修改环境变量
修改如下
 10 PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin

source ~/.bash_profile         ##加载

3、初始化configure文件

phpize
ls

4、查看编译环境,编译,安装

./configure
make && make install

5、修改php的配置文件,并重新加载

cd /usr/local/lnmp/php/etc/
ls
vim php.ini
修改如下

 873 extension=memcache.so

/etc/init.d/php-fpm reload
  

6、查看php支持的软件

php -m    ##查看php支持的软件
php -m | grep memcache
php -m | grep mysql

7、安装memcached 的服务软件,并开启服务

yum install -y memcached

/etc/init.d/memcached start
netstat -antlp        ##查看端口号

tcp        0      0 0.0.0.0:11211               0.0.0.0:*                   LISTEN      21553/memcached  


注意:配置文件中如何设置只允许某个IP使用
vim /etc/sysconfig/memcached


  5 OPTIONS=" -l 172.25.38.1 "

tcp        0      0 172.25.38.1:11211           0.0.0.0:*                   LISTEN      21571/memcached  


8、编辑

telnet localhost 11211

stats
set name 0 0 6
westos
westosal
getname
delete name
set name 0 5 6
getname

0 0 6:第一个数字 ,第二个数字是缓存时间:如果是0不清除缓存  ,第三个数字:允许缓存的字符数

9、复制测试文件,并修改配置文件

cd memcache-2.2.5
cp memcache.php example.php /usr/local/lnmp/nginx/html/
cd /usr/local/lnmp/nginx/html/
ls
vim memcache.php
修改如下

 23 define('ADMIN_PASSWORD','westos');      // Admin Password


 28 $MEMCACHE_SERVERS[] = '172.25.38.1:11211'; // add more as an array
 29 #$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array


测试:

1、查看访问次数

访问172.25.38.1/memcache.php    ##查看memcache的hit和miss次数


访问172.25.38.1/example.php    ##缓存的php

ab -c 10 -n 5000 http://172.25.38.1/example.php    ##-c 10 10次并发, -n 5000 发送5000次。

ab -c 10 -n 5000 http://172.25.38.1/index.php

2、查看访问时间统计

[kiosk@foundation38 2.18]$ ab -c 10 -n 5000 http://172.25.38.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.38.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.38.1
Server Port:            80

Document Path:          /index.php
Document Length:        84785 bytes

Concurrency Level:      10
Time taken for tests:   41.893 seconds
Complete requests:      5000
Failed requests:        506
   (Connect: 0, Receive: 0, Length: 506, Exceptions: 0)
Write errors:           0
Total transferred:      424709438 bytes
HTML transferred:       423924438 bytes
Requests per second:    119.35 [#/sec] (mean)
Time per request:       83.786 [ms] (mean)
Time per request:       8.379 [ms] (mean, across all concurrent requests)
Transfer rate:          9900.30 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.4      0      18
Processing:    12   84   5.4     81     114
Waiting:        2   69   4.4     67      90
Total:         13   84   5.4     81     114

Percentage of the requests served within a certain time (ms)
  50%     81
  66%     87
  75%     90
  80%     90
  90%     91
  95%     91
  98%     91
  99%     92
 100%    114 (longest request)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[kiosk@foundation38 2.18]$ ab -c 10 -n 5000 http://172.25.38.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.38.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.38.1
Server Port:            80

Document Path:          /example.php
Document Length:        123 bytes

Concurrency Level:      10
Time taken for tests:   5.868 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Total transferred:      1400000 bytes
HTML transferred:       615000 bytes
Requests per second:    852.04 [#/sec] (mean)
Time per request:       11.737 [ms] (mean)
Time per request:       1.174 [ms] (mean, across all concurrent requests)
Transfer rate:          232.98 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     3   12   0.5     11      14
Waiting:        1   11   0.5     11      14
Total:          3   12   0.5     12      15
WARNING: The median and mean for the processing time are not within a normal deviation
        These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
  50%     12
  66%     12
  75%     12
  80%     12
  90%     12
  95%     13
  98%     13
  99%     14
 100%     15 (longest request)


三、nginx的memcache缓存

首先关闭原来的nginx服务,因为需要openresty的nginx支持

nginx -s stop
netstat -antlp

1、解压安装包

cd
tar zxf openresty-1.13.6.1.tar.gz

2、查看编译环境,编译,安装

cd openresty-1.13.6.1
ls
./configure
gmake && gmake install

3、复制测试文件

cd /usr/local/openresty/nginx/html/
cp /usr/local/lnmp/nginx/html/index.php .
cp /usr/local/lnmp/nginx/html/example.php .

4、修改OpenResty的nginx的配置文件

ls
cd ..
cd conf/
vim nginx.conf
修改如下


 17 http {
 18         upstream memcache {         方向代理
 19                 server localhost:11211;
 20                 keepalive 512;
 21
 22         }
 23     include       mime.types;
 24     default_type  application/octet-stream;


 55         location /memc{
 56                 internal;
 57                 memc_connect_timeout 100ms;
 58                 memc_send_timeout 100ms;
 59                 memc_read_timeout 100ms;
 60                 set $memc_key $query_string;
 61                 set $memc_exptime 300;
 62                 memc_pass memcache;
 63         }


 79         location ~ \.php$ {
 80             set $key $uri$args;
 81             srcache_fetch GET /memc $key;
 82             srcache_store PUT /memc $key;
 83             root           html;
 84             fastcgi_pass   127.0.0.1:9000;
 85             fastcgi_index  index.php;
 86             #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 87             include        fastcgi.conf;
 88         }


5、查看是否可以打开,打开nginx

cd ..
cd sbin/
ls
./nginx -t
./nginx
netstat -antlp

查看80端口

6、测试,在客户端机器,进行并发多条命令,发现速度稍微快了一点,大约三分之一。


[kiosk@foundation38 2.18]$ ab -c 10 -n 5000 http://172.25.38.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.38.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:        openresty/1.13.6.1
Server Hostname:        172.25.38.1
Server Port:            80

Document Path:          /index.php
Document Length:        84795 bytes

Concurrency Level:      10
Time taken for tests:   30.194 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Total transferred:      424934954 bytes
HTML transferred:       423975000 bytes
Requests per second:    165.60 [#/sec] (mean)
Time per request:       60.388 [ms] (mean)
Time per request:       6.039 [ms] (mean, across all concurrent requests)
Transfer rate:          13743.59 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2  49.3      0    3025
Processing:     8   59  20.0     57     330
Waiting:        1   50   9.4     51      78
Total:         11   60  52.9     57    3074

Percentage of the requests served within a certain time (ms)
  50%     57
  66%     63
  75%     64
  80%     64
  90%     64
  95%     65
  98%     81
  99%    164
 100%   3074 (longest request)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[kiosk@foundation38 2.18]$ ab -c 10 -n 5000 http://172.25.38.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.38.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:        openresty/1.13.6.1
Server Hostname:        172.25.38.1
Server Port:            80

Document Path:          /example.php
Document Length:        123 bytes

Concurrency Level:      10
Time taken for tests:   2.505 seconds
Complete requests:      5000
Failed requests:        0
Write errors:           0
Total transferred:      1564979 bytes
HTML transferred:       615000 bytes
Requests per second:    1995.95 [#/sec] (mean)
Time per request:       5.010 [ms] (mean)
Time per request:       0.501 [ms] (mean, across all concurrent requests)
Transfer rate:          610.08 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       2
Processing:     2    5   0.4      5       8
Waiting:        2    5   0.4      5       8
Total:          2    5   0.4      5       8

Percentage of the requests served within a certain time (ms)
  50%      5
  66%      5
  75%      5
  80%      5
  90%      5
  95%      6
  98%      6
  99%      6
 100%      8 (longest request)

猜你喜欢

转载自blog.csdn.net/qq_41627390/article/details/87801519