PHPÄڴ滺´æ¹¦ÄÜmemcachedʾÀý

5年以前  |  阅读数:940 次  |  编程语言:PHP 

ÏÂÎļoµ¥½eÉÜÁËmemcachedÀaµÄÓ¦ÓÃʾÀý,¾ßÓÐÒ»¶¨µÄ²Î¿¼¼ÛÖµ£¬¸ÐÐËȤµÄС»i°eÃÇ¿ÉÒԲο¼Ò»Ï¡£

Ò»¡¢memcached ¼o½e

Ôںܶa³¡ºÏ£¬ÎÒÃǶ¼»aÌýµ½ memcached Õa¸oÃu×Ö£¬µ«ºÜ¶aͬѧֻÊÇÌý¹ý£¬²¢Ã»ÓÐÓùý»oʵ¼ÊÁ˽a¹ý£¬Ö»ÖªµÀËuÊÇÒ»¸oºÜ²»´iµÄ¶«¶«¡£ÕaÀi¼oµ¥½eÉÜһϣ¬memcached ÊǸßЧ¡¢¿iËÙµÄ*Ö²¼Ê½ÄÚ´ae¶ÔÏo»º´aeϵͳ£¬Ö÷ÒªÓÃÓÚ¼ÓËÙ WEB ¶¯Ì¬Ó¦ÓóÌÐo¡£

¶þ¡¢memcached °²×°

Ê×ÏÈÊÇÏÂÔØ memcached ÁË£¬Ä¿Ç°×iаae±¾ÊÇ 1.1.12£¬Ö±½Ó´Ó¹Ù*½ÍøÕ¾¼´¿ÉÏÂÔص½ memcached-1.1.12.tar.gz¡£³ý´ËÖ®Ía£¬memcached Óõ½ÁË libevent£¬ÎÒÏÂÔصÄÊÇ libevent-1.1a.tar.gz¡£

½ÓÏÂÀ´ÊÇ*ֱ𽫠libevent-1.1a.tar.gz ºÍ memcached-1.1.12.tar.gz ½a¿ª°u¡¢±aÒe¡¢°²×°£º

tar -xzf libevent-1.1a.tar.gz

cd libevent-1.1a

./configure --prefix=/usr

make

make install

cd ..

tar -xzf memcached-1.1.12.tar.gz

cd memcached-1.1.12

./configure --prefix=/usr

make

make install

°²×°Íe³ÉÖ®ºo£¬memcached Ó¦¸ÃÔÚ /usr/bin/memcached¡£

Èý¡¢ÔËÐÐ memcached ÊØ»¤³ÌÐo

ÔËÐÐ memcached ÊØ»¤³ÌÐoºÜ¼oµ¥£¬Ö»ÐeÒ»¸oÃuÁiÐм´¿É£¬²»ÐeÒªÐÞ¸ÄÈκÎÅaÖÃÎļþ£¨Ò²Ã»ÓÐÅaÖÃÎļþ¸øÄaÐ޸ģ(C)£º
/usr/bin/memcached -d -m 128 -l 192.168.1.1 -p 11211 -u httpd

²ÎÊý½aÊÍ£º

  • -d ÒÔÊØ»¤³ÌÐo£¨daemon£(C)*½Ê½ÔËÐÐ memcached£»
  • -m ÉeÖà memcached ¿ÉÒÔʹÓõÄÄÚ´ae´oС£¬µ¥Î»Îª M£»
  • -l ÉeÖüaÌýµÄ IP µØÖ*£¬Èç¹uÊDZ¾»uµÄ»°£¬Í¨³£¿ÉÒÔ²»ÉeÖô˲ÎÊý£»
  • -p ÉeÖüaÌýµÄ¶Ë¿Ú£¬Ä¬ÈÏΪ 11211£¬ËuÒÔÒ²¿ÉÒÔ²»ÉeÖô˲ÎÊý£»
  • -u Ö¸¶¨Óû§£¬Èç¹uµ±Ç°Îª root µÄ»°£¬ÐeҪʹÓô˲ÎÊýÖ¸¶¨Óû§¡£

µ±È»£¬»¹ÓÐÆaËu²ÎÊý¿ÉÒÔÓã¬man memcached һϾͿÉÒÔ¿´µ½ÁË¡£

ËÄ¡¢memcached µÄ¹¤×÷Ô­Ài

Ê×ÏÈ memcached ÊÇÒÔÊØ»¤³ÌÐo½Ê½ÔËÐÐÓÚÒ»¸o»o¶a¸oþÎñÆ÷ÖУ¬Ëaeʱ½ÓÊÜ¿Í»§¶ËµÄÁ¬½Ó²Ù×÷£¬¿Í»§¶Ë¿ÉÒÔÓɸ÷ÖÖÓiÑÔ±aд£¬Ä¿Ç°ÒÑÖªµÄ¿Í»§¶Ë API °uÀ¨ Perl/PHP/Python/Ruby/Java/C#/C µÈµÈ¡£PHP µÈ¿Í»§¶ËÔÚÓe memcached þÎñ½¨Á¢Á¬½ÓÖ®ºo£¬½ÓÏÂÀ´µÄÊÂÇe¾ÍÊÇ´aeÈ¡¶ÔÏoÁË£¬Ã¿¸o±»´aeÈ¡µÄ¶ÔÏo¶¼ÓÐÒ»¸oΨһµÄ±eʶu key£¬´aeÈ¡²Ù×÷¾uͨ¹ýÕa¸o key ½øÐУ¬±£´aeµ½ memcached ÖеĶÔÏoʵ¼ÊÉÏÊÇÅÖÃÄÚ´aeÖеģ¬²¢²»ÊDZ£´aeÔÚ cache ÎļþÖеģ¬ÕaÒ²ÊÇΪʲô memcached Äܹ»Èç´Ë¸ßЧ¿iËÙµÄÔ­Òo¡£×¢Òa£¬ÕaÐ(C)¶ÔÏo²¢²»Êdz־õģ¬þÎñÍ£Ö¹Ö®ºo£¬Ài±ßµÄÊý¾Ý¾Í»a¶ªÊ§¡£

Îa¡¢PHP ÈçºÎ×÷Ϊ memcached ¿Í»§¶Ë

ÓÐÁ½ÖÖ½¨¿ÉÒÔʹ PHP ×÷Ϊ memcached ¿Í»§¶Ë£¬µ÷Óà memcached µÄ*þÎñ½øÐжÔÏo´aeÈ¡²Ù×÷¡£

µÚÒ»ÖÖ£¬PHP ÓÐÒ»¸o½Ð×o memcache µÄÀ(C)Õ¹£¬Linux ϱaÒeʱÐeÒª´øÉÏ ¨Cenable-memcache[=DIR] Ñ¡Ïi£¬Window ÏÂÔoÔÚ php.ini ÖÐÈ¥µo php_memcache.dll Ç°±ßµÄ×¢ÊÍ*u£¬Ê¹Æa¿ÉÓá£

³ý´ËÖ®Ía£¬»¹ÓÐÒ»ÖÖ½¨£¬¿ÉÒԱܿªÀ(C)Õ¹¡¢ÖØбaÒeËu´øÀ´µÄÂe*³£¬ÄǾÍÊÇÖ±½ÓʹÓà php-memcached-client¡£

±¾ÎÄÑ¡ÓõڶþÖÖ*½Ê½£¬ËaȻЧÂÊ»a±ÈÀ(C)Õ¹¿aÉÔ²iÒ»Ð(C)£¬µ«ÎÊÌa²»´o¡£

Áu¡¢PHP memcached Ó¦ÓÃʾÀý

Ê×ÏÈ ÏÂÔØ memcached-client.php£¬ÔÚÏÂÔØÁË memcached-client.php Ö®ºo£¬¾Í¿ÉÒÔͨ¹ýÕa¸oÎļþÖеÄÀa¡°memcached¡±¶Ô memcached þÎñ½øÐвÙ×÷ÁË¡£Æaʵ´uÂeµ÷ÓÃdz£¼oµ¥£¬Ö÷Òª»aÓõ½µÄ½¨ÓÐ add()¡¢get()¡¢replace() ºÍ delete()£¬½¨ËµÃ÷ÈçÏ£º


    add ($key, $val, $exp = 0)

Íu memcached ÖÐдÈe¶ÔÏo£¬$key ÊǶÔÏoµÄΨһ±eʶ*u£¬$val ÊÇдÈeµÄ¶ÔÏoÊý¾Ý£¬$exp Ϊ¹ýÆÚʱ¼a£¬µ¥Î»ÎªÃe£¬Ä¬ÈÏΪ²»ÏÞʱ¼a£»


    get ($key)

´Ó memcached ÖлñÈ¡¶ÔÏoÊý¾Ý£¬Í¨¹ý¶ÔÏoµÄΨһ±eʶ*u $key »ñÈ¡£»


    replace ($key, $value, $exp=0)

ʹÓà $value Ìae»» memcached ÖбeʶuΪ $key µÄ¶ÔÏoÄÚÈÝ£¬²ÎÊýÓe add() ½*¨Ò»Ñu£¬Ö»ÓÐ $key ¶ÔÏo´aeÔÚµÄÇe¿oϲŻaÆð×÷Óã»


    delete ($key, $time = 0)

ɾ³ý memcached Öбeʶ*uΪ $key µÄ¶ÔÏo£¬$time Ϊ¿ÉÑ¡²ÎÊý£¬±iʾɾ³ý֮ǰÐeÒªµÈ´ý¶a³¤Ê±¼a¡£

ÏÂÃaeÊÇÒ»¶Î¼oµ¥µÄ²aÊÔ´uÂe£¬´uÂeÖжԱeʶ*uΪ 'mykey' µÄ¶ÔÏoÊý¾Ý½øÐдaeÈ¡²Ù×÷£º


    <?php 
    // °uº¬ memcached ÀaÎļþ 
    require_once('memcached-client.php'); 
    // Ñ¡ÏiÉeÖÃ 
    $options = array( 
     'servers' => array('192.168.1.1:11211'), //memcached *þÎñµÄµØÖ*¡¢¶Ë¿Ú£¬¿ÉÓöa¸oÊý×eÔªËرiʾ¶a¸o memcached *þÎñ 
     'debug' => true, //ÊÇ*ñ´o¿ª debug 
     'compress_threshold' => 10240, //³¬¹ý¶aÉÙ×Ö½ÚµÄÊý¾Ýʱ½øÐÐѹËo 
     'persistant' => false //ÊÇ*ñʹÓó־ÃÁ¬½Ó 
     ); 
    // ´´½¨ memcached ¶ÔÏoʵÀý 
    $mc = new memcached($options); 
    // ÉeÖô˽ű¾Ê¹ÓõÄΨһ±eʶ*u 
    $key = 'mykey'; 
    // Íu memcached ÖÐдÈe¶ÔÏo 
    $mc->add($key, 'some random strings'); 
    $val = $mc->get($key); 
    echo "n".str_pad('$mc->add() ', 60, '_')."n"; 
    var_dump($val); 
    // Ìae»»ÒÑдÈeµÄ¶ÔÏoÊý¾ÝÖµ 
    $mc->replace($key, array('some'=>'haha', 'array'=>'xxx')); 
    $val = $mc->get($key); 
    echo "n".str_pad('$mc->replace() ', 60, '_')."n"; 
    var_dump($val); 
    // ɾ³ý memcached ÖеĶÔÏo 
    $mc->delete($key); 
    $val = $mc->get($key); 
    echo "n".str_pad('$mc->delete() ', 60, '_')."n"; 
    var_dump($val); 
    ?>

ÊDz»ÊǺܼoµ¥£¬ÔÚʵ¼ÊÓ¦ÓÃÖУ¬Í¨³£»a°ÑÊý¾Ý¿a²eѯµÄ½a¹u¼¯±£´aeµ½ memcached ÖУ¬Ï´ÎÃÎÊʱֱ½Ó´Ó memcached ÖлñÈ¡£¬¶ø²»ÔÙ×oÊý¾Ý¿a²eѯ²Ù×÷£¬ÕaÑu¿ÉÒÔÔںܴo³Ì¶ÈÉϼoÇaÊý¾Ý¿aµÄ¸ºµ£¡£Í¨³£»a½« SQL Ói¾a md5() Ö®ºoµÄÖµ×÷ΪΨһ±eʶu key¡£Ï±ßÊÇÒ»¸oÀuÓà memcached À´»º´aeÊý¾Ý¿a²eѯ½a¹u¼¯µÄʾÀý£¨´Ë´uÂeƬ¶Î½o½ÓÉϱߵÄʾÀý´uÂe£(C)£º


    <?php 
    $sql = 'SELECT * FROM users'; 
    $key = md5($sql); //memcached ¶ÔÏo±eʶ*u 
    { 
     // ÔÚ memcached ÖÐδ»ñÈ¡µ½»º´aeÊý¾Ý£¬ÔoʹÓÃÊý¾Ý¿a²eѯ»ñÈ¡¼Ç¼¼¯¡£ 
     echo "n".str_pad('Read datas from MySQL.', 60, '_')."n"; 
     $conn = mysql_connect('localhost', 'test', 'test'); 
     mysql_select_db('test'); 
     $result = mysql_query($sql); 
     while ($row = mysql_fetch_object($result)) 
      $datas[] = $row; 
     // ½«Êý¾Ý¿aÖлñÈ¡µ½µÄ½a¹u¼¯Êý¾Ý±£´aeµ½ memcached ÖУ¬ÒÔ¹(C)Ï´Î*ÃÎÊʱʹÓᣠ
     $mc->add($key, $datas); 
    { 
     echo "n".str_pad('Read datas from memcached.', 60, '_')."n"; 
    } 
    var_dump($datas); 
    ?> 

¿ÉÒÔ¿´³o£¬Ê¹Óà memcached Ö®ºo£¬¿ÉÒÔ¼oÉÙÊý¾Ý¿aÁ¬½Ó¡¢²eѯ²Ù×÷£¬Êý¾Ý¿a¸ºÔØÏÂÀ´ÁË£¬½Å±¾µÄÔËÐÐËÙ¶ÈÒ²Ìa¸ßÁË¡£

֮ǰÎÒÔø¾­Ð´¹ýһƪÃuΪ¡¶PHP ʵÏÖ¶aþÎñÆ÷¹²Ïi SESSION Êý¾Ý¡ÎÄÕ£¬ÎÄÖÐµÄ SESSION ÊÇʹÓÃÊý¾Ý¿a±£´aeµÄ£¬ÔÚ²¢¢ÃÎÊÁ¿´oµÄʱºo£¬*þÎñÆ÷µÄ¸ºÔØ»aºÜ´o£¬¾­³£»a³¬³o MySQL ×i´oÁ¬½ÓÊý£¬ÀuÓà memcached£¬ÎÒÃÇ¿ÉÒԺܺõؽa¾oÕa¸oÎÊÌa£¬¹¤×÷Ô­ÀiÈçÏ£º

  • Óû§ÃÎÊÍøҳʱ£¬²e¿´ memcached ÖÐÊÇñÓе±Ç°Óû§µÄ SESSION Êý¾Ý£¬Ê¹Óà session_id() ×÷ΪΨһ±eʶu£»Èç¹uÊý¾Ý´aeÔÚ£¬ÔoÖ±½Óµ»Ø£¬Èç¹u²»´aeÔÚ£¬ÔÙ½øÐÐÊý¾Ý¿aÁ¬½Ó£¬»ñÈ¡ SESSION Êý¾Ý£¬²¢½«´ËÊý¾Ý±£´aeµ½ memcached ÖУ¬¹(C)Ï´ÎʹÓã»

  • µ±Ç°µÄ PHP ÔËÐнaÊø£¨»oʹÓÃÁË session_write_close()£(C)ʱ£¬»aµ÷Óà My_Sess::write() ½¨£¬½«Êý¾ÝдÈeÊý¾Ý¿a£¬ÕaÑuµÄ»°£¬Ã¿´ÎÈÔÈ»»aÓÐÊý¾Ý¿a²Ù×÷£¬¶ÔÓÚÕa¸o½¨£¬Ò²ÐeÒª½øÐÐÓÅ»¯¡£Ê¹ÓÃÒ»¸oÈ«¾Ö±aÁ¿£¬¼Ç¼Óû§½øÈeÒ³ÃaeʱµÄ SESSION Êý¾Ý£¬È»ºoÔÚ write() ½¨ÄڱȽϴËÊý¾ÝÓeÏeҪдÈeµÄ SESSION Êý¾ÝÊÇñÏaͬ£¬²»Í¬²Å½øÐÐÊý¾Ý¿aÁ¬½Ó¡¢Ð´ÈeÊý¾Ý¿a£¬Í¬Ê±½« memcached ÖжÔÓ¦µÄ¶ÔÏoɾ³ý£¬Èç¹uÏaͬµÄ»°£¬Ôo±iʾ SESSION Êý¾Ýδ¸Ä±a£¬ÄÇô¾Í¿ÉÒÔ²»×oÈκβÙ×÷£¬Ö±½Óµ»ØÁË£»

  • ÄÇôÓû§ SESSION ¹ýÆÚʱ¼aÔoô½a¾oÄØ£¿¼ÇµÃ memcached µÄ add() ½¨Óиo¹ýÆÚʱ¼a²ÎÊý $exp Â𣿰ÑÕa¸o²ÎÊýÖµÉeÖóÉСÓÚ SESSION ×i´o´ae»iʱ¼a¼´¿É¡£ÁiÍa±ðÍuÁ˸øÄÇÐ(C)Ò»Ö±ÔÚÏßµÄÓû§ÑÓÐø SESSION ʱ³¤£¬Õa¸o¿ÉÒÔÔÚ write() ½¨Öнa¾o£¬Í¨¹ýÅжÏʱ¼a£¬*uºÏÌo¼þÔo¸uÐÂÊý¾Ý¿aÊý¾Ý¡£

ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu¶Ô´o¼ÒµÄѧϰÓÐËu°iÖu£¬Ò²Ï£Íu´o¼Ò¶a¶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分页类完整实例