2009年1月7日 星期三

memcached - 解決高流量問題

memcached - 解決高流量問題




###################################################

# 文章來源 http://www.ecstart.com

# 作者 : FIEND

# 轉貼請註明出處

###################################################



介紹 :





現在機器的記憶體真的比起過去便宜太多了....



看到這個套件 感觸蠻多的 ^^ ....



使用這個套件 必需是個有經驗 而且具備分析和規畫能力的 programer ..



因為它必需要建置 services 時 就開始想怎麼去規畫它 .





http://tw2.php.net/manual/en/intro.memcache.php





什麼是 memcache :



memcache 官方網站的介紹 -



memcached is a high-performance, distributed memory object caching
system, generic in nature, but intended for use in speeding up dynamic
web applications by alleviating database load.



Danga Interactive developed memcached to enhance the speed of LiveJournal.com, a site which was already doing 20 million+ dynamic page views per day for 1 million users with a bunch of webservers and a bunch of database servers.
memcached dropped the database load to almost nothing, yielding faster
page load times for users, better resource utilization, and faster
access to the databases on a memcache miss.





##############



台灣同時幾萬人就很了不起了 @@



百萬人環境 存取資料庫將會非常的可怕 , 一般這種流量等級的網站是絕對不會輕易的把資料庫的存取對外的 .



所以 能夠節省 動態網頁 / user 存取動作去接觸到 db 的動作 能快取就盡量快取 ( 最好能沒有 db 就盡量全部從 db doump 出靜態 html )



當然這樣的苦和想法 早在很久以前就大家都玩過了 , 很多做 http 技術多年的朋友們看到這個工具應該都會覺得現在的硬體科技進步太多了 ^^ .



而 這個工具主要是為了 降低 存取 資料庫次數及要求 將 動態網頁 所需要的存取行為快取下來 , 而沿生出來的工具 .



當然 它還有一個好處是可以去避免 sql injection 啦... @@





感想 :



@@ 不過 這麼幹算你狠 ....



我記得多年前 在某一家流量蠻大的公司...



@@ 同時線上人數四萬人 , 會員人數二百多萬..每日點擊次數 幾千萬 , 而且 server 還是 p4 1.x 及 p3 古董 等級的 cpu 而 記憶體 也才 幾百mb 就是屌了.



光是靜態的一個服務就要幾台 做 分散式架構了 ...而且 我們在做這種東西時都只能存在 硬碟 .. ( 記憶體那有這麼大 @@ )



而它提供了 記憶體存取 物件 , 而且可以 分散式的存取 .



現在寫程式的人真幸福... @@ 看到這個 tool 我直接聯想就是... 記憶體真便宜 哈哈哈....



這個玩法 之前和一個南非工程師在閒聊 他們公司是 java 好像也是用類似這樣的工具來做的 在南非有五萬人會同時去存取他們的伺服器...



用這個方法 一台伺服器就可以搞定五萬人同時存取... @@ 只是 記憶體用的超兇... 不過總比 買一堆伺服器好吧  ccc



在無名還沒賣掉前 我在 it home 看到 無名小站用了 幾百台伺服器才搞定 超高流量存取量的問題 ( 我家伺服器還在貨櫃上 厚~ 別跟大家說你用 php 架 services  ) .....



看到這篇報導時我覺得當時還真是會亂花錢 , 還好賣掉了 cccc ....



你家網站存取量超級高而且機器用很兇嗎??? 玩看看吧. ^^





範例

In this example, an object is being saved in the cache and then
retrieved back. Object and other non-scalar types are serialized before
saving, so it's impossible to store resources (i.e. connection
identifiers and others) in the cache.
複製內容到剪貼板
代碼:
<?php



$memcache = new Memcache;

$memcache->connect('localhost', 11211) or die ("Could not connect");



$version = $memcache->getVersion();

echo "Server's version: ".$version."

\n";



$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)

\n";



$get_result = $memcache->get('key');

echo "Data from the cache:

\n";



var_dump($get_result);



?>
複製內容到剪貼板
代碼:
<?php



$session_save_path = "tcp://$host:$port?persistent=1&weight=2&timeout=2&retry_interval=10,&nbsp; ,tcp://$host:$port  ";

ini_set('session.save_handler', 'memcache');

ini_set('session.save_path', $session_save_path);



?>

沒有留言: