企业实战-- php + memcache

一、Memcache简介

Memcached是一个自由开源的,高性能,分布式内存对象缓存系统。**用于动态Web应用以减轻数据库的负载。**它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高了网站访问的速度。

MemCaChe是一个存储键值对的HashMap,在内存中对任意的数据(比如字符串、对象等)所使用的key-value存储,数据可以来自数据库调用、API调用,或者页面渲染的结果。

memcache和memcached区别:

memcache是项目的名称
memcache是服务器端可以执行的文件的名称

二、php增加memcache模块

1.将php的bin目录路径增加到~/.bash_profile中,为了方便调用

[root@server1 ~]# vim ~/.bash_profile

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

2.解压

memcache目录本来没有configure文件,所以没法编译

需要先执行命令:phpize,phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块,执行该命令需要先安装autoconf软件:

[

root@server1 memcache-4.0.5.2]# source /root/.bash_profile 
[root@server1 memcache-4.0.5.2]# yum install autoconf -y
[root@server1 memcache-4.0.5.2]# phpize
Configuring for:
PHP Api Version:         20190902
Zend Module Api No:      20190902
Zend Extension Api No:   320190902

3.编译安装memcache

[root@server1 memcache-4.0.5.2]# ./configure --with-php-config=/usr/local/lnmp/php/bin/php-config
[root@server1 memcache-4.0.5.2]# make && make install

    

4.修改php主配置文件,增加模块

[root@server1 ~]# cd /usr/local/lnmp/php/etc/
[root@server1 etc]# vim php.ini 
修改:
 761 extension_dir = "/usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20     190902/"
 912 extension=memcache.so

   

5.重新加载或启动php

[root@server1 memcache-4.0.5.2]# /etc/init.d/php-fpm reload			#没有启动时,使用start命令启动
[root@server1 memcache-4.0.5.2]# php -m | grep memcache
memcache

6.安装memcached

[root@server1 memcache-4.0.5.2]# yum install -y memcached

    

7.查看memcached的配置文件

[root@server1 memcache-4.0.5.2]# cd /etc/sysconfig/
[root@server1 sysconfig]# cat memcached 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""					#如果写为OPTIONS=“-l 127.0.0.1”,即为只监听本机的11211端口

8.启动memcached

[root@server1 sysconfig]# systemctl start memcached 

11.编辑测试页面

[root@server1 sysconfig]# cd /root/lnmp/memcache-4.0.5.2
[root@server1 memcache-4.0.5.2]# cp memcache.php example.php /usr/local/lnmp/nginx/html/
[root@server1 memcache-4.0.5.2]# cd /usr/local/lnmp/nginx/html/
[root@server1 html]# vim memcache.php 

 22 define('ADMIN_USERNAME','memcache');    // Admin Username
 23 define('ADMIN_PASSWORD','westos');      // Admin Password

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

浏览器输入http://172.25.63.1/memcache.php测试:

12.客户端测试

测试访问没有memcache的php页面时,发现速度很慢而且很多失败的:

[root@foundation63 kiosk]# ab -c 10 -n 5000 http://172.25.63.1/index.php

试访问有memcache的php页面时,发现速度很快而且没有失败的:

[root@foundation63 kiosk]# ab -c 10 -n 5000 http://172.25.63.1/memcache.php
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值