windows下安裝memcached

來源:互聯網
上載者:User

memcached官網
http://www.danga.com/memcached/
memcached windows官網
http://jehiah.cz/projects/memcached-win32/

memcached 1.2.0 for Win32為最新版,需libevent 1.2

Unzip the binaries in your desired directory (eg. c:\memcached)
Install the service using the command: 'c:\memcached\memcached.exe -d install' from either the command line
Start the server from the Microsoft Management Console or by running the following command: 'c:\memcached\memcached.exe -d start'
Use the server, by default listening to port 11211
Use 'memcached.exe -h' for extra help and command line server

以後memcached將作為windows的一個服務每次開機時自動啟動。
在php.ini 去掉 'extension=php_memcache.dll'前的注釋
下載pecl的memcache模組包到ext目錄
NOTE: php和pecl的版本要一致。

  1. <?php
  2. $memcache_obj = new Memcache;
  3. /* connect to memcached server */
  4. $memcache_obj->connect('localhost', 11211);
  5. /*
  6. set value of item with key 'var_key', using on-the-fly compression
  7. expire time is 50 seconds
  8. */
  9. $memcache_obj->set('var_key', 'some really big variable', MEMCACHE_COMPRESSED, 50);
  10. echo $memcache_obj->get('var_key');
  11. ?>

顯示"some really big variable"就是成功了

memcached for Win32

This is a port of memcached to the win32 architecture by Kronuz

The win32 version of memcached can be run both as a NT Service or from the command line.
To install memcached as a service, follow the next steps:

  1. Unzip the binaries in your desired directory (eg. c:\memcached)
  2. Install the service using the command: 'c:\memcached\memcached.exe -d install' from either the command line
  3. Start the server from the Microsoft Management Console or by running the following command: 'c:\memcached\memcached.exe -d start'
  4. Use the server, by default listening to port 11211

Use 'memcached.exe -h' for extra help and command line server
You can check the ChangeLog to see what's new.

PHP memcached 應用樣本

首先 下載 memcached-client.php,在下載了 memcached-client.php 之後,就可以通過這個檔案中的類“memcached”對 memcached 服務進行操作了。其實代碼調用非常簡單,主要會用到的方法有 add()、get()、replace() 和 delete(),方法說明如下:

add ($key, $val, $exp = 0)
往 memcached 中寫入對象,$key 是對象的唯一識別碼,$val 是寫入的對象資料,$exp 為到期時間,單位為秒,預設為不限時間;

get ($key)
從 memcached 中擷取對象資料,通過對象的唯一識別碼 $key 擷取;

replace ($key, $value, $exp=0)
使用 $value 替換 memcached 中標識符為 $key 的對象內容,參數與 add() 方法一樣,只有 $key 對象存在的情況下才會起作用;

delete ($key, $time = 0)
刪除 memcached 中標識符為 $key 的對象,$time 為選擇性參數,表示刪除之前需要等待多長時間。

下面是一段簡單的測試代碼,代碼中對標識符為 ‘mykey’ 的對象資料進行存取操作:

以下是引用片段:
<?php
// 包含 memcached 類檔案
require_once('memcached-client.php');
// 選項設定
$options = array(
    'servers' => array('192.168.1.1:11211′), //memcached 服務的地址、連接埠,可用多個數組元素表示多個 memcached 服務
    'debug' => true, //是否開啟 debug
    'compress_threshold' => 10240, //超過多少位元組的資料時進行壓縮
    'persistant' => false //是否使用持久串連
    );
// 建立 memcached 對象執行個體
$mc = new memcached($options);
// 設定此指令碼使用的唯一識別碼
$key = 'mykey';
// 往 memcached 中寫入對象
$mc->add($key, 'some random strings');
$val = $mc->get($key);
echo "n".str_pad('$mc->add() ', 60, '_')."n";
var_dump($val);
// 替換已寫入的對象資料值
$mc->replace($key, array('some'=>'haha', 'array'=>'xxx'));
$val = $mc->get($key);
echo "n".str_pad('$mc->replace() ', 60, '_')."n";
var_dump($val);
// 刪除 memcached 中的對象
$mc->delete($key);
$val = $mc->get($key);
echo "n".str_pad('$mc->delete() ', 60, '_')."n";
var_dump($val);
?>

是不是很簡單,在實際應用中,通常會把資料庫查詢的結果集儲存到 memcached 中,下次訪問時直接從 memcached 中擷取,而不再做資料庫查詢操作,這樣可以在很大程度上減輕資料庫的負擔。通常會將 SQL 陳述式 md5() 之後的值作為唯一識別碼 key。下邊是一個利用 memcached 來快取資料庫查詢結果集的樣本(此程式碼片段緊接上邊的範例程式碼):

以下是引用片段:
<?php
$sql = 'SELECT * FROM users';
$key = md5($sql);   //memcached 物件識別碼
if ( !($datas = $mc->get($key)) ) {
    // 在 memcached 中未擷取到快取資料,則使用資料庫查詢擷取記錄集。
    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;
    // 將資料庫中擷取到的結果集資料儲存到 memcached 中,以供下次訪問時使用。
    $mc->add($key, $datas);
} else {
    echo "n".str_pad('Read datas from memcached.', 60, '_')."n";
}
var_dump($datas);
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.