memcache

本文介绍Memcached在Web应用中的作用,包括配置、实现PHP加速及监控页面搭建。并通过OpenResty结合Memcached优化数据传输速率,显著提升高并发场景下的Web应用性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

memcache

memcached是LiveJournal旗下的Danga Interactive公司的Brad Fitzpatric为首开发的一款软件。现在已经成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。传统的Web应用都将数据保存到RDBMS中,应用服务器从RDBMS中读取数据、处理数据并在浏览器中显示。但是随着数据量增大、访问的集中、就会出现RDBMS的负担加重、数据库响应变慢、导致整个系统响应延迟增加。而memcached就是为了解决这个问题而出现的,memcached是一款高性能的分布式内存缓存服务器,一般目的是为了通过缓存数据库的查询命中减少数据库压力、提高应用响应速度、提高可扩展性。
图解:
在这里插入图片描述

一:memcache的配置

编译

[root@server1 mnt]# tar zxf memcache-2.2.5.tgz 
[root@server1 mnt]# vim ~/.bash_profile ##添加环境变量 
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
[root@server1 mnt]# source ~/.bash_profile   ##刷新环境变量
[root@server1 mnt]# cd memcache-2.2.5
[root@server1 memcache-2.2.5]# phpize 
[root@server1 memcache-2.2.5]# ./configure 

配置

[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/php/etc/
[root@server1 etc]# vim php.ini 
 extension=memcache.so       ## 给php扩展模块:memcache

在这里插入图片描述

[root@server1 etc]# /etc/init.d/php-fpm reload
[root@server1 etc]# php -m | grep memcache
memcache
[root@server1 etc]# yum install memcached -y    ##下载memcached
[root@server1 etc]# cat /etc/sysconfig/memcached 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
[root@server1 etc]# /etc/init.d/memcached start
Starting memcached:                                        [  OK  ]
[root@server1 php]# yum install telnet -y
[root@server1 php]# telnet localhost 11211    测试memache功能
set name 0 0 5  设置  0   缓存时间,字符限制
hello
STORED
get name
VALUE name 0 5
hello
END
set name 0 10 4
zhao
STORED
get name
VALUE name 0 4
zhao
END
get name
quit  ##退出
[root@server1 html]#  netstat -antlpe | grep 11211
tcp        0      0 0.0.0.0:11211               0.0.0.0:*                   LISTEN      498        95464      11255/memcached     
tcp        0      0 :::11211                    :::*                        LISTEN      498        95465      11255/memcached     

二.memorycache实现php加速:

[root@server1 etc]# cd /mnt/memcache-2.2.5
[root@server1 memcache-2.2.5]# cp memcache.php example.php /usr/local/lnmp/nginx/html/   ##添加到ngnix的发布目录下
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/nginx/html/
[root@server1 html]# vim memcache.php 
define('ADMIN_PASSWORD','westos');      // Admin Password
$MEMCACHE_SERVERS[] = '172.25.5.1:11211'; // add more as an array
#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array

测试

[kiosk@foundation5 Desktop]$ ab -c 10 -n 5000 http://172.25.5.1/index.php
##10个并发访问5000次
Time taken for tests:   39.496 seconds   ##用时
Complete requests:      5000
Failed requests:        578
[kiosk@foundation5 Desktop]$ ab -c 10 -n 5000 http://172.25.5.1/example.php
Time taken for tests:   13.624 seconds  ##用时
Complete requests:      5000

172.25.5.1/memcache.php #memcache监控页面.使用memcache.php文件中的Admin用户名和密码登陆
172.25.5.1/example.php #测试页面
多次刷新测试页面,然后刷新监控页面发现缓存命中率Hits上升,

在这里插入图片描述
在这里插入图片描述

三.openresty+memcache

nginx本身具有高并发的特点,如果将数据缓存放在php后面,则客户请求发给nginx,nginx给php-fpm处理,然后将获取的数据缓存到memcache上,则nginx只有在等待php-fpm处理结束后,必定会影响数据传输速率,如果将memcache直接与nginx连接,当客户发出请求时,nginx直接从memcahce中将数据出给客户端,即可提高速率,Nginx + srcache(nginx的cache) + memc(连接php的memcache)和 openresty+memcache都可以实现,这里我们使用后者来实现。
OpenResty(也称为 ngx_openresty)是一个全功能的 Web 应用服务器。它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项。通过揉和众多设计良好的 Nginx 模块,OpenResty 有效地把 Nginx 服务器转变为一个强大的 Web 应用服务器,基于它开发人员可以使用 Lua 编程语言对 Nginx 核心以及现有的各种 Nginx C 模块进行脚本编程,构建出可以处理一万以上并发请求的极端高性能的 Web 应用。
1.编译安装openresty

[root@server1 mnt]# tar zxf openresty-1.13.6.1.tar.gz 
[root@server1 mnt]# nginx -s stop   ##停止ngninx服务
[root@server1 mnt]# cd openresty-1.13.6.1
[root@server1 openresty-1.13.6.1]# ./configure 
[root@server1 openresty-1.13.6.1]# cd /usr/local/openresty/
[root@server1 openresty]# cd nginx/html/
[root@server1 html]# cp /usr/local/lnmp/nginx/html/index.php .
[root@server1 html]# cp /usr/local/lnmp/nginx/html/example.php . ##拷贝前面nginx的发布目录文件以给后面测试用

2.编辑openresty的nginx的配置文件
[root@server1 html]# cd …
[root@server1 nginx]# cd conf/
[root@server1 conf]# vim nginx.conf

# 设定负载均衡的服务器列表,可以指定多个服务器。
# keepalive指令是指http-upstream-keeplive-module提供的功能,这里我们最大保持512个立即不关闭的连接用于提高性能
upstream memcache {
server localhost:11211;
keepalive 512;
}

# memcache是基于http method语义的,使http的GET方法表示get,PUT方法表示set,DELETE方法表示delete
# internal表示只接受内部网络,不接受外部http请求,如果需要接受外部访问 ,可以使用allow和deny来指令控制权限
# $memc_key表示以什么key,这里直接使用nginx内置的$query_string来作为key,$memc_exptime表示缓存失效时间,单位为秒
实际应用中根据具体情况设置。
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;
}

# ~ \.php$这个location配置了缓存,这表示所有以.php结尾的请求结果都会被缓存
# srcache_fetch表示注册一个输入拦截处理器到location,这个配置进入时将被执行
# srcache_store表示注册一个输出拦截器到location,当location执行完成输出时会被执行。
# 在location  ~\.php$中添加
set $key $uri$args;
srcache_fetch GET /memc $key;
srcache_store PUT /memc $key;

完成以上配置后,相当于对nginx增加了这些逻辑:当所有请求以"'.php"结尾时
首先到memcahce中查询有没有以$url$args为key的数据,如果有则返回否则执行location的逻辑。

启动服务

[root@server1 nginx]# cd sbin/
[root@server1 sbin]# ./nginx 

测试:

[kiosk@foundation5 Desktop]$ ab -c 10 -n 5000 http://172.25.5.1/example.php
Time taken for tests:   2.816 seconds     ##用时较上一个实验大幅度缩短
Complete requests:      5000
Failed requests:        0
[kiosk@foundation5 Desktop]$ ab -c 10 -n 5000 http://172.25.5.1/index.php
Time taken for tests:   2.643 seconds
Complete requests:      5000
Failed requests:        0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值