安装memcache

yum install memcached  

 

安装libmemcached库

yum install libmemcached

PHP安装Memcache扩展

root@blog.phpha.com] wget -c http://pecl.php.net/get/memcache-3.0.8.tgz
[root@blog.phpha.com] tar -zxvf memcache-3.0.8.tgz
[root@blog.phpha.com] cd memcache-3.0.8
[root@blog.phpha.com] /usr/local/php/bin/phpize
[root@blog.phpha.com]  ./configure  -with-php-config=/usr/bin/php-config -with-zlib-dir
[root@blog.phpha.com] make && make install
#Installing shared extensions:     /usr/lib64/php/modules/
#/usr/lib64/php/modules/memcache.so
[root@blog.phpha.com] vim /etc/php.ini
#extension = /usr/lib64/php/modules/memcache.so
[root@blog.phpha.com] service php-fpm reload

 启动

memcached -d -u root -p 11211

 测试:

<?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"));

?>
View Code

相关文章: