(三)安装memcache的php扩展端:
1、安装memcache
# tar xf memcache-2.2.5.tgz
# cd memcache-2.2.5
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
# make && make install
安装完后会有类似以下的提示:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
在/etc/php.d/下创建php.ini文件,添加如下一行来载入memcache扩展:
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
重启一下php-fpm服务
#service php-fpm restart
在phpinfo中可以看到memcache已经启用了
2、对memcached功能进行测试
在网站目录/web/htdocs中建立测试页面test.php,添加如下内容:
<?php
$mem = new Memcache;
$mem->connect("127.0.0.1", 11211) or die("Could not connect");
$version = $mem->getVersion();
echo "Server's version: ".$version."<br/>\n";
$mem->set('testkey', 'Hello World', 0, 600) or die("Failed to save data at the memcached server");
echo "Store data in the cache (data will expire in 600 seconds)<br/>\n";
$get_result = $mem->get('testkey');
echo "$get_result is from memcached server.";
?>
访问test.php,如果有输出“Hello World is from memcached.”等信息,则表明memcache已经能够正常工作。