PHPÀ©Õ¹Ä£¿émemcached³¤Á¬½ÓʹÓ÷½·¨·ÖÎö

6年以前  |  阅读数:1010 次  |  编程语言:PHP 
  ÍøÉϹa*ºÁ÷´«×ÅһƪÎÄÕ£¬½²ÊophpµÄÁ½¸oÀ(C)չģ¿ememcacheºÍmemcachedµÄÇø±ð£¬ÆaÖÐÌØÒaÇ¿µ÷ÁËmemcachedÓememcachedÒ»¸oºÜ´oµÄÇø±ðÊÇmemcachedÄ£¿e²»Ö§³Ö³¤Á¬½Ó¡£ÒÔÖÁÓÚºoÀ´ºÜ¶aÄeÎÒ¶¼ÈÏΪmemcachedÊDz»Ö§³Ö³¤Á¬½ÓµÄ£¬Æaʵ²»È»£¬memcachedÀ(C)չģ¿e´ÓºÜÔçµÄ°ae±¾¿ªÊ¼¾ÍÒѾ­Ö§³Ö³¤Á¬½ÓÁË¡£´ÓÀ(C)չģ¿eµÄÔ´Âe×¢ÊÓÖÐÎÒÃǾÍÄÜ¿´µ½£º

/* {{{ Memcached::__construct([string persistent_id[, callback on_new[, string connection_str]]]))

Creates a Memcached object, optionally using persistent memcache connection */

static PHP_METHOD(Memcached, __construct)

{

´ÓphpµÄÊÖ²aÉiÉÏÎÒÃÇ¿ÉÒÔ¿´µ½memcachedµÄÀ(C)չģ¿eÌa¹(C)µÄ¹¹Ôiº¯ÊýÌa¹(C)Ò»¸o²ÎÊý_persistent_id_¿ÉÑ¡Ïi£¬ÊÖ²aÖÐÕaÑu½eÉÜ£º

  ĬÈÏÇe¿oÏ£¬MemcachedʵÀýÔÚÇeÇo½aÊøºo»a±»Ïu»Ù¡£µ«¿ÉÒÔÔÚ´´½¨Ê±Í¨¹ý_`persistent_id`_Ϊÿ¸oʵÀýÖ¸¶¨Î¨Ò»µÄID£¬ ÔÚÇeÇo¼a¹²ÏiʵÀý¡£ËuÓÐͨ¹ýÏaͬµÄ_`persistent_id`_Öµ´´½¨µÄʵÀý¹²Ïiͬһ¸oÁ¬½Ó¡£ 

Õa¸o²ÎÊýµÄº¬Òa¾ÍÊÇ˵Èç¹uÄa´«µÝÁËÒ»¸oÃuÃuid¸øµ½¹¹Ôi½¨£¬ÄÇô¾Í»a½¨Á¢³¤Á¬½Ó£¬Í¨³£ÎÒÃÇʹÓõͼÊÇPHP-FPMģʽ£¬ÕaÑuPHP-FPM½ø³Ì¾Í»aºÍmemcached*þÎñ¼oÀuÒ»Ìo³¤Á¬½ÓͨµÀ¡£ÎÒÃÇÒ²¿ÉÒÔÀi½aΪpersistent_id¾ÍÊÇÒ»¸oÁ¬½Ó³ØÃu×Ö£¬ËuÓÐphp-fpm½ø³Ì¶¼ÊÇÕa¸oÁ¬½Ó³ØÖеÄÒ»Ô±¡£

 µ«ÎÒÃÇÐeҪעÒaµÄÊÇphpÊǽaÊÍÐÔÓiÑÔ£¬µ±phpµÚÒ»´Îͨ¹ýmemachedÄ£¿e½¨Á¢Æð³¤Á¬½Óºo£¬ÇмǺoÐøµÄphpÖ´ÐоͲ»ÒªÔÙͨ¹ýmemcachedµÄ¹¹Ôiº¯Êý¹¹½¨Ïaͬpersistent_idÃuÃuµÄ³¤Á¬½Ó£¬¿ÉÒÔ½¨Á¢²»Í¬persistent_idÃu×ֵij¤Á¬½Ó£¬Èç¹uÊÇÏaͬµÄÃu×Ö±»phpÖØ¸´Ö´ÐУ¬Ò»¶¨»aµ¼ÖÂphp-fpmµÄ½ø³ÌÒi³£µ¼ÖÂÓememcachedµÄͨÐÅÔ½À´Ô½Âý£¬Í¬Ê±¸u¾ÝlibmemcachedµÄ°ae±¾²»Í¬»¹»aµ¼ÖÂphp²uÉucoredump¡£

ÄÇôÎÒÃÇÈçºÎ±ÜÃaµ¥¸ophp-fpmÔÚ½¨Á¢ÍeÒÔpersistent_idÃuÃuµÄ³¤Á¬½Óºo²»ÔÙÖØ¸´½¨Á¢³¤Á¬½ÓÄØ£¿ÆaʵÔÚPHP´øÓÐÆÀ×¢µÄÊÖ²aÉÏÊÇÓн²½aµÄ£¬ÄÚÈÝÈçÏ£º

When using persistent connections, it is important to not re-add servers.

This is what you do not want to do:


    $mc = new Memcached('mc');
    $mc->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
    $mc->addServers(array(
      array('mc1.example.com',11211),
      array('mc2.example.com',11211),
    ));

Every time the page is loaded those servers will be appended to the list resulting in many simultaneous open connections to the same server. The addServer/addServers functions to not check for existing references to the specified servers.

A better approach is something like:


    $mc = new Memcached('mc');
    $mc->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
    if (!count($mc->getServerList())) {
      $mc->addServers(array(
        array('mc1.example.com',11211),
        array('mc2.example.com',11211),
      ));
    }  

ͨ¹ýʹÓÃgetServerList()½¨À´¼i²eµ±Ç°Ö´ÐÐʹÓõÄphp-fpm½ø³ÌÈÝÆ÷ÖÐÊÇñÒѾ­´aeÔÚÏaͬÃu×ֵij¤Á¬½Ó×ÊÔ´£¬Èç¹u´aeÔھͲ»ÒªÖظ´Ê¹ÓÃaddServers() ½*¨À´ÐÂÔo³¤Á¬½ÓÅaÖá£

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8