2010-11-21 19 views
1

我正在使用mysql作爲數據庫在php中構建一個web應用程序。Memcached對於有很多選擇的小型數據庫的好處

我正在使用memcached來存儲會話,我想如果我還應該存儲(在另一個memcached實例中)數據庫記錄。

數據庫將是非常小,並在一段時間內更新一次。雖然會有很多選擇,但我們預計會有很高的流量。

對於一個小型數據庫,如果我將記錄存儲在memcached中,或者我應該將它留給mysql緩存並調整數據庫以獲得大量連接(將讚賞用於優化mysql的一些指針),我會受益嗎?

謝謝

回答

0

爲什麼不運行一些基準,然後傳播負載!使用APC

http://www.php.net/manual/en/intro.apc.php

Page generated in 0.003202 secs using 2 DB calls and 0 DBCache hits (users ttl=5 countries ttl=5 and both items have expired) 

Page generated in 0.002728 secs using 1 DB calls and 1 DBCache hits (users ttl=5 countries ttl=immortal and users has expired) 

Page generated in 0.000067 secs using 0 DB calls and 2 DBCache hits (users ttl=5 countries ttl=immortal and both are fetched from cache) 

how fast - 0.000067 ouch !! 

樣品的PHP腳本

<?php 

require_once "CacheDB.php"; 

ob_start(); 

echo "<h3>APC info:</h3>"; 

print_r(apc_sma_info()); 

try{ 

    //I assume you've already have a db connection available for the cacheDB to use 

    $db = @new mysqli("127.0.0.1","foo_dbo","pass","foo_db",3306); 

    if ($db->connect_errno) 
     throw new Exception("Could not connect: " . $db->connect_error); 

    //start the demo... 

    $startTime = microtime(true); 

    $cacheDB = new CacheDB($db); 

    $rows = $cacheDB->Query("call list_users()", CacheDB::TTL_5); //5 second Time To Live (TTL) (30 secs might be more realistic) 
    if($rows){ 
     echo "<h3>Users:</h3><ul>"; 
     foreach($rows as $row) echo sprintf("<li>%s</li>", $row["username"]); 
     echo "</ul>"; 
    } 

    $rows = $cacheDB->Query("call list_countries()", CacheDB::TTL_IMMORTAL); //never expires 

    if($rows){ 
     echo "<h3>Countries:</h3><ul>"; 
     foreach($rows as $row) echo sprintf("<li>%s</li>", $row["name"]); 
     echo "</ul>"; 
    } 

    echo sprintf("<p><b>Page generated in %s secs using %d DB calls and %d DBCache hits</b></p><p>Refresh me !!</p>", 
      number_format(microtime(true) - $startTime, 6, ".", ""), 
      $cacheDB->GetDBHits(), $cacheDB->GetCacheHits()); 

    $db->close(); 
} 
catch(Exception $ex) 
{ 
    ob_clean(); 
    echo sprintf("zomg borked - %s", $ex->getMessage()); 
} 

//finally 

ob_end_flush(); 

?> 
+0

內存緩存支持分佈 - http://stackoverflow.com/questions/815041/memcached-vs-apc-which-one - 我應該選擇 – ajreal 2010-11-21 16:14:33

+0

這麼真實,但它的代價是http://www.mysqlperformanceblog.com/2006/09/27/apc-or-memcached/(我不瞭解你,但我是一個速度怪胎,如果我在我的負載平衡的網絡服務器上覆制) – 2010-11-21 16:21:31

+0

取決於您如何看待它,在APC緩存的數據/代碼僅適用於執行APC的服務器。在使用memcache時,只執行一次,存儲值並在多個服務器上重複使用 – ajreal 2010-11-21 16:39:51

相關問題