MemcacheÊÇdanga.comµÄÒ»¸oÏiÄ¿£¬×iÔçÊÇΪ LiveJournal þÎñµÄ£¬Ä¿Ç°È«ÊÀ½ç²»ÉÙÈËʹÓÃÕa¸o»º´aeÏiÄ¿À´¹¹½¨×Ô¼º´o¸ºÔصÄÍøÕ¾£¬À´Öµ£Êý¾Ý¿aµÄѹÁ¦¡£Ëu¿ÉÒÔÓ¦¶ÔÈÎÒa¶a¸oÁ¬½Ó£¬Ê¹ÓÃÇ×eÈuµÄÍøÂçIO¡£ÓÉÓÚËuµÄ¹¤×÷»uÖÆÊÇÔÚÄÚ´aeÖпª±ÙÒ»¿e¿Õ¼a£¬È»ºo½¨Á¢Ò»¸oHashTable£¬Memcached×Ô¹ÜÀiÕaÐ(C)HashTable¡£Memcache¹Ù½ÍøÕ¾£ºhttp://www.danga.com/memcached£¬¸u¶aÏeϸµÄÐÅÏ¢¿ÉÒÔÀ´ÕaÀiÁ˽a¡£
Ϊʲô»aÓÐMemcacheºÍmemcachedÁ½ÖÖÃu³Æ£¿ÆaʵMemcacheÊÇÕa¸oÏiÄ¿µÄÃu³Æ£¬¶ømemcachedÊÇËu*þÎñÆ÷¶ËµÄÖ÷³ÌÐoÎļþÃu£¬ÖªµÀÎÒµÄÒa˼ÁË°Ñ~~~~¡£Ò»¸oÊÇÏiÄ¿Ãu³Æ£¬Ò»¸oÊÇÖ÷³ÌÐoÎļþÃu£¬ÔÚÍøÉÏ¿´µ½Á˺ܶaÈ˲»Ã÷°×£¬ÓÚÊÇ»iÓÃÁË¡£
MemcacheµÄ°²×°
ÖΪÁ½¸o¹ý³Ì£ºmemcacheþÎñÆ÷¶ËµÄ°²×°ºÍmemcached¿Í»§¶ËµÄ°²×°¡£
ËuνþÎñÆ÷¶ËµÄ°²×°¾ÍÊÇÔÚþÎñÆ÷£¨Ò»°a¶¼ÊÇlinuxϵͳ£(C)ÉÏ°²×°MemcacheʵÏÖÊý¾ÝµÄ´ae´¢¡£
Ëuν¿Í»§¶ËµÄ°²×°¾ÍÊÇÖ¸php£¨»oÕßÆaËu³ÌÐo£¬Memcache»¹ÓÐÆaËu²»´iµÄapi½Ó¿ÚÌa¹(C)£(C)ȥʹÓÃ*þÎñÆ÷¶ËµÄMemcacheÌa¹(C)µÄº¯Êý£¬ÐeÒªphpÌi¼ÓÀ(C)Õ¹¡£
PHPµÄMemcache
<?php
//Á¬½Ó
$mem = new Memcache;
$mem->connect("db.nowamagic.net", );
//±£´aeÊý¾Ý
$mem->set('key', 'This is first value', , );
$val = $mem->get('key');
echo "Get key value: " . $val ."<br />";
//Ìae»»Êý¾Ý
$mem->replace('key', 'This is replace value', , );
$val = $mem->get('key');
echo "Get key value: " . $val . "<br />";
//±£´aeÊý×e
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key', $arr, , );
$val = $mem->get('key');
echo "Get key value: ";
print_r($val);
echo "<br />";
//ɾ³ýÊý¾Ý
$mem->delete('key');
$val = $mem->get('key');
echo "Get key value: " . $val . "<br />";
//Ça³ýËuÓÐÊý¾Ý
$mem->flush();
$val = $mem->get('key');
echo "Get key value: ";
print_r($val);
echo "<br />";
//¹Ø±ÕÁ¬½Ó
$mem->close();
?>
Èç¹uÕý³£µÄ»°£¬a¯ÀÀÆ÷½«Êa³o£º
Get key value: This is first value
Get key value: This is replace value
Get key value: Array ( [] => aaa [] => bbb [] => ccc [] => ddd )
Get key value:
Get key value:
*³ÌÐo´uÂeÖÎo**
³oʼ»¯Ò»¸oMemcacheµÄ¶ÔÏo£º$mem = new Memcache;
Á¬½Óµ½ÎÒÃǵÄMemcacheþÎñÆ÷¶Ë£¬µÚÒ»¸o²ÎÊýÊÇþÎñÆ÷µÄIPµØÖ£¬Ò²¿ÉÒÔÊÇÖ÷»uÃu£¬µÚ¶þ¸o²ÎÊýÊÇMemcacheµÄ¿ªÅµÄ¶Ë¿Ú£º$mem->connect("192.168.0.200", 12000);
±£´aeÒ»¸oÊý¾Ýµ½MemcacheþÎñÆ÷ÉÏ£¬µÚÒ»¸o²ÎÊýÊÇÊý¾ÝµÄkey£¬ÓÃÀ´¶¨Î»Ò»¸oÊý¾Ý£¬µÚ¶þ¸o²ÎÊýÊÇÐeÒª±£´aeµÄÊý¾ÝÄÚÈÝ£¬ÕaÀiÊÇÒ»¸o×Öu´®£¬µÚÈý¸o²ÎÊýÊÇÒ»¸o±e¼Ç£¬Ò»°aÉeÖÃΪ0»oÕßMEMCACHE_COMPRESSED¾ÍÐÐÁË£¬µÚËĸo²ÎÊýÊÇÊý¾ÝµÄÓÐЧÆÚ£¬¾ÍÊÇ˵Êý¾ÝÔÚÕa¸oʱ¼aÄÚÊÇÓÐЧµÄ£¬Èç¹u¹ýÈ¥Õa¸oʱ¼a£¬ÄÇô»a±»MemcacheþÎñÆ÷¶ËÇa³ýµoÕa¸oÊý¾Ý£¬µ¥Î»ÊÇÃe£¬Èç¹uÉeÖÃΪ0£¬ÔoÊÇÓÀÔ¶ÓÐЧ£¬ÎÒÃÇÕaÀiÉeÖÃÁË60£¬¾ÍÊÇÒ»ÖÖÓÓÐЧʱ¼a£º$mem->set(¡®key1¡®, ¡®This is first value', 0, 60);
´ÓMemcache*þÎñÆ÷¶Ë»ñÈ¡Ò»ÌoÊý¾Ý£¬ËuÖ»ÓÐÒ»¸o²ÎÊý£¬¾ÍÊÇÐeÒª»ñÈ¡Êý¾ÝµÄkey£¬ÎÒÃÇÕaÀiÊÇÉÏÒ»²½ÉeÖõÄkey1£¬ÏÖÔÚ»ñÈ¡Õa¸oÊý¾ÝºoÊa³oÊa³o£º
$val = $mem->get('key¡a);
echo "Get key value: " . $val;
ÏÖÔÚÊÇʹÓÃreplace½¨À´Ìae»»µoÉÏÃaekey1µÄÖµ£¬replace½¨µÄ²ÎÊý¸usetÊÇÒ»ÑuµÄ£¬²»¹ýµÚÒ»¸o²ÎÊýkey1ÊDZØÐeÊÇÒªÌae»»Êý¾ÝÄÚÈݵÄkey£¬×iºoÊa³oÁË£º
$mem->replace('key', 'This is replace value', , );
$val = $mem->get('key');
echo "Get key value: " . $val;
ͬÑuµÄ£¬MemcacheÒ²ÊÇ¿ÉÒÔ±£´aeÊý×eµÄ£¬ÏÂÃaeÊÇÔÚMemcacheÉÏÃae±£´aeÁËÒ»¸oÊý×e£¬È»ºo»ñÈ¡»ØÀ´²¢Êa³o£º
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key', $arr, , );
$val = $mem->get('key');
print_r($val);
ÏÖÔÚɾ³ýÒ»¸oÊý¾Ý£¬Ê¹ÓÃdelte½Ó¿Ú£¬²ÎÊý¾ÍÊÇÒ»¸okey£¬È»ºo¾ÍÄܹ»°ÑMemcache*þÎñÆ÷Õa¸okeyµÄÊý¾Ýɾ³ý£¬×iºoÊa³oµÄʱºoûÓнa¹u£º
$mem->delete('key');
$val = $mem->get('key');
echo "Get key value: " . $val . "<br />";
×iºoÎÒÃÇ°ÑËuÓеı£´aeÔÚMemcacheþÎñÆ÷ÉϵÄÊý¾Ý¶¼Ça³ý£¬»a¢ÏÖÊý¾Ý¶¼Ã»ÓÐÁË£¬×iºoÊa³okey2µÄÊý¾ÝΪ¿Õ£¬×iºo¹Ø±ÕÁ¬½Ó£º
$mem->flush();
$val = $mem->get('key');
echo "Get key value: ";
print_r($val);
echo "<br />";
MemcacheµÄʹÓÃ
ʹÓÃMemcacheµÄÍøÕ¾Ò»°aÁ÷Á¿¶¼ÊDZȽϴoµÄ£¬ÎªÁË»º½aÊý¾Ý¿aµÄѹÁ¦£¬ÈÃMemcache×÷Ϊһ¸o»º´aeÇøÓo£¬°Ñ²¿ÖÐÅÏ¢±£´aeÔÚÄÚ´aeÖУ¬ÔÚÇ°¶ËÄܹ»Ñ¸ËٵĽøÐдaeÈ¡¡£ÄÇôһ°aµÄ½¹µa¾ÍÊǼ¯ÖÐÔÚÈçºÎÖµ£Êý¾Ý¿aѹÁ¦ºÍ½øÐÐÖ²¼Ê½£¬±Ï¾¹µ¥Ì¨MemcacheµÄÄÚ´aeÈÝÁ¿µÄÓÐÏ޵ġ£ÎÒÕaÀi¼oµ¥Ìa³oÎҵĸoÈË¿´¨£¬Î´¾Êµ¼u£¬È¨µ±²Î¿¼¡£
*Ö²¼Ê½Ó¦ÓÃ
Memcache±¾À´Ö§³ÖÖ²¼Ê½£¬ÎÒÃÇ¿Í»§¶ËÉÔ¼Ó¸ÄÔi£¬¸uºÃµÄÖ§³Ö¡£ÎÒÃǵÄkey¿ÉÒÔÊʵ±½øÐÐÓйaeÂɵÄa×°£¬±ÈÈçÒÔuserΪÖ÷µÄÍøÕ¾À´Ëµ£¬Ã¿¸oÓû§¶¼ÓÐUser ID£¬ÄÇô¿ÉÒÔ°´Õչ̶¨µÄIDÀ´½øÐÐÌaÈ¡ºÍ´aeÈ¡£¬±ÈÈç1¿ªÍµÄÓû§±£´aeÔÚµÚһ̨MemcacheþÎñÆ÷ÉÏ£¬ÒÔ2¿ªÍµÄÓû§µÄÊý¾Ý±£´aeÔÚµÚ¶þÌ¥MecacheþÎñÆ÷ÉÏ£¬´aeÈ¡Êý¾Ý¶¼ÏÈ°´ÕÕUser IDÀ´½øÐÐÏaÓ¦µÄת»»ºÍ´aeÈ¡¡£
µ«ÊÇÕa¸oÓÐȱµa£¬¾ÍÊÇÐeÒª¶ÔUser ID½øÐÐÅжϣ¬Èç¹uÒµÎñ²»Ò»Ö£¬»oÕßÆaËuÀaÐ͵ÄÓ¦Ó㬿ÉÄܲ»ÊÇÄÇôºÏÊÊ£¬ÄÇô¿ÉÒÔ¸u¾Ý×Ô¼ºµÄʵ¼ÊÒµÎñÀ´½øÐп¼ÂÇ£¬»oÕßÈ¥Ïe¸uºÏÊʵĽ¨¡£
¼oÉÙÊý¾Ý¿aѹÁ¦
Õa¸oËaÊDZȽÏÖØÒªµÄ£¬ËuÓеÄÊý¾Ý»u±¾É϶¼ÊDZ£´aeÔÚÊý¾Ý¿aµ±Öеģ¬Ã¿´ÎƵ±µÄ´aeÈ¡Êý¾Ý¿a£¬µ¼ÖÂÊý¾Ý¿aÐÔÄܼ«¾ßϽµ£¬ÎިͬʱþÎñ¸u¶aµÄÓû§£¬±ÈÈçMySQL£¬ÌرðƵ±µÄËø±i£¬ÄÇôÈÃMemcacheÀ´Öµ£Êý¾Ý¿aµÄѹÁ¦°É¡£ÎÒÃÇÐeÒªÒ»ÖָĶ¯±È½ÏС£¬²¢ÇÒÄܹ»²»»a´o¹aeÄ£¸Ä±aÇ°¶ËµÄ½Ê½À´½øÐиıaÄ¿Ç°µÄ¼Ü¹¹¡£
ÎÒ¿¼ÂǵÄÒ»ÖÖ¼oµ¥½¨£º
ºo¶ËµÄÊý¾Ý¿a²Ù×÷Ä£¿e£¬°ÑËuÓеÄSelect²Ù×÷ÌaÈ¡³oÀ´£¨update/delete/insert²»¹Ü£(C)£¬È»ºo°Ñ¶ÔÓ¦µÄSQL½øÐÐÏaÓ¦µÄhashËa¨¼ÆËaµÃ³oÒ»¸ohashÊý¾Ýkey£¨±ÈÈçMD5»oÕßSHA£(C)£¬È»ºo°ÑÕa¸okeyÈ¥MemcacheÖвeÕÒÊý¾Ý£¬Èç¹uÕa¸oÊý¾Ý²»´aeÔÚ£¬ËµÃ÷»¹Ã»Ð´Èeµ½»º´aeÖУ¬ÄÇô´ÓÊý¾Ý¿a°ÑÊý¾ÝÌaÈ¡³oÀ´£¬Ò»¸oÊÇÊý×eÀa¸ñʽ£¬È»ºo°ÑÊý¾ÝÔÚsetµ½MemcacheÖУ¬key¾ÍÊÇÕa¸oSQLµÄhashÖµ£¬È»ºoÏaÓ¦µÄÉeÖÃÒ»¸oʧЧʱ¼a£¬±ÈÈçÒ»¸oСʱ£¬ÄÇôһ¸oСʱÖеÄÊý¾Ý¶¼ÊÇ´Ó»º´aeÖÐÌaÈ¡µÄ£¬ÓÐЧ¼oÉÙÊý¾Ý¿aµÄѹÁ¦¡£È±µaÊÇÊý¾Ý²»ÊµÊ±£¬µ±Êý¾Ý×oÁËÐÞ¸ÄÒÔºo£¬Îިʵʱµ½Ç°¶ËÏÔʾ£¬²¢ÇÒ»¹ÓпÉÄܶÔÄÚ´aeÕ¼ÓñȽϴo£¬±Ï¾¹Ã¿´Îselect³oÀ´µÄÊý¾ÝÊýÁ¿¿ÉÄܱȽϾ޴o£¬Õa¸oÊÇÐeÒª¿¼ÂǵÄÒoËØ¡£
MemcacheµÄ°²È«
ÎÒÃÇÉÏÃaeµÄMemcacheþÎñÆ÷¶Ë¶¼ÊÇÖ±½Óͨ¹ý¿Í»§¶ËÁ¬½ÓºoÖ±½Ó²Ù×÷£¬Ã»ÓÐÈκεÄÑeÖ¤¹ý³Ì£¬ÕaÑuÈç¹uþÎñÆ÷ÊÇÖ±½Ó±(C)¶ÔÚ»¥ÁªÍøÉϵĻ°ÊDZȽÏΣÏÕ£¬ÇaÔoÊý¾Ý鶱»ÆaËuÎÞ¹ØÈËÔ±²e¿´£¬ÖØÔoþÎñÆ÷±»ÈeÇÖ£¬ÒoΪMecacheÊÇÒÔrootȨÏÞÔËÐеģ¬¿oÇÒÀiÃae¿ÉÄÜ´aeÔÚÒ»Ð(C)ÎÒÃÇδ֪µÄbug»oÕßÊÇ»º³aÇøÒç³oµÄÇe¿o£¬ÕaÐ(C)¶¼ÊÇÎÒÃÇδ֪µÄ£¬ËuÒÔΣÏÕÐÔÊÇ¿ÉÒÔÔ¤¼uµÄ¡£ÎªÁË°²È«Æð¼u£¬ÎÒ×oÁ½µa½¨Òe£¬Äܹ»ÉÔ΢µÄÀÖ¹ºÚ¿ÍµÄÈeÇÖ»oÕßÊý¾ÝµÄй¶¡£
*ÄÚÍøÃÎÊ**
×iºÃ°ÑÁ½Ì¨þÎñÆ÷Ö®¼aµÄÃÎÊÊÇÄÚÍøÐÎ̬µÄ£¬Ò»°aÊÇWebþÎñÆ÷¸uMemcacheþÎñÆ÷Ö®¼a¡£ÆÕ±eµÄþÎñÆ÷¶¼ÊÇÓÐÁ½¿eÍø¿¨£¬Ò»¿eÖ¸Ïo»¥ÁªÍø£¬Ò»¿eÖ¸ÏoÄÚÍø£¬ÄÇô¾ÍÈÃWebþÎñÆ÷ͨ¹ýÄÚÍøµÄÍø¿¨À´ÃÎÊMemcacheþÎñÆ÷£¬ÎÒÃÇMemcacheµÄþÎñÆ÷ÉÏÆo¶¯µÄʱºo¾Í¼aÌýÄÚÍøµÄIPµØֺͶ˿ڣ¬ÄÚÍø¼aµÄÃÎÊÄܹ»ÓÐЧ×eÖ¹ÆaËuǨµÄÃÎÊ¡£
MemcacheþÎñÆ÷¶ËÉeÖüaÌýͨ¹ýÄÚÍøµÄ192.168.0.200µÄipµÄ11211¶Ë¿Ú£¬Õ¼ÓÃ1024MBÄÚ´ae£¬²¢ÇÒÔÊÐi×i´o1024¸o²¢¢Á¬½Ó¡£
*ÉeÖÃÀ»ðǽ**
À»ðǽÊǼoµ¥ÓÐЧµÄ½Ê½£¬Èç¹uÈ´ÊÇÁ½Ì¨þÎñÆ÷¶¼ÊǹÒÔÚÍøµÄ£¬²¢ÇÒÐeҪͨ¹ýÍaÍøIPÀ´ÃÎÊMemcacheµÄ»°£¬ÄÇô¿ÉÒÔ¿¼ÂÇʹÓÃÀ»ðǽ»oÕß´uÀi³ÌÐoÀ´¹ýÂËǨÃÎÊ¡£Ò»°aÎÒÃÇÔÚLinuxÏ¿ÉÒÔʹÓÃiptables»oÕßFreeBSDϵÄipfwÀ´Ö¸¶¨Ò»Ð(C)¹aeÔoÀÖ¹Ò»Ð(C)ǨµÄÃÎÊ£¬±ÈÈçÎÒÃÇ¿ÉÒÔÉeÖÃÖ»ÔÊÐiÎÒÃǵÄWebþÎñÆ÷À´ÃÎÊÎÒÃÇMemcacheþÎñÆ÷£¬Í¬Ê±×eÖ¹ÆaËuµÄÃÎÊ¡£
ÉÏÃaeµÄiptables¹aeÔo¾ÍÊÇÖ»ÔÊÐi192.168.0.2ÕąWebþÎñÆ÷¶ÔMemcacheþÎñÆ÷µÄÃÎÊ£¬Äܹ»ÓÐЧµÄ×eÖ¹Ò»Ð(C)ǨÃÎÊ£¬ÏaÓ¦µÄÒ²¿ÉÒÔÔo¼ÓÒ»Ð(C)ÆaËuµÄ¹aeÔoÀ´¼ÓÇ¿°²È«ÐÔ£¬Õa¸o¿ÉÒÔ¸u¾Ý×Ô¼ºµÄÐeÒªÀ´×o¡£