ibaobei 发表于 2015-11-18 08:30:55

centos 为php安装memcache

  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(&quot;localhost&quot;,11211);
echo &quot;Version:&quot;.$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(&quot;hell world&quot;,1123);
$mem->set(&quot;key&quot;,$test,false,1000);
var_dump($mem->get(&quot;key&quot;));
$test_array = array(&quot;index_1&quot;=>&quot;hello&quot;,&quot;index_2&quot;=>&quot;world&quot;);
$mem->set(&quot;array_test&quot;,$test_array,false);
var_dump($mem->get(&quot;array_test&quot;));
?>

  结果如下:





版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: centos 为php安装memcache