分布式緩存系統(tǒng)Memcached簡介與實(shí)踐
http://blog.csdn.net/hitman9099/archive/2008/09/04/2878417.aspx
緣起: 在數(shù)據(jù)驅(qū)動的web開發(fā)中,經(jīng)常要重復(fù)從數(shù)據(jù)庫中取出相同的數(shù)據(jù),這種重復(fù)極大的增加了數(shù)據(jù)庫負(fù)載。緩存是解決這個問題的好辦法。但是ASP.NET中的雖然已經(jīng)可以實(shí)現(xiàn)對頁面局部進(jìn)行緩存,但還是不夠靈活。此時Memcached或許是你想要的。
Memcached是什么?
Memcached是由Danga Interactive開發(fā)的,高性能的,分布式的內(nèi)存對象緩存系統(tǒng),用于在動態(tài)應(yīng)用中減少數(shù)據(jù)庫負(fù)載,提升訪問速度。
Memcached能緩存什么?
通過在內(nèi)存里維護(hù)一個統(tǒng)一的巨大的hash表,Memcached能夠用來存儲各種格式的數(shù)據(jù),包括圖像、視頻、文件以及數(shù)據(jù)庫檢索的結(jié)果等。
Memcached快么?
非
常快。Memcached使用了libevent(如果可以的話,在linux下使用epoll)來均衡任何數(shù)量的打開鏈接,使用非阻塞的網(wǎng)絡(luò)I/O,對
內(nèi)部對象實(shí)現(xiàn)引用計數(shù)(因此,針對多樣的客戶端,對象可以處在多樣的狀態(tài)), 使用自己的頁塊分配器和哈希表,
因此虛擬內(nèi)存不會產(chǎn)生碎片并且虛擬內(nèi)存分配的時間復(fù)雜度可以保證為O(1).。
Danga Interactive為提升Danga
Interactive的速度研發(fā)了Memcached。目前,LiveJournal.com每天已經(jīng)在向一百萬用戶提供多達(dá)兩千萬次的頁面訪問。而這
些,是由一個由web服務(wù)器和數(shù)據(jù)庫服務(wù)器組成的集群完成的。Memcached幾乎完全放棄了任何數(shù)據(jù)都從數(shù)據(jù)庫讀取的方式,同時,它還縮短了用戶查看
頁面的速度、更好的資源分配方式,以及Memcache失效時對數(shù)據(jù)庫的訪問速度。
Memcached的特點(diǎn)
Memcached的緩存是一種分布式的,可以讓不同主機(jī)上的多個用戶同時訪問, 因此解決了共享內(nèi)存只能單機(jī)應(yīng)用的局限,更不會出現(xiàn)使用數(shù)據(jù)庫做類似事情的時候,磁盤開銷和阻塞的發(fā)生。
Memcached的使用
一 Memcached服務(wù)器端的安裝 (此處將其作為系統(tǒng)服務(wù)安裝)
下載文件:memcached 1.2.1 for Win32 binaries (Dec 23, 2006)
1 解壓縮文件到c:\memcached
2 命令行輸入 'c:\memcached\memcached.exe -d install'
3 命令行輸入 'c:\memcached\memcached.exe -d start' ,該命令啟動 Memcached ,默認(rèn)監(jiān)聽端口為 11211
通過 memcached.exe -h 可以查看其幫助
一個簡單的測試?yán)?/p>
- package com.mapbar.util.cache;
- import java.util.Date;
- import java.util.ResourceBundle;
- import com.danga.MemCached.MemCachedClient;
- import com.danga.MemCached.SockIOPool;
- /**
- * 使用配置文件方式的memcache組件
- *
- */
- public class Cache {
- protected static ResourceBundle rb=ResourceBundle.getBundle("memcached");;
- public static MemCachedClient mcc = new MemCachedClient();
- static {
- init();
- }
- public static void init(){
- synchronized (mcc) {
- System.out.println("init call");
- String[] servers =rb.getString("cache.servers").split(",");
- String[] strWeights =rb.getString("cache.weights").split(",");
- Integer[] weights = new Integer[strWeights.length];
- for(int i=0;i<strWeights.length;i++){
- weights[i]=Integer.parseInt(strWeights[i]);
- }
- //創(chuàng)建一個實(shí)例對象SockIOPool
- SockIOPool pool = SockIOPool.getInstance();
- // set the servers and the weights
- //設(shè)置Memcached Server
- pool.setServers( servers );
- pool.setWeights( weights );
- pool.setInitConn(Integer.parseInt(rb.getString("cache.initConn")));
- pool.setMinConn(Integer.parseInt(rb.getString("cache.minConn")));
- pool.setMaxConn(Integer.parseInt(rb.getString("cache.maxConn")));
- pool.setMaxIdle(Long.parseLong(rb.getString("cache.maxIdle")));
- // set the sleep for the maint thread
- // it will wake up every x seconds and
- // maintain the pool size
- pool.setMaintSleep( 30 );
- // Tcp的規(guī)則就是在發(fā)送一個包之前,本地機(jī)器會等待遠(yuǎn)程主機(jī)
- // 對上一次發(fā)送的包的確認(rèn)信息到來;這個方法就可以關(guān)閉套接字的緩存,
- // 以至這個包準(zhǔn)備好了就發(fā);
- pool.setNagle( false );
- //連接建立后對超時的控制
- pool.setSocketTO( 3000 );
- //連接建立時對超時的控制
- pool.setSocketConnectTO( 0 );
- // initialize the connection pool
- //初始化一些值并與MemcachedServer段建立連接
- pool.initialize();
- // lets set some compression on for the client
- // compress anything larger than 64k
- mcc.setCompressEnable( true );
- mcc.setCompressThreshold( 64 * 1024 );
- }
- }
- protected static void bulidCache(){
- //set(key,value,Date) ,Date是一個過期時間,如果想讓這個過期時間生效的話,這里傳遞的new Date(long date) 中參數(shù)date,需要是個大于或等于1000的值。
- //因為java client的實(shí)現(xiàn)源碼里是這樣實(shí)現(xiàn)的 expiry.getTime() / 1000 ,也就是說,如果 小于1000的值,除以1000以后都是0,即永不過期
- mcc.set( "test", "This is a test String" ,new Date(100000)); //十秒后過期
- }
- protected static void output() {
- //從cache里取值
- String value = (String) mcc.get( "test" );
- System.out.println(value);
- }
- public static void main(String[] args){
- bulidCache();
- output();
- }
- }
其中在classespath下有個memcached.properties文件
內(nèi)容如下:
##muti servers spilt by ,
#這里是你的memcached啟動的地址
cache.servers=192.168.0.116:11211
cache.weights=1
#memcached be used
cache.cluster=true
#set some basic pool settings
#5 initial, 5 min, and 250 max conns
#and set the max idle time for a conn
#to 6 hours 6*60*60*1000
cache.initConn=5
cache.minConn=5
cache.maxConn=250
cache.maxIdle=21600000
在Windows下安裝Memcached
http://www.oschina.net/docs/article/4
很多phper不知道如何在Windows下搭建Memcache的開發(fā)調(diào)試環(huán)境,最近個人也在研究Memcache,記錄下自己安裝搭建的過程。
其實(shí)我開始研究Memcache的時候并不知道居然還有memcached for Win32這個鳥東西,害得我在CnetOS下折騰1天才搞定,今天突然發(fā)現(xiàn)Windows下的Memcache進(jìn)行開發(fā)調(diào)試完全沒有問題,所以寫篇 Memcache的文檔分享給大家。
Windows下的Memcache安裝:
1. 下載memcache的 windows穩(wěn)定版,解壓放某個盤下面,比如在c:\memcached
2. 在終端(也即cmd命令界面)下輸入 ‘c:\memcached\memcached.exe -d install’ 安裝
3. 再輸入: ‘c:\memcached\memcached.exe -d start’ 啟動。NOTE: 以后memcached將作為windows的一個服務(wù)每次開機(jī)時自動啟動。這樣服務(wù)器端已經(jīng)安裝完畢了。
4.下載php_memcache.dll, 請自己查找對應(yīng)的php版本的文件
5. 在C:\winnt\php.ini 加入一行 ‘extension=php_memcache.dll’
6.重新啟動Apache,然后查看一下phpinfo,如果有memcache,那么就說明安裝成功!
memcached的基本設(shè)置:
-p 監(jiān)聽的端口
-l 連接的IP地址, 默認(rèn)是本機(jī)
-d start 啟動memcached服務(wù)
-d restart 重起memcached服務(wù)
-d stop|shutdown 關(guān)閉正在運(yùn)行的memcached服務(wù)
-d install 安裝memcached服務(wù)
-d uninstall 卸載memcached服務(wù)
-u 以的身份運(yùn)行 (僅在以root運(yùn)行的時候有效)
-m 最大內(nèi)存使用,單位MB。默認(rèn)64MB
-M 內(nèi)存耗盡時返回錯誤,而不是刪除項
-c 最大同時連接數(shù),默認(rèn)是1024
-f 塊大小增長因子,默認(rèn)是1.25
-n 最小分配空間,key+value+flags默認(rèn)是48
-h 顯示幫助
Memcache環(huán)境測試:
運(yùn)行下面的php文件,如果有輸出This is a test!,就表示環(huán)境搭建成功。開始領(lǐng)略Memcache的魅力把!
< ?php
$mem = new Memcache;
$mem->connect(”127.0.0.1″, 11211);
$mem->set(’key’, ‘This is a test!’, 0, 60);
$val = $mem->get(’key’);
echo $val;
?>