Windows環境MemCache(for Java)的簡單運用

來源:互聯網
上載者:User

步驟如下:

1.下載memcached for Win32:

http://jehiah.cz/projects/memcached-win32/

安裝啟動並執行方法:

http://www.ccvita.com/258.html

 

2.下載memcache的Java支援包

http://www.whalin.com/memcached/#download

 

3.代碼

  1. package demo;
  2. import java.util.Date;
  3. import com.danga.MemCached.MemCachedClient;
  4. import com.danga.MemCached.SockIOPool;
  5. public class Test {
  6.     protected static MemCachedClient mcc = new MemCachedClient();       
  7.     
  8.     static {       
  9.         String[] servers ={"127.0.0.1:11211"};       
  10.        
  11.         Integer[] weights = { 3 };       
  12.        
  13.         //建立一個執行個體對象SockIOPool     
  14.         SockIOPool pool = SockIOPool.getInstance();       
  15.        
  16.         // set the servers and the weights    
  17.         //設定Memcached Server    
  18.         pool.setServers( servers );       
  19.         pool.setWeights( weights );       
  20.        
  21.         // set some basic pool settings       
  22.         // 5 initial, 5 min, and 250 max conns       
  23.         // and set the max idle time for a conn       
  24.         // to 6 hours       
  25.         pool.setInitConn( 5 );       
  26.         pool.setMinConn( 5 );       
  27.         pool.setMaxConn( 250 );       
  28.         pool.setMaxIdle( 1000 * 60 * 60 * 6 );       
  29.        
  30.         // set the sleep for the maint thread       
  31.         // it will wake up every x seconds and       
  32.         // maintain the pool size       
  33.         pool.setMaintSleep( 30 );       
  34.        
  35. //        Tcp的規則就是在發送一個包之前,本地機器會等待遠程主機    
  36. //        對上一次發送的包的確認資訊到來;這個方法就可以關閉通訊端的緩衝,    
  37. //        以至這個包準備好了就發;    
  38.         pool.setNagle( false );       
  39.         //串連建立後對逾時的控制    
  40.         pool.setSocketTO( 3000 );    
  41.         //串連建立時對逾時的控制    
  42.         pool.setSocketConnectTO( 0 );       
  43.        
  44.         // initialize the connection pool       
  45.         //初始化一些值並與MemcachedServer段建立串連    
  46.         pool.initialize();    
  47.                
  48.        
  49.         // lets set some compression on for the client       
  50.         // compress anything larger than 64k       
  51.         mcc.setCompressEnable( true );       
  52.         mcc.setCompressThreshold( 64 * 1024 );       
  53.     }       
  54.            
  55.     public static void bulidCache(){       
  56.         //set(key,value,Date) ,Date是一個到期時間,如果想讓這個到期時間生效的話,這裡傳遞的new Date(long date) 中參數date,需要是個大於或等於1000的值。    
  57.         //因為java client的實現源碼裡是這樣實現的 expiry.getTime() / 1000 ,也就是說,如果 小於1000的值,除以1000以後都是0,即永不到期    
  58.         mcc.set( "test", "This is a test String" ,new Date(10000));   //十秒後到期    
  59.               
  60.     }       
  61.       
  62.     public static void output() {       
  63.         //從cache裡取值    
  64.         String value = (String) mcc.get( "test" );       
  65.         System.out.println(value);        
  66.     }       
  67.            
  68.     public static void main(String[] args){       
  69.         bulidCache();      
  70.         output();           
  71.     } 
  72. }

運行:

運行輸出值為:This is a test String

注釋掉buildCache();

十秒後運行,輸出值為 null

 

注意:

1.程式需要log4j.jar

2.memcache預設連接埠:11211

 

其他資源:

http://www.javayou.com/diary/7141

http://www.javaeye.com/topic/61898

本文參考自:http://ttitfly.javaeye.com/blog/110495

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.