一、MemCache简介

MemCache是一个高性能的分布式的内存对象缓存系统,用于各种动态应用以减轻数据库负担。它通过在内存中缓存数据和对象,来减少读取数据库的次数, 从而提高动态、数据库驱动应用速度。MemCache会在内存中开辟一块空间,建立一个统一的巨大的hash表,hash表能够用来存储各种格式的数据, 包括图像、视频、文件以及数据库检索的结果等。 

二、环境配置

搭建好lnmp架构:

mysql安装与配置

php安装与配置

nginx安装与配置

三、实现访问加速

1.下载编译memcache

[[email protected] lnmp]# tar zxf memcache-2.2.5.tgz ##解压
[[email protected] lnmp]# cd memcache-2.2.5
[[email protected] memcache-2.2.5]# vim ~/.bash_profile #增加php的环境变量

13 PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin

[[email protected] memcache-2.2.5]# source ~/.bash_profile  #重新加载系统环境变量
[[email protected] memcache-2.2.5]# ls    ##进入解压后的目录后,没有configure的选项

[[email protected] memcache-2.2.5]# phpize #通过phpize命令生成configure
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
[[email protected] memcache-2.2.5]# ls

LNMP——MemCache实现访问加速

LNMP——MemCache实现访问加速

LNMP——MemCache实现访问加速

LNMP——MemCache实现访问加速

编译安装:

./configure     ##编译
make && make install

2.配置缓存

[[email protected] memcache-2.2.5]# cd /usr/local/lnmp/php/etc/
[[email protected] etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php.ini
[[email protected] etc]# vim php.ini

873 extension=memcache.so   ##将memcache模块添加到php中

[[email protected] etc]# /etc/init.d/php-fpm reload  ##重新加载php服务
Reload service php-fpm  done
[[email protected] etc]# php -m | grep memcache ##查看php下加载的模块是否有memcache
memcache

LNMP——MemCache实现访问加速

LNMP——MemCache实现访问加速

yum install -y memcached      ##安装memcached
vim /etc/sysconfig/memcached     ##查看memcached的启动配置,监控端口为所有主机的11211端口
/etc/init.d/memcached start      ##启动memcached服务

LNMP——MemCache实现访问加速

LNMP——MemCache实现访问加速

3.测试端口:

yum install telnet -y
[[email protected] etc]# telnet localhost 11211  #远程登陆本地的11211端口,用于测试缓存功能
Trying ::1...
Connected to localhost.
Escape character is '^]'.
set name 0 0 6  #最大字节数为6
westos
STORED
aaaaaaa
ERROR
get name
VALUE name 0 6
westos
END
stats  ##可查看版本信息
STAT pid 3890
STAT uptime 221
STAT time 1556629860
STAT version 1.4.4
STAT pointer_size 64
STAT rusage_user 0.000000
STAT rusage_system 0.005999
STAT curr_connections 10
STAT total_connections 11
......

LNMP——MemCache实现访问加速

4.修改默认发布测试页

[[email protected] etc]# cd /mnt/lnmp/memcache-2.2.5
[[email protected] memcache-2.2.5]# ls
[[email protected] memcache-2.2.5]# cp memcache.php example.php /usr/local/lnmp/nginx/html/
[[email protected] memcache-2.2.5]# cd /usr/local/lnmp/nginx/html
[[email protected] html]# vim memcache.php ##修改统计访问的页面文件
23 define('ADMIN_PASSWORD','westos');     ##将登陆密码修改为westos
28 $MEMCACHE_SERVERS[] = '172.25.60.1:11211'; ##添加测试的后台服务器的ip
29 #$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211';  ##因为只配置了一台服务器,所以多出的注释

LNMP——MemCache实现访问加速

5.测试:

打开firefox:http://172.25.60.1/memcache.php

LNMP——MemCache实现访问加速

LNMP——MemCache实现访问加速

LNMP——MemCache实现访问加速

命中率为50%

可以统计访问172.25.60.1/example.php页面的具体结果,是去访问的缓存还是后台的服务器

第一次访问:172.25.60.1/example.php

LNMP——MemCache实现访问加速

第十次访问:172.25.60.1/example.php

LNMP——MemCache实现访问加速

6.在真机上:

模拟5台主机并发访问2000次主页的情况

ab -c 5 -n 2000 http://172.25.60.1/index.php

LNMP——MemCache实现访问加速

175次失败,时间14.417秒

ab -c 5 -n 1000 http://172.25.60.1/example.php

LNMP——MemCache实现访问加速

0次失败,时间1.110秒

  • 可以查看到访问缓存的时间效率远远大于直接访问后台服务器

相关文章:

  • 2021-04-30
  • 2021-06-07
  • 2021-05-22
  • 2022-01-14
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
  • 2021-07-15
猜你喜欢
  • 2021-05-10
  • 2021-07-07
  • 2021-04-02
  • 2021-08-01
  • 2022-12-23
  • 2021-12-08
  • 2021-05-20
相关资源
相似解决方案