目录
一. 案例设计
需要安装的软件
memcache服务端:libevent-2.1.8 memcached-1.5.6
memcahe客户端:LAMP架构 memcache-2.2.7
二. memcache服务端设置
2.1 编译安装libevent-2.1.8-stable
#安装编译器
[root@localhost libevent-2.1.8-stable]# yum -y install gcc gcc-c++ make //make其实是默认安装的有
#解压事件
[root@localhost ~]# cd opt
[root@localhost opt]# tar zxvf libevent-2.1.8-stable.tar.gz
[root@localhost opt]# cd libevent-2.1.8-stable/
#./configure配置
[root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[root@localhost libevent-2.1.8-stable]# make && make install
2.2 编译安装memcached-1.5.6
#解压memcached-1.5.6
[root@localhost libevent-2.1.8-stable]# cd ../memcached-1.5.6/
#./configure配置
[root@localhost memcached-1.5.6]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
[root@localhost memcached-1.5.6]# make && make install
[root@localhost memcached-1.5.6]# ln -s /usr/local/memcached/bin/* /usr/local/bin
[root@localhost memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root //-d指定守护进程,-m指定缓存大小,-p指定端口,-u指定用户管理
[root@localhost memcached-1.5.6]# netstat -anpt | grep memcached
2.3 memcache服务端数据操作
#安装telnet
[root@localhost memcached-1.5.6]# telnet 127.0.0.1 11211
bash: telnet: 未找到命令... //telnet命令默认没有安装
yum -y install telnet -y
telnet 127.0.0.1 11211
add username 0 0 7 //0:不进行压缩和序列化标识:0:数据过期时间为永不过期 7:键的长度为7
example //输入数据
get username //获取数据
VALLUE username 0 7
example
gets username
VALUE username 0 7 1 //最后一位是更新因子会自增1
example
set username 0 0 10 //更新信息,若键名不存在,则自行添加
everything
replace username 0 0 8 //更新信息,若键名不存在,则报错
12345678
gets username
VALUE username 0 8 4
12345678
cas username 0 0 7 4 //检查更新,更新因子相等则更新,否则返回EXIEX
lodging
STORED
append username 0 0 7 //键值后追加数据
example
STORED
delete username //删除键
flush_all //清除所有缓存数据
OK
stats //显示状态信息
三. 客户端设置
3.1 客户端需要安装LAMP架构
LAMP架构的搭建参考https://blog.csdn.net/weixin_48192495/article/details/108848710
3.2 安装memcache-2.2.7
#yum -y install autoconf
tar zxvf memcache-2.2.7.tgz -C /opt/
[root@localhost memcache-2.2.7]# /usr/local/php5/bin/phpize //增加为php的模块后再对memcache进行配置
./configure \
--enable-memcache \
--with-php-config=/usr/local/php5/bin/php-config
make && make install
/usr/local/php5/lib/php/extensions/no-debug-zts-20131226 //检查是否有这一个执行文件
vi /usr/local/php5/php.ini //搜索并修改下面一行,再新增一行
extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/"
extension = memcache.so
四. 测试
以下是在客户端去检测服务端是否可以连接
vim /usr/local/httpd/htdocs/index.php
<?php
$memcache = new Memcache();
$memcache->connect('12.0.0.22',11211);
$memcache->set('key','Memcache test Successfull!',0,60);
$result = $memcache->get('key');
unset($memcache);
echo $result;
?>
service httpd restart
http://12.0.0..24/index.php //输入客户端地址测试是否成功