PHP缓存应用:PHP MEMCACHE 详解

网络整理 - 08-29

Memcache函数库是在PECL(PHP Extension Community Library)中,主要作用是搭建大容量的内存数据的临时存放区域,在分布式的时候作用体现的非常明显,否则不建议使用。在ubuntu上安装完运行的时候报错:


/usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2:

cannot open shared object file: No such file or directory

按照:《libeven、memcached、libmemcache安装》中的方法,使用:

sudo ln -s /usr/local/lib/libevent-1.4.so.2 /usr/lib/libevent-1.4.so.2

可以修正这个BUG

通过新得立安装php的memcached模块,注销/etc/php5/conf.d/memcached.ini里面的“;”,重启apache,调用phpinfo()出现memcached的信息。


<?php   
$memcache = new Memcache;   
$memcache->connect('localhost', 11211) or die ("Could not connect");   
$version = $memcache->getVersion();   
echo "Server's version: ".$version."\n";   
?>


01.<?php
02.$memcache = new Memcache;
03.$memcache->connect('localhost', 11211) or die ("Could not connect");
04.$memcache->set( 'name', 'leo', 0, 30);
05.if(!$memcache->add( 'name', 'susan', 0, 30))
06.{
07. echo 'susan is exist';
08.};
09.$memcache->replace( 'name', 'lion', 0, 300);
10.echo $memcache->get( 'name');
11.$memcache->delete( 'name', 5);
12.?>


01.<?php
02.function _callback_memcache_failure($host, $port) {
03. print "memcache '$host:$port' failed";
04.}
05.$memcache = new Memcache;
06.$memcache->addServer('192.168.1.116', 11211);
07.$memcache->setServerParams('192.168.1.116', 11211, 1, 15, true,
08.
09.'_callback_memcache_failure');
10.echo $memcache->getServerStatus('192.168.1.116', 11211);
11.?>


01.<?php
02.$memcache = new Memcache;
03.$memcache->connect('localhost', 11211);
04.$memcache->set('test_item', 8);
05.$memcache->increment('test_item', 4);
06.echo $memcache->decrement('test_item', 7);
07.// 显示 5
08.?>


/usr/local/bin/memcached -d -m 10 -u root -l 127.0.0.1 -p 11211 -c 256 -P
 
/tmp/memcached.pid