Memcached 是一个高性能的、分布式的内在对象缓存系统。
笔者需要在centos下面来缓存一些内存对象,所以考虑安装Memcached。
一、 安装 Memcached
Centos 安装memcached很简单, 一条指令就可以了。
yum install memcached
安装好了之后,可以用下面的指令来测试一下:
memcached -m 512 -u nobody -vv
说明安装成功了。
设定系统的自启动
chkconfig --add memcached
可以通过修改/etc/init.d/memcached来设置启动的参数
查看启动
二、配置PHP
1. 下载php memcache client,并安装
wget http://pecl.php.net/get/memcache-2.1.2.tgz
tar -xvf memcache-2.1.2.tgz
cd memcache-2.1.2
phpize && ./configure --enable-memcache && make
2. 拷贝memcache.so到php的扩展module中
3. 修改php.ini文件
添加
extension=memcache.so
4. 查看php memcache配置
5. 测试代码
<?php
$mem = new Memcache;
$mem->addServer("localhost",11211);
echo "Version:".$mem->getVersion();
class testClass{
private $str_attr;
private $int_attr;
public function __construct($str,$int)
{
$this->str_attr = $str;
$this->int_attr = $int;
}
public function set_str($str)
{
$this->str_attr = $str;
}
public function set_int($int)
{
$this->int_attr = $int;
}
public function get_str()
{
return $this->str_attr;
}
public function get_int()
{
return $this->int_attr;
}
}
$test = new testClass("hell world",1123);
$mem->set("key",$test,false,1000);
var_dump($mem->get("key"));
$test_array = array("index_1"=>"hello","index_2"=>"world");
$mem->set("array_test",$test_array,false);
var_dump($mem->get("array_test"));
?>
结果如下: