利用memcached java client一个简单的应用

来源:互联网 发布:淘宝上莫桑石值得买吗 编辑:程序博客网 时间:2024/03/29 18:19

关键字: 利用memcached java client一个简单的应用

1.memcached java client一个实现的下载地址

http://www.whalin.com/memcached/#download
2.  利用memcached java client 一个简单的应用

java 代码
  1. package com.danga.MemCached.test;   
  2.   
  3. import java.util.Date;   
  4.   
  5. import com.danga.MemCached.MemCachedClient;   
  6. import com.danga.MemCached.SockIOPool;   
  7.   
  8.   
  9. public class Test {       
  10.     protected static MemCachedClient mcc = new MemCachedClient();      
  11.       
  12.     static {      
  13.         String[] servers ={"192.168.40.4:12000"};      
  14.       
  15.         Integer[] weights = { 3 };      
  16.       
  17.         //创建一个实例对象SockIOPool    
  18.         SockIOPool pool = SockIOPool.getInstance();      
  19.       
  20.         // set the servers and the weights   
  21.         //设置Memcached Server   
  22.         pool.setServers( servers );      
  23.         pool.setWeights( weights );      
  24.       
  25.         // set some basic pool settings      
  26.         // 5 initial, 5 min, and 250 max conns      
  27.         // and set the max idle time for a conn      
  28.         // to 6 hours      
  29.         pool.setInitConn( 5 );      
  30.         pool.setMinConn( 5 );      
  31.         pool.setMaxConn( 250 );      
  32.         pool.setMaxIdle( 1000 * 60 * 60 * 6 );      
  33.       
  34.         // set the sleep for the maint thread      
  35.         // it will wake up every x seconds and      
  36.         // maintain the pool size      
  37.         pool.setMaintSleep( 30 );      
  38.       
  39. //        Tcp的规则就是在发送一个包之前,本地机器会等待远程主机   
  40. //        对上一次发送的包的确认信息到来;这个方法就可以关闭套接字的缓存,   
  41. //        以至这个包准备好了就发;   
  42.         pool.setNagle( false );      
  43.         //连接建立后对超时的控制   
  44.         pool.setSocketTO( 3000 );   
  45.         //连接建立时对超时的控制   
  46.         pool.setSocketConnectTO( 0 );      
  47.       
  48.         // initialize the connection pool      
  49.         //初始化一些值并与MemcachedServer段建立连接   
  50.         pool.initialize();   
  51.               
  52.       
  53.         // lets set some compression on for the client      
  54.         // compress anything larger than 64k      
  55.         mcc.setCompressEnable( true );      
  56.         mcc.setCompressThreshold( 64 * 1024 );      
  57.     }      
  58.           
  59.     public static void bulidCache(){      
  60.         //set(key,value,Date) ,Date是一个过期时间,如果想让这个过期时间生效的话,这里传递的new Date(long date) 中参数date,需要是个大于或等于1000的值。   
  61.         //因为java client的实现源码里是这样实现的 expiry.getTime() / 1000 ,也就是说,如果 小于1000的值,除以1000以后都是0,即永不过期   
  62.         mcc.set( "test""This is a test String" ,new Date(10000));   //十秒后过期   
  63.              
  64.     }      
  65.      
  66.     public static void output() {      
  67.         //从cache里取值   
  68.         String value = (String) mcc.get( "test" );      
  69.         System.out.println(value);       
  70.     }      
  71.           
  72.     public static void main(String[] args){      
  73.         bulidCache();     
  74.         output();          
  75.     }    
  76.       
  77. }      

 


运行输出值为:


This is a test String  

 

3.注释掉buildCache();

十秒后运行,输出值为 null

原创粉丝点击