±ÈÈç˵PECLÀiÓÐÁ½¸oMemcachedµÄÄ£¿e£¬MemcacheºÍMemcached£¬Ä¿Ç°´o²¿ÖPHP»¾³ÀiʹÓõÄÊÇÃu×ÖÀi²»´ødµÄMemcache°ae±¾£¬Õa¸o°ae±¾ÊͳoµÄ±È½ÏÔ磬ÊÇÒ»¸oÔÉu°ae±¾£¬ÓeÖ®¶ÔÓ¦µÄ´ødµÄMemcached°ae±¾ÔoÊǽ¨Á¢ÔÚlibmemcachedµÄ»u´¡ÉÏ£¬ËuÒÔ˵Memcached°ae±¾µÄ¹¦ÄܸuÈ«Ò»Ð(C)¡£
°²×°Memcached°ae±¾µÄPHPÄ£¿e
wget http://download.tangent.org/libmemcached-0.35.tar.gz
tar zxf libmemcached-0.35.tar.gz
cd libmemcached-0.35
./configure
make
make install
wget http://pecl.php.net/get/memcached-1.0.0.tgz
tar zxf memcached-1.0.0.tgz
cd memcached-1.0.0
phpize
./configure
make
make install
´o¿ªphp.ini¼ÓÉÏ£º
extension = "memcached.so"
ÕaÑu°²×°¾Í½aÊøÁË£¬Äa¿ÉÒÔͨ¹ýÏÂÁÐÃuÁiÀ´È*ÈÏ£º
php -m | grep mem
ÑÝʾMemcached°ae±¾µÄй¦ÄÜ
ÏÈÐe¹¹Ò»¸oÎÊÌa£¬¼ÙÉecounter³oʼֵÊÇÒ»¸oÕuÊý£¬²»Ê¹ÓÃincrement½¨£¬Í¨¹ýget/setÍe³Éÿ´Î¼ÓÒ»¡£
ÔÚMemcache°ae±¾Ài£¬ÎÒÃÇÖ»ÄÜ°´ÕÕ´oÖÂÈçϵÄ*½Ê½À´½øÐУº
$m = new Memcache();
$m->addServer('localhost', 11211);
$v = $m->get('counter');
$m->set('counter', $v + 1);
ÓÉÓÚget/setÕaÁ½¸o¶¯×÷ÎÞ*¨×÷Ϊһ¸oÔ×ÓÀ´²Ù×÷£¬ËuÒÔµ±¶a¸o½ø³Ìͬʱ´¦Àiʱ£¬»a³oÏÖ¶ªÊ§µÄ¿ÉÄÜ£¬¸uÈÃÈËÄÕ»ðµÄÊÇ£¬Äa¸u±¾¾Í²»ÖªµÀʲôʱºo³oÏÖ¶ªÊ§¡£
ÔÙ¿´¿´Memcached°ae±¾Ài£¬ÎÒÃÇÊÇÈçºÎ×oµÄ£º
$md = new Memcached();
$md->addServer('localhost', 11211);
$v = $md->get('counter', null, $token)
$md->cas($token, 'counter', $v + 1);
casÊÇMemcached°ae±¾ÀiÌa¹(C)µÄ¹¦ÄÜ£¬Ëµ°×Á˾ÍÊÇÒ»¸oÀÖ¹ÛËøµÄ¹¦ÄÜ£¬Èç¹uÄa°Ñ$tokenµÄÖµvar_dump³oÀ´£¬¾Í»a*¢ÏÖ$tokenÆaʵ¾ÍÊÇÒ»¸o°ae±¾ºÅ£¬Èç¹uͨ¹ýgetµÃµ½µÄ$token°ae±¾ºÅÔÚcasµÄʱºo²»¶ÔÓ¦£¬¾Í˵Ã÷ÒѾÓбðµÄ²Ù×÷¸uÐÂÁË£¬´Ëʱcas²Ù×÷»aʧ°Ü£¬ÖÁÓÚÈçºÎ¼ÌÐø²Ù×÷£¬¾Í¿´Äa×Ô¼ºÁË¡£
×¢£ºÈç¹uÄaÏeÊÖ¶¯ÖØÏÖһϳaÍ»µÄÇe¿o£¬¿ÉÔÚgetºÍcasÖ®¼asleepÈo¸ÉÃe£¬²¢¿½±´Á½*ݽű¾£¬ÏȺoÖ´ÐС£
˳±a˵һ¾a£¬ÍƼoµÄMemcached°ae±¾Ä£¿eµÄ¹þÏ£ÉeÖÃÈçÏ£º
$md->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
$md->setOption(Memcached::OPT_HASH, Memcached::HASH_CRC);
×ܽa
Memcached°ae±¾»¹ÓкܶaMemcacheûÓеŦÄÜ£¬±ÈÈçͨ¹ýgetByKey, setByKeyµÈ×Ô¶¯Ö§³Ö¶a¸o*þÎñÆ÷£¬¾Í²»×¸ÊoÁË£¬¸ÃÓÃÄĸoÀ(C)Õ¹ÒѾ²»ÑÔ×ÔÃ÷ÁË¡£
²¹³a£ºhttp://code.google.com/p/memcached/wiki/PHPClientComparison