php添加memcache功能模块
memcache简介:
memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问速度提升效果十分显著 [1] 。这是一套开放源代码软件,以BSD license授权发布。
memcache解析:
MemCache的工作流程如下:先检查客户端的请求数据是否在memcached中,如有,直接把请求数据返回,不再对数据库进行任何操作;如果请求的数据不在memcached中,就去查数据库,把从数据库中获取的数据返回给客户端,同时把数据缓存一份到memcached中(memcached客户端不负责,需要程序明确实现);每次更新数据库的同时更新memcached中的数据,保证一致性;当分配给memcached内存空间用完之后,会使用LRU(Least Recently Used,最近最少使用)策略加上到期失效策略,失效数据首先被替换,然后再替换掉最近未使用的数据。 [2]
Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从内存中读取,从而大大提高读取速度。
Memcache是danga的一个项目,最早是LiveJournal 服务的,最初为了加速 LiveJournal 访问速度而开发的,后来被很多大型的网站采用。
1.解压
[root@php ~]# tar zxf memcache-4.0.5.2.tgz [root@php ~]# cd memcache-4.0.5.2/ [root@php memcache-4.0.5.2]# ls autom4te.cache config9.m4 config.log config.status config.w32 Dockerfile libtool Makefile.fragments memcache.php README build config.h config.m4 CREDITS example.php LICENSE Makefile.objects modules run-tests.php cloudbuild.yaml config.h.in config.nice configure.ac docker include Makefile memcache.la php7 tests
2. memcache的编译安装和nginx和php的方法有所不同,解压后的源码目录是没有configure这个编译安装脚本的,需要执行phpize这个命令脚本才能调出,但因为路径问题phpize这个命令还不能使用,所以要先把phpize这个命令加入到系统环境变量里面(也可做个软链接)才能使用
[root@php ~]# vim .bash_profile 10 PATH=$PATH:$HOME/bin:/usr/local/php/bin [root@php ~]# source .bash_profile [root@php ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/usr/local/php/bin
3. 再进到memcache源码目录中,执行phpize的时候发现需要autoconf这个依赖性,所以要先安装autoconf,再执行phpize就ok了
[root@php memcache-4.0.5.2]# yum install autoconf -y [root@php memcache-4.0.5.2]# phpize Configuring for: PHP Api Version: 20190902 Zend Module Api No: 20190902 Zend Extension Api No: 320190902
2.1 再查看源码目录configure这个脚本就出现了
[root@php ~]# cd memcache-4.0.5.2/ [root@php memcache-4.0.5.2]# ls autom4te.cache config.h config.m4 configure.ac Dockerfile LICENSE memcache.la README build config.h.in config.nice config.w32 example.php Makefile memcache.php run-tests.php cloudbuild.yaml config.h.in~ config.status CREDITS include Makefile.fragments modules tests config9.m4 config.log configure docker libtool Makefile.objects php7
3. 编译安装
[root@php memcache-4.0.5.2]# ./configure [root@php memcache-4.0.5.2]# make && make install
...... Build complete. Don't forget to run 'make test'. Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
4. 进入到memcache的模块安装目录,查看安装的模块已经显示了,但使用php -m可以查看php安装的所有模块,却发现没有memcache这个模块
[root@php memcache-4.0.5.2]# cd /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/ [root@php no-debug-non-zts-20190902]# ls memcache.so opcache.a opcache.so [root@php no-debug-non-zts-20190902]# php -m
[PHP Modules] bcmath Core ctype curl date dom exif fileinfo filter ftp gd hash iconv json libxml mbstring mysqli mysqlnd openssl pcre PDO pdo_mysql pdo_sqlite Phar posix Reflection session SimpleXML soap sockets SPL sqlite3 standard tokenizer xml xmlreader xmlwriter zlib [Zend Modules]
5. 这个呀是因为memcache虽然安装了,但php它还没有识别到,需要修改php服务的配置文件加载memcache的动态扩展,然后重载,它才能识别
[root@php no-debug-non-zts-20190902]# cd /usr/local/php/etc [root@php etc]# ls php-fpm.conf php-fpm.conf.default php-fpm.d php.ini [root@php etc]# vim php.ini 909 ; - Many DLL files are located in the extensions/ (PHP 4) or ext/ (PHP 5+) 910 ; extension folders as well as the separate PECL DLL download (PHP 5+). 911 ; Be sure to appropriately set the extension_dir directive. 912 ; 913 ;extension=bz2 914 extension=memcache ##从下一行复制,然后去掉前面的;注释将后面的模块改成memcache,加载memcache的动态扩展,其他不变 915 ;extension=curl 916 ;extension=ffi 917 ;extension=ftp [root@php etc]# systemctl reload php-fpm [root@php etc]# php -m | grep memcache memcache
6. 下载memcached,进行前端访问,端口是11211
[root@php etc]# yum install memcached Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. Resolving Dependencies --> Running transaction check ---> Package memcached.x86_64 0:1.4.15-10.el7_3.1 will be installed --> Processing Dependency: libevent-2.0.so.5()(64bit) for package: memcached-1.4.15-10.el7_3.1.x86_64 --> Running transaction check ---> Package libevent.x86_64 0:2.0.21-4.el7 will be installed --> Finished Dependency Resolution ...... [root@php etc]# netstat -antlp Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 425/php-fpm: master tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 6956/memcached tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3226/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3325/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 3421/master tcp 0 0 172.25.32.2:22 172.25.32.250:41046 ESTABLISHED 3586/sshd: root@pts tcp 0 0 172.25.32.2:80 172.25.32.250:48402 ESTABLISHED 3239/nginx: worker tcp 0 0 127.0.0.1:9000 127.0.0.1:53474 TIME_WAIT - tcp6 0 0 :::11211 :::* LISTEN 6956/memcached tcp6 0 0 :::22 :::* LISTEN 3325/sshd tcp6 0 0 ::1:25 :::* LISTEN 3421/master
7. 将memcache源码目录中的默认发布目录拷贝到nginx的html中,然后进行修改memcache.php
[root@php ~]# cd memcache-4.0.5.2/ [root@php memcache-4.0.5.2]# cp memcache.php example.php /usr/local/nginx/html/ [root@php memcache-4.0.5.2]# cd /usr/local/nginx/html/ [root@php html]# ls 50x.html example.php index.html index.php memcache.php [root@php html]# vim memcache.php 22 define('ADMIN_USERNAME','memcache'); // Admin Username 23 define('ADMIN_PASSWORD','westos'); // Admin Password ##在浏览器登陆memcache的时候是需要用户名和密码认证的,memcache用户已经自动创建了,密码要自己设置,我这里是westos 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.32.2:11211'; // add more as an array ##修改成本机的ip
8. 然后从浏览器进行访问,ip/memcache.php ,用户名是memcache密码就是上面文件中的westos
9. 在新的主机中可以用ab 命令做压测,进行访问速度的对比
通过php来处理访问,进行测试
[root@nginx ~]# ab -c10 -n 1000 http://172.25.32.2/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.32.2 (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: nginx/1.22.0 Server Hostname: 172.25.32.2 Server Port: 80 Document Path: /index.php Document Length: 71579 bytes Concurrency Level: 10 Time taken for tests: 0.185 seconds Complete requests: 1000 Failed requests: 77 (Connect: 0, Receive: 0, Length: 77, Exceptions: 0) Write errors: 0 Total transferred: 71741834 bytes HTML transferred: 71578834 bytes Requests per second: 5410.41 [#/sec] (mean) Time per request: 1.848 [ms] (mean) Time per request: 0.185 [ms] (mean, across all concurrent requests) Transfer rate: 379055.15 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.0 0 0 Processing: 1 2 0.3 2 3 Waiting: 1 2 0.3 2 3 Total: 1 2 0.3 2 3 Percentage of the requests served within a certain time (ms) 50% 2 66% 2 75% 2 80% 2 90% 2 95% 2 98% 3 99% 3 100% 3 (longest request) 测试后会发现访问速度和访问的出错在1000个中报错了77个,后端处理不过来,随着请求的增多报错也会越来越多 通过memcache模块缓存加速处理访问,进行测试 [root@nginx ~]# ab -c10 -n 1000 http://172.25.32.2/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.32.2 (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: nginx/1.22.0 Server Hostname: 172.25.32.2 Server Port: 80 Document Path: /example.php Document Length: 228 bytes Concurrency Level: 10 Time taken for tests: 0.071 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 391000 bytes HTML transferred: 228000 bytes Requests per second: 14170.73 [#/sec] (mean) Time per request: 0.706 [ms] (mean) Time per request: 0.071 [ms] (mean, across all concurrent requests) Transfer rate: 5410.89 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.0 0 0 Processing: 0 1 0.3 1 2 Waiting: 0 1 0.3 1 2 Total: 0 1 0.3 1 2 Percentage of the requests served within a certain time (ms) 50% 1 66% 1 75% 1 80% 1 90% 1 95% 1 98% 1 99% 1 100% 2 (longest request)