memcache-2.2.6.tgz ------------------memcache的php扩展
memcached-1.4.13.tar.gz ----------------------memcache服务端软件
安装memcached
安装该软件时需要libevent的支持,
tar -zxvf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable/
./configure
make
make install
安装 memcached
tar -zxvf memcached-1.4.11.tar.gz
cd memcached-1.4.11/
./configure --prefix=/usr/local/memcached--with-libevent=/usr
make
make install
启动memcached:
memcached -d -m 10 -u root -l 0.0.0.0 -p 12000 -c 256-P /tmp/memcached.pid
验证memcached:
ps -ef|grep mem
netstat -tnulp|grep mem
关闭memcached
cat /tmp/memcached.pid
949
kill -9 949
安装php的memcache扩展
tar -zxvf memcache-2.2.6.tgz
cd memcache-2.2.6/
/usr/local/php/bin/phpize
./configure --enable-memcache--with-php-config=/usr/local/php/bin/php-config
make
make install
接下来修改php配置文件php.ini
vi php.ini
extension=memcache.so
测试memcache的php扩展是否安装成功
memcached -d -m 10 -u root -l 0.0.0.0 -p 12000 -c 256-P /tmp/memcached.pid
/usr/local/apache/bin/apachectl start
cd /usr/local/apache/htdocs
vi mem_test.php
<?php
$mem=newMemcache;
$mem->connect("10.10.10.16",12000);
$mem->set('hello','world',0,60);
$val=$mem->get('hello');
echo $val;
?>
如果可以看到world,那你的memcachephp扩展就成功了
memcache与php的配合使用
php与memcache结合测试代码
cd /usr/local/apache/htdocs
vim php-mem.php
<?php
//connect
$mem = newMemcache;
$mem->connect('10.10.10.16',12000);
//save data
$mem->set('key1','this is first value',0,60);
$val =$mem->get('key1');
echo "Getkey1 value:".$val."<br/>";
//replace data
$mem->replace('key1','this is replace value',0,60);
$val =$mem->get('key1');
echo "Getkey1 value:".$val."<br/>";
//save datagroup
$arr =array('aa','bb','cc');
$val = $mem->get('key1');
echo "Getkey1 value:".$val."<br/>";
//replace data
$mem->replace('key1','this is replace value',0,60);
$val =$mem->get('key1');
echo "Getkey1 value:".$val."<br/>";
//save datagroup
$arr =array('aa','bb','cc');
$mem->set('key2',$arr,0,60);
$val2 =$mem->get('key2');
echo "Getkey2 value:";
print_r($val2);
echo"<br/>";
//delete data
$mem->delete('key1');
$val=$mem->get('key1');
echo "getkey1 value:".$val."<br/>";
//closeconnetions
$mem->close();
?>
验证:
现在就成功了。
memcached查看所有key方法: