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

5年以前  |  阅读数:903 次  |  编程语言: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Öá£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例