欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP缓存应用:PHP MEMCACHE 详解_PHP

程序员文章站 2022-06-01 20:46:14
...
memcacheEMC

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的信息。


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

  1. $memcache = new Memcache;
  2. $memcache->connect('localhost', 11211) or die ("Could not connect");
  3. print_r($memcache->getStats());
  4. /**
  5. * Array
  6. * (
  7. * [pid] => 8052
  8. * [uptime] => 9205
  9. * [time] => 1205898428
  10. * [version] => 1.2.5
  11. * [pointer_size] => 32
  12. * [rusage_user] => 0.008000
  13. * [rusage_system] => 0.000000
  14. * [curr_items] => 1
  15. * [total_items] => 17
  16. * [bytes] => 57
  17. * [curr_connections] => 2
  18. * [total_connections] => 15
  19. * [connection_structures] => 3
  20. * [cmd_get] => 9
  21. * [cmd_set] => 23
  22. * [get_hits] => 5
  23. * [get_misses] => 4
  24. * [evictions] => 0
  25. * [bytes_read] => 671
  26. * [bytes_written] => 850
  27. * [limit_maxbytes] => 10485760
  28. * [threads] => 1
  29. * )
  30. */
  31. ?>


01.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.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.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