MemCache介绍
动态数据缓存,是一个开源软件,性能好,分布式存储,缓解后端服务器的压力,提高用户的访问速度,缓存的数据存放在内存中,断电清空
把MemCache作为mysql缓存服务器。memcache里面存放的是键值对
memcache依赖libevent服务的API通讯通道,接受客户端请求,将请求作为一个事件进行封装。
MemCache是一个hash环,长度为2的32次方个,数据不是永久的
MemCache与php整合
实验环境
一台lnmp:192.168.1.5
两台
实验目的
通过php访问数据库中的数据后,再次访问会直接调取memcache的缓存来查看数据
实验步骤
第一步
在lnmp上安装memcache
vim /usr/local/php5/etc/php-fpm.conf #php-fpm监听端口必须是本地ip:9000
vim /usr/local/nginx/conf/nginx.conf # 同样修改php监听端口ip
安装libevent
tar zxf libevent-2.0.22-stable.tar.gz -C /usr/src
cd /usr/src/libevent-2.0.22-stable/
./configure && make && make install
安装memcached
tar zxf memcached-1.5.9.tar.gz -C /usr/src
cd /usr/src/memcached-1.5.9/
./configure --prefix=/usr/local/memecached --with-libevent=/usr/local && make && make install
ln -s /usr/local/memecached/bin/* /usr/local/bin
启动memcached
memcached -d -l 192.168.1.5 -p 11211 -c 10240 -m 512 -P /usr/local/memecached/memcache.pid -u root
-d:后台运行
-l:指定主机
-p:监听端口1211
-c:memcached的最大连接数
-m:当前memcache的内存缓存大小,单位M
-P:pid文件路径
-u:指定运行用户
数据库授权
[root@localhost ~]# mysql -u root -p123.com # 登录数据库
mysql> create database abc; # 创建数据库
mysql> use abc; # 进入数据库
mysql> create table test(id int,name varchar(10)); # 创建表test
# 插入数据
mysql> insert into test values(1,'cyj'),(2,'pjf'),(3,'mp'),(4,'fy'),(5,'mf');
# 对数据库abc授权
mysql> grant all on abc to 'root'@'192.168.1.5' identified by '123.com';
安装模块支持memcache
tar zxf memcache-2.2.7.tgz -C /usr/src
cd /usr/src/memcache-2.2.7/
phpize # 制作configure文件,这个模块包本身没有configure
./configure --enable-memcache --with-php-config=/usr/local/php5/bin/php-config && make && make install
cd /usr/local/php5/lib/php/extensions/no-debug-non-zts-20121212/
ls
memcache.so opcache.a opcache.so # 生成的模块
在php中添加memcache模块
vim /usr/local/php5/php.ini
extension = memcache.so
验证支持模块
编写php文件
vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
访问192.168.1.5/index.php
修改php页面使memcache与数据库连接
<?php
$memcachehost="192.168.1.5"; #指定memcache的节点ip
$memcacheport=11211; #指定memcache的端口
$memcache=new Memcache; #定义一个memcache的对选哪个
$memcache->connect($memcachehost,$memcacheport) or die ("could not connect"); #连接memcache
$query="select * from abc.test limit 5"; #执行客户端的指定语句,注意数据库和表
$key=md5($query); #对客户端要查找的数据进行hash计算
if(!$memcache->get($key)){ #判断 如果客户端的请求未命中
$name="mysql"; #第一行的数据内容
$conn=mysqli_connect("192.168.1.5","root","123.com"); #连接数据库
$result=mysqli_query($query);
while ($row=mysql_fetch_assoc($result)){ #去后端循环抓取
$arr[]=$row;
}
$memcache->set($key,serialize($arr),0,10); #将值缓存到memcache中
$data=$arr; #要返回给客户端的数据
}
else{ #如果客户端命中
$name="memcache"; #输出的第一行内容
$data_mem=$memcache->get($key); #通过hash计算的键获取到客户端的请求的值
$data=unserialize($data_mem); #要返回给客户端的数据
}
echo $name; # 以下为在客户端浏览器上显示出的页面内容
echo "<br>";
foreach($data as $a){
echo "id is $a[id]";
echo "<br>";
echo "name is $a[name]";
echo "<br>";
}
?>
访问192.168.1.5/index.php
是否获取到数据库的数据
访问成功后,刷新显示如下即可
扩展
使用telnet可进入到memcache的管理界面
yum -y install telnet
telnet 192.168.1.5 11211
set name 0 30 4 # 设置name键,0为键的标签,30为缓存时间,值得长度
haha # name所对应的值
STORED
get name # 获取name的键值
haha
如果使用set添加键值对时,如果值已经存在,则会将之前的数据覆盖
add也是添加键值对的命令,与set相反,如果值已经存在,则添加失败
replace,对已经存在的键值进行替换,如果键不存在则修改失败
append 向已存在的键值对中的值进行追加
delete 删除指定键值对
flush_all 清空所有键值对
quit 退出