Windows环境MemCache(for Java)的简单运用

来源:互联网 发布:广州淘宝摄影工作室 编辑:程序博客网 时间:2024/05/18 13:47

步骤如下:

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

原创粉丝点击