Memcached學習
緣起: 在數據驅動的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();
}
}