Ò». Linux Ï°²×ª

    1. Ê×ÏÈÊÇÏÂÔØ memcached ÁË£¬Ä¿Ç°×îа汾ÊÇ 1.2.1£¬Ö±½Ó´Ó¹Ù·½ÍøÕ¾¼´¿ÉÏÂÔص½ memcached-1.2.1.tar.gz ¡£
            ³ý´ËÖ®Í⣬memcached Óõ½ÁË libevent £¬ÎÒÏÂÔصÄÊÇ libevent-1.3a.tar.gz¡£

            memcached¹Ù·½°æ±¾ÏÂÔصØÖ·£ºhttp://www.danga.com/memcached/
            libevent¹Ù·½°æ±¾ÏÂÔصØÖ·£ºhttp://monkey.org/%7Eprovos/libevent/

    2. ½ÓÏÂÀ´ÊǷֱ𽫠libevent-1.3a.tar.gz ºÍ memcached-1.2.1.tar.gz ½â¿ª°ü¡¢±àÒë¡¢°²×°£º
            ÎÒ°²×°memcached ÔÚ /usr/bin/memcachedλÖá£

            # tar -xzf libevent-1.3a.tar.gz
            # cd libevent-1.3a
            # ./configure –prefix=/usr
            # make
            # make install
            # cd ..
            # tar -xzf memcached-1.2.1.tar.gz
            # cd memcached-1.2.1
            # ./configure –prefix=/usr
            # make
            # make install

          NOTE£ºÈç¹û libevent ²»ÊÇ°²×°ÔÚ /usr Ŀ¼Ï£¬ÄÇôÐèÒª°Ñ libevent-1.2a.so.1 ¿½±´/Á´½Ó£¨ls -s /usr/local/lib/libevent-1.1.so.1 /usr/lib£©µ½ /usr/lib ÖУ¬ ·ñÔòmemcached ÎÞ·¨Õý³£¼ÓÔØ¡£ÔËÐÐÒÔÏÂÃüÁîÀ´Æô¶¯ memcached¡£

    3. ÔËÐÐ memcached ÊØ»¤³ÌÐò
            ÔËÐÐ memcached ÊØ»¤³ÌÐòºÜ¼òµ¥£¬Ö»ÐèÒ»¸öÃüÁîÐм´¿É£¬²»ÐèÒªÐÞ¸ÄÈκÎÅäÖÃÎļþ£¨Ò²Ã»ÓÐÅäÖÃÎļþ¸øÄãÐ޸Ġ           £©£º

            #/usr/bin/memcached -d -m 128 -l 192.168.1.117 -p 11211 -u nobody     

            /usr/bin/memcached -d -m 128 -l 192.168.1.117 -p 11211 -u httpd
            ²ÎÊý½âÊÍ£º
            -d ÒÔÊØ»¤³ÌÐò£¨daemon£©·½Ê½ÔËÐÐ memcached£»
            -m ÉèÖà memcached ¿ÉÒÔʹÓõÄÄÚ´æ´óС£¬µ¥Î»Îª M£»
            -l ÉèÖüàÌýµÄ IP µØÖ·£¬Èç¹ûÊDZ¾»úµÄ»°£¬Í¨³£¿ÉÒÔ²»ÉèÖô˲ÎÊý£»
            -p ÉèÖüàÌýµÄ¶Ë¿Ú£¬Ä¬ÈÏΪ 11211£¬ËùÒÔÒ²¿ÉÒÔ²»ÉèÖô˲ÎÊý£»
            -u Ö¸¶¨Óû§£¬Èç¹ûµ±Ç°Îª root µÄ»°£¬ÐèҪʹÓô˲ÎÊýÖ¸¶¨Óû§¡£

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

    4. ±àÒëPHP configure¼ÓÉÏ ¨Cenable-memcache[=DIR] Ñ¡Ïî¡£ÎÒÒÔÇ°¶¼ÊǾ²Ì¬°²×° PHPÄ£¿é£¬´Ë´Î °²×°memcache PHPÄ£¿é²ÉÓö¯Ì¬Ä£¿é(DSO)·½Ê½

          memcache PHPÄ£¿éÏÂÔصØÖ·£ºhttp://pecl.php.net/package/memcache

          # tar -vxzf memcache-1.5.tgz
          # cd memcache-1.5
          # /usr/local/php/bin/phpize
          # ./configure –enable-memcache –with-php-config=/usr/local/php/bin/php-config –with-zlib-dir
          # make
          # make install

          °²×°Íêºó»áÓÐÀàËÆÕâÑùµÄÌáʾ£º
          Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/

          °ÑÕâ¸ö¼Çס
          È»ºóÐÞ¸Äphp.ini
          °Ñ
          extension_dir = “./”
          ÐÞ¸ÄΪ
          extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/”

          ²¢Ìí¼ÓÒ»ÐÐ
          extension=memcache.so
    
¶þ. Windows Ï°²×°

              1. ÏÂÔØmemcacheµÄwindows°æ£¬½âѹ·ÅÔÚE:\soft\memcached-1.2.1-win32
                  NOTE: memcached Windows°æ±¾ÏÂÔصØÖ·£ºhttp://jehiah.cz/projects/memcached-win32/

              2. ÔÚÖÕ¶ËÏÂÊäÈë ‘E:\soft\memcached-1.2.1-win32\memcached.exe -d install’ °²×°
              3. ÔÚÖÕ¶ËÏÂÊäÈë ‘E:\soft\memcached-1.2.1-win32\memcached.exe -d start’ Æô¶¯
                  NOTE: ÒÔºómemcached½«×÷ΪwindowsµÄÒ»¸ö·þÎñÿ´Î¿ª»úʱ×Ô¶¯Æô¶¯¡£
              4            Use the server, by default listening to port 11211
              5. ÏÂÔØpeclµÄÄ£¿é°ü£¬½âѹËõºó½«ÆäÖеÄphp_memcache.dll¿¼µ½D:\php\ext ÖÐ
                  NOTE: phpºÍpeclµÄ°æ±¾ÒªÒ»Ö¡£

              6. ÔÚC:\WINDOWS\php.ini ¼ÓÈëÒ»ÐÐ ‘extension=php_memcache.dll’

Èý. ʹÓ÷½·¨£º¼ûPHPÊÖ²áMemcache Functions

ÎļþÃû³Æ£ºe:/webroot/example.php
<?php
    $memcache = new Memcache;
    $memcache->connect('localhost', 11211) or die ("Could not connect");

    $version = $memcache->getVersion();
    echo "Server's version: ".$version."<br/>\n";

    /**
                 * Ô­À´¾ÍÊÇ»ùÀà¡£ºÜ¶àphp³ÌÐòÔ±ÓÃËüÀ´´«µÝһϵÁбäÁ¿µÄÖµ£¬¶øͬʱÓÖÀÁµÃÈ¥´´½¨Ò»¸ö×Ô¼ºµÄÀà¡£
                 * Õâ¸ö»ùÀàÖ»ÄÜ´«µÝÊôÐÔ£¬¶ø²»Äܶ¨Òå·½·¨¡£ÒòΪ£¬Ò»µ©À౻ʵÁл¯ÒԺ󣬾Ͳ»ÄÜÔÚÌí¼Ó·½·¨ÁË¡£
                 * ÔÙ˵µÄÃ÷°×Ò»µã£¬Õâ¸östdClass¾ÍÀàËÆÓÚC++ÀïÃæµÄstructur¡£
                 * Äã¿ÉÒÔÓÃËüÀ´´æ´¢ºÜ¶à±äÁ¿ÊôÐÔ£¬µ«ÊÇûÓз½·¨¡£¾ÍÊÇÕâÑù¡£
                 * stdClass is a default PHP object.
                 */

    $tmp_object = new stdClass;
    $tmp_object->str_attr = 'test';
    $tmp_object->int_attr = 123;

    $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
    echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";

    $get_result = $memcache->get('key');
    echo "Data from the cache:<br/>\n";

    var_dump($get_result);

?>

Table of Contents

Memcache::add – Add an item to the server
Memcache::addServer – Add a memcached server to connection pool
Memcache::close – Close memcached server connection
Memcache::connect – Open memcached server connection
memcache_debug – Turn debug output on/off
Memcache::decrement – Decrement item’s value
Memcache::delete – Delete item from the server
Memcache::flush – Flush all existing items at the server
Memcache::get – Retrieve item from the server
Memcache::getExtendedStats – Get statistics from all servers in pool
Memcache::getStats – Get statistics of the server
Memcache::getVersion – Return version of the server
Memcache::increment – Increment item’s value
Memcache::pconnect – Open memcached server persistent connection
Memcache::replace – Replace value of the existing item
Memcache::set – Store data at the server
Memcache::setCompressThreshold – Enable automatic compression of large values

<div style=”background-color:#FFD7FF;border:1px solid      #FF66FF;padding:5px;”>
sss
</div>

发表评论

邮箱地址不会被公开。 必填项已用*标注