緣起: 在數(shù)據(jù)驅(qū)動(dòng)的web開發(fā)中,經(jīng)常要重復(fù)從數(shù)據(jù)庫中取出相同的數(shù)據(jù),這種重復(fù)極大的增加了數(shù)據(jù)庫負(fù)載。緩存是解決這個(gè)問題的好辦法。
Memcached是什么?
Memcached是由Danga Interactive開發(fā)的,高性能的,分布式的內(nèi)存對象緩存系統(tǒng),用于在動(dòng)態(tài)應(yīng)用中減少數(shù)據(jù)庫負(fù)載,提升訪問速度。
Memcached能緩存什么?
?????? 通過在內(nèi)存里維護(hù)一個(gè)統(tǒng)一的巨大的hash表,Memcached能夠用來存儲(chǔ)各種格式的數(shù)據(jù),包括圖像、視頻、文件以及數(shù)據(jù)庫檢索的結(jié)果等。
Memcached快么?
?????? 非常快。Memcached使用了libevent(如果可以的話,在linux下使用epoll)來均衡任何數(shù)量的打開鏈接,使用非阻塞的網(wǎng)絡(luò)I/O,對內(nèi)部對象實(shí)現(xiàn)引用計(jì)數(shù)(因此,針對多樣的客戶端,對象可以處在多樣的狀態(tài)), 使用自己的頁塊分配器和哈希表, 因此虛擬內(nèi)存不會(huì)產(chǎn)生碎片并且虛擬內(nèi)存分配的時(shí)間復(fù)雜度可以保證為O(1).。
?????? Danga Interactive為提升Danga Interactive的速度研發(fā)了Memcached。目前,LiveJournal.com每天已經(jīng)在向一百萬用戶提供多達(dá)兩千萬次的頁面訪問。而這些,是由一個(gè)由web服務(wù)器和數(shù)據(jù)庫服務(wù)器組成的集群完成的。Memcached幾乎完全放棄了任何數(shù)據(jù)都從數(shù)據(jù)庫讀取的方式,同時(shí),它還縮短了用戶查看頁面的速度、更好的資源分配方式,以及Memcache失效時(shí)對數(shù)據(jù)庫的訪問速度。
Memcached的特點(diǎn)
?????? Memcached的緩存是一種分布式的,可以讓不同主機(jī)上的多個(gè)用戶同時(shí)訪問, 因此解決了共享內(nèi)存只能單機(jī)應(yīng)用的局限,更不會(huì)出現(xiàn)使用數(shù)據(jù)庫做類似事情的時(shí)候,磁盤開銷和阻塞的發(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' ,該命令啟動(dòng) Memcached ,默認(rèn)監(jiān)聽端口為 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 一個(gè)簡單的應(yīng)用
- 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 ?};??????? ??
- ??????? ??
- ???????? //創(chuàng)建一個(gè)實(shí)例對象SockIOPool????? ??
- ????????SockIOPool?pool?=?SockIOPool.getInstance();??????? ??
- ??????? ??
- ???????? //?set?the?servers?and?the?weights???? ??
- ???????? //設(shè)置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的規(guī)則就是在發(fā)送一個(gè)包之前,本地機(jī)器會(huì)等待遠(yuǎn)程主機(jī)???? ??
- ?????????????????? //?對上一次發(fā)送的包的確認(rèn)信息到來;這個(gè)方法就可以關(guān)閉套接字的緩存,???? ??
- ?????????????????? //?以至這個(gè)包準(zhǔn)備好了就發(fā);???? ??
- ??????????????????pool.setNagle(? false ?);??????? ??
- ???????? //連接建立后對超時(shí)的控制???? ??
- ??????????????????pool.setSocketTO(? 3000 ?);???? ??
- ???????? //連接建立時(shí)對超時(shí)的控制???? ??
- ??????????????????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是一個(gè)過期時(shí)間,如果想讓這個(gè)過期時(shí)間生效的話,這里傳遞的new?Date(long?date)?中參數(shù)date,需要是個(gè)大于或等于1000的值。???? ??
- ???????? //因?yàn)閖ava?client的實(shí)現(xiàn)源碼里是這樣實(shí)現(xiàn)的?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 }; //創(chuàng)建一個(gè)實(shí)例對象SockIOPool SockIOPool pool = SockIOPool.getInstance(); // set the servers and the weights //設(shè)置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的規(guī)則就是在發(fā)送一個(gè)包之前,本地機(jī)器會(huì)等待遠(yuǎn)程主機(jī) // 對上一次發(fā)送的包的確認(rèn)信息到來;這個(gè)方法就可以關(guān)閉套接字的緩存, // 以至這個(gè)包準(zhǔn)備好了就發(fā); pool.setNagle( false ); //連接建立后對超時(shí)的控制 pool.setSocketTO( 3000 ); //連接建立時(shí)對超時(shí)的控制 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是一個(gè)過期時(shí)間,如果想讓這個(gè)過期時(shí)間生效的話,這里傳遞的new Date(long date) 中參數(shù)date,需要是個(gè)大于或等于1000的值。 //因?yàn)閖ava client的實(shí)現(xiàn)源碼里是這樣實(shí)現(xiàn)的 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(); } }
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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