Memcached :是一个高性能的分布式内存对象缓存系统,用于动态 Web 应用以减轻 数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、 数据库驱动网站的速度。Memcached 基于一个存储键/值对的 hashmap。
Memcached :是一个自由、源码开放、高性能、分布式内存对象缓存系统,目的在 于通过减轻数据库负载来使动态 Web 应用程序提速。
Memcached :是一个在内存中对任意的数据(比如字符串,对象等)所使用的 key-value 存储。数据可以来自数据库调用,API 调用,或者页面渲染的结果。
Memcached :设计理念就是小而强大,它简单的设计促进了快速部署、易于开发,并 解决面对大规模的数据缓存的许多难题。所开放的 API 能用于大部分流行的程序语言
client -> nginx -> fastagi_pass -> php-fpm -> php.memcache -> memcahe:11211
给php添加了一个缓存,可以加速
1:解压memchche压缩包:
[root@server1 ~]# tar zxf memcache-2.2.5.tgz
2:配置环境变量,加载环境变量:
[root@server1 ~]# vim ~/.bash_profile
ATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
[root@server1 ~]# source ~/.bash_profile
3:初始化 编译 安装
[root@server1 ~]# cd memcache-2.2.5
[root@server1 memcache-2.2.5]# phpize #编译预环境,执行之后会有configure命令
[root@server1 memcache-2.2.5]# ./configure
[root@server1 memcache-2.2.5]# make
[root@server1 memcache-2.2.5]# make install
[root@server1 ~]# php -m ##可以查看php服务所支持的所有软件
4:修改php的配置文件,并重新加载
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/php/etc/
[root@server1 etc]# vim php.ini
修改如下
873 extension=memcache.so ##将此处的注释取消
5:重新加载php-fpm服务
[root@server1 etc]# /etc/init.d/php-fpm reload
6:查看php支持的软件(是否支持memcache)
[root@server1 etc]# php -m | grep memcache
memcache
7:安装memcached 的服务软件,并开启服务
[root@server1 ~]# yum install memcached -y
[root@server1 ~]# /etc/init.d/memcached start
8:查看端口号
[root@server1 ~]# netstat -antlp | grep memcache
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 3650/memcached
注意:配置文件中如何设置只允许某个IP使用(只是为了看一下,一般不使用,肯定设置成都能看到的)
vim /etc/sysconfig/memcached
5 OPTIONS=" -l 172.25.60.1 " ##或者写成127.0.0.1
tcp 0 0 172.25.60.1:11211 0.0.0.0:* LISTEN 21571/memcached
[root@server1 ~]# /etc/init.d/memcached restart
[root@server1 ~]# netstat -antlp | grep memcache
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 3688/memcached
9:telnet测试
[root@server1 ~]# yum install -y telnet
[root@server1 ~]# telnet localhost 11211
stats 可以查看所有的缓存
stats
set name 0 0 6 ##新建name缓存
westos
westosal ##超过6位无法建立成功
getname ##查看name缓存
delete name ##删除name缓存
set name 0 5 6
getname
0 0 6:第一个数字 ,第二个数字是缓存时间:如果是0不清除缓存 ,第三个数字:允许缓存的字符数
10:复制配置文件到nginx的默认发布目录下并修改配置文件
[root@server1 ~]# cd memcache-2.2.5
[root@server1 memcache-2.2.5]# cp memcache.php example.php /usr/local/lnmp/nginx/html/
[root@server1 html]# vim memcache.php
define('ADMIN_USERNAME','memcache'); // Admin Username (登录用的账号和密码)
define('ADMIN_PASSWORD','westos'); // Admin Password
$MEMCACHE_SERVERS[] = '172.25.60.1:11211'; // add more as an array ##改为开启memcache主机的ip
#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array ##注释掉
11:测试,
访问172.25.60.1/memcache.php ##查看memcache的hit和miss次数
测试 172.25.60.1/example.php
浏览器访问172.25.60.1/memcache.php
登录:
每刷新一次172.25.60.1/example.php,从缓存(hit)拿的比列会增加
测试:
结果:
12:用真机进行测试:
[root@foundation60 Desktop]#
[root@foundation60 Desktop]# ab -c 10 -n 5000 http://172.25.60.1/index.php
#exemple中有去访问memcache的配置,所以更快
[root@foundation60 Desktop]# ab -c 10 -n 5000 http://172.25.60.1/example.php
nginx模块:
如果memcache中有数据让nginx直接访问memcache,可以更快的加速
1:关闭之前的nginx服务,用openresty中带的nginx
[root@server1 ~]# nginx -s stop
2:解压压缩包:
[root@server1 ~]# tar zxf openresty-1.13.6.1.tar.gz
3:查看编译环境,编译,安装
[root@server1 ~]# cd openresty-1.13.6.1
[root@server1 openresty-1.13.6.1]# ./configure
[root@server1 openresty-1.13.6.1]# gmake
[root@server1 openresty-1.13.6.1]# gmake install
4:复制测试文件到openresty下nginx的默认发布目录下
[root@server1 ~]# cd /usr/local/openresty/nginx/html/
[root@server1 html]# cp /usr/local/lnmp/nginx/html/index.php .
[root@server1 html]# cp /usr/local/lnmp/nginx/html/example.php .
4、修改OpenResty的nginx的配置文件
[root@server1 openresty-1.13.6.1]# cd /usr/local/openresty/nginx/conf
[root@server1 conf]# vim nginx.conf
http {
upstream memcache {
server localhost:11211;
keepalive 512;
}
include mime.types;
default_type application/octet-stream;
location / {
root html;
index index.html index.htm;
}
location /memc {
internal;
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string;
set $memc_exptime 300;
memc_pass memcache;
}
location ~ \.php$ {
set $key $uri$args;
srcache_fetch GET /memc $key;
srcache_store PUT /memc $key;
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
5:开启nginx服务
[root@server1 conf]# cd ..
[root@server1 nginx]# cd sbin/ #sbin为nginx的启动目录
[root@server1 sbin]# ./nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@server1 sbin]# ./nginx
重新加载的命令
[root@server1 sbin]# ./nginx -s reload
[root@server1 sbin]# netstat -antlp ##查看80端口
6:此时在真机上继续进行检测,发现速度更会加快
[root@foundation60 openresty-bao]# ab -c 10 -n 5000 http://172.25.60.1/index.php
[root@foundation60 openresty-bao]# ab -c 10 -n 5000 http://172.25.60.1/example.php