緣起: 在數據驅動的web開發中,經常要重復從數據庫中取出相同的數據,這種重復極大的增加了數據庫負載。緩存是解決這個問題的好辦法。
Memcached是什么?
Memcached是由Danga Interactive開發的,高性能的,分布式的內存對象緩存系統,用于在動態應用中減少數據庫負載,提升訪問速度。
Memcached能緩存什么?
?????? 通過在內存里維護一個統一的巨大的hash表,Memcached能夠用來存儲各種格式的數據,包括圖像、視頻、文件以及數據庫檢索的結果等。
Memcached快么?
?????? 非常快。Memcached使用了libevent(如果可以的話,在linux下使用epoll)來均衡任何數量的打開鏈接,使用非阻塞的網絡I/O,對內部對象實現引用計數(因此,針對多樣的客戶端,對象可以處在多樣的狀態), 使用自己的頁塊分配器和哈希表, 因此虛擬內存不會產生碎片并且虛擬內存分配的時間復雜度可以保證為O(1).。
?????? Danga Interactive為提升Danga Interactive的速度研發了Memcached。目前,LiveJournal.com每天已經在向一百萬用戶提供多達兩千萬次的頁面訪問。而這些,是由一個由web服務器和數據庫服務器組成的集群完成的。Memcached幾乎完全放棄了任何數據都從數據庫讀取的方式,同時,它還縮短了用戶查看頁面的速度、更好的資源分配方式,以及Memcache失效時對數據庫的訪問速度。
Memcached的特點
?????? Memcached的緩存是一種分布式的,可以讓不同主機上的多個用戶同時訪問, 因此解決了共享內存只能單機應用的局限,更不會出現使用數據庫做類似事情的時候,磁盤開銷和阻塞的發生。
Memcached的使用
一 、Memcached服務器端的安裝 (此處將其作為系統服務安裝)
???? 下載文件: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 ,默認監聽端口為 11211
? 通過 memcached.exe -h 可以查看其幫助
二、客戶端使用
????? 下載memcached java client:http://www.whalin.com/memcached/#download
?? 1 解壓后將java_memcached-release_2.0.1.jar jar包添加到工程的classpath中
?????? 2 利用memcached java client 一個簡單的應用
- package ?com.danga.MemCached.test;???? ??
- ??? ??
- import ?java.util.Date;???? ??
- ??? ??
- import ?com.danga.MemCached.MemCachedClient;???? ??
- import ?com.danga.MemCached.SockIOPool;???? ??
- ??? ??
- ??? ??
- public ? class ?Test?{???????? ??
- ???? protected ? static ?MemCachedClient?mcc?=? new ?MemCachedClient();??????? ??
- ??????? ??
- ???? static ?{??????? ??
- ????????String[]?servers?={ "192.168.40.4:12000" };??????? ??
- ??????? ??
- ????????Integer[]?weights?=?{? 3 ?};??????? ??
- ??????? ??
- ???????? //創建一個實例對象SockIOPool????? ??
- ????????SockIOPool?pool?=?SockIOPool.getInstance();??????? ??
- ??????? ??
- ???????? //?set?the?servers?and?the?weights???? ??
- ???????? //設置Memcached?Server???? ??
- ????????pool.setServers(?servers?);??????? ??
- ????????pool.setWeights(?weights?);??????? ??
- ??????? ??
- ???????? //?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??????? ??
- ????????pool.setInitConn(? 5 ?);??????? ??
- ????????pool.setMinConn(? 5 ?);??????? ??
- ????????pool.setMaxConn(? 250 ?);??????? ??
- ????????pool.setMaxIdle(? 1000 ?*? 60 ?*? 60 ?*? 6 ?);??????? ??
- ??????? ??
- ???????? //?set?the?sleep?for?the?maint?thread??????? ??
- ???????? //?it?will?wake?up?every?x?seconds?and??????? ??
- ???????? //?maintain?the?pool?size??????? ??
- ????????pool.setMaintSleep(? 30 ?);??????? ??
- ??????? ??
- ???????? //?Tcp的規則就是在發送一個包之前,本地機器會等待遠程主機???? ??
- ?????????????????? //?對上一次發送的包的確認信息到來;這個方法就可以關閉套接字的緩存,???? ??
- ?????????????????? //?以至這個包準備好了就發;???? ??
- ??????????????????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 ?);??????? ??
- ????}??????? ??
- ??????????? ??
- ???? public ? static ? void ?bulidCache(){??????? ??
- ???????? //set(key,value,Date)?,Date是一個過期時間,如果想讓這個過期時間生效的話,這里傳遞的new?Date(long?date)?中參數date,需要是個大于或等于1000的值。???? ??
- ???????? //因為java?client的實現源碼里是這樣實現的?expiry.getTime()?/?1000?,也就是說,如果?小于1000的值,除以1000以后都是0,即永不過期???? ??
- ????????mcc.set(? "test" ,? "This?is?a?test?String" ?, new ?Date( 11211 ));??? ??
- ???? //十秒后過期???? ??
- ?????????????? ??
- ????}??????? ??
- ?????? ??
- ???? public ? static ? void ?output()?{??????? ??
- ???????? //從cache里取值???? ??
- ????????String?value?=?(String)?mcc.get(? "test" ?);??????? ??
- ????????System.out.println(value);???????? ??
- ????}??????? ??
- ??????????? ??
- ???? public ? static ? void ?main(String[]?args){??????? ??
- ????????bulidCache();?????? ??
- ????????output();??????????? ??
- ????}????? ??
- ??????? ??
- }?????????
package com.danga.MemCached.test; import java.util.Date; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; public class Test { protected static MemCachedClient mcc = new MemCachedClient(); static { String[] servers ={"192.168.40.4:12000"}; Integer[] weights = { 3 }; //創建一個實例對象SockIOPool SockIOPool pool = SockIOPool.getInstance(); // set the servers and the weights //設置Memcached Server pool.setServers( servers ); pool.setWeights( weights ); // 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 pool.setInitConn( 5 ); pool.setMinConn( 5 ); pool.setMaxConn( 250 ); pool.setMaxIdle( 1000 * 60 * 60 * 6 ); // set the sleep for the maint thread // it will wake up every x seconds and // maintain the pool size pool.setMaintSleep( 30 ); // Tcp的規則就是在發送一個包之前,本地機器會等待遠程主機 // 對上一次發送的包的確認信息到來;這個方法就可以關閉套接字的緩存, // 以至這個包準備好了就發; 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 ); } public static void bulidCache(){ //set(key,value,Date) ,Date是一個過期時間,如果想讓這個過期時間生效的話,這里傳遞的new Date(long date) 中參數date,需要是個大于或等于1000的值。 //因為java client的實現源碼里是這樣實現的 expiry.getTime() / 1000 ,也就是說,如果 小于1000的值,除以1000以后都是0,即永不過期 mcc.set( "test", "This is a test String" ,new Date(11211)); //十秒后過期 } public static void output() { //從cache里取值 String value = (String) mcc.get( "test" ); System.out.println(value); } public static void main(String[] args){ bulidCache(); output(); } }
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
