Memcache是一个与php兼容的内存高速缓存插件,不仅可以缓存变量等对象,而且可以与MySQL配合,缓存数据查询。由于Memcache在内存中缓存数据,因此它的读取写入速度非常之快,能为大容量快速变化的动态数据提供高速缓存。
#### 第一步:安装memcache服务器
yum -y install memcached
#### 第二步:安装php memcache扩展
yum -y install php-pecl-memcache
#### 第三步:检测php是否正常加载了memcahce模块
php -m | grep memcache 或者用phpinfo()函数查看
#### 第四步:启动memcache
pkill memcached
memcached -d -m 256 -u nobody -l localhost -p 11211
#### 第五步:写一段php代码,测试memcache是否能够工作,如果输入的是Memcache test ok!则表示成功!
注:为了防火墙影响,把iptables和selinux都先关闭掉,关闭命令如下
```shell
/etc/init.d/iptables stop
setenforce 0
```
```php
connect('127.0.0.1', 11211);
if($re){
//set(键,值,是否采用zlib压缩,过期时间0为永不过期)
$memcache->set('bestphper', 'Memcache test ok!',0, 60);
$result = $memcache->get('bestphper');
unset($memcache);
echo $result;
}else{
echo 'Memcache test fail';
}
?>
```
[注memcache客户端命令]
1.连接 telnet ip址址 端口号(11211) 注:若没有telnet工具,用yum -y install telnet安装一下子喽
2.添加 add 变量名 标志位(没什么用随便写) 时间(秒) 长度(定义要存数据的长度,多一个不行,少一个不行)
3.修改 set|replace
4.获取 get
5.删除 delete 变量名