重复容易,改变很难
java,,,不错的玩具
posts - 21,  comments - 42,  trackbacks - 0
使用java调用memcache,获取数据
 1,下载客户端
      下载java客户端API,实际上是一个jar包。
    http://www.danga.com/memcached/apis.bml
    memcache支持很多种客户端调用如:Perl、php、python、Ruby、java、C#、C等等
    在java中目前支持两种客户端“Java API for memcached”和“Improved Java API for memcached”
    我用的是前者,因为它有详细的说明文档
   
    下载http://img.whalin.com/memcached/jdk6/standard/java_memcached-release_2.0.1.jar

 2,开始用java程序调用memcached   
  

在eclipse中新建一个工程,将上面下载的java_memcached-release_2.0.1.jar引入工程。
      新建java类memcachedTest
    import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;


public class memcachedTest {
 // create a static client as most installs only need
 // a single instance
 protected static MemCachedClient mcc = new MemCachedClient();
 
 //连接memcache
 static{
  
  // set up connection pool once at class load
  // server list and weights
  String[] servers =
   {
        "192.168.0.20:11211",
     "192.168.0.20:11212"
   };

  Integer[] weights = { 3,2 };

  // grab an instance of our connection pool
  SockIOPool pool = SockIOPool.getInstance();

  // set the servers and the weights
  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 );

  // set some TCP settings
  // disable nagle
  // set the read timeout to 3 secs
  // and don't set a connect timeout
  pool.setNagle( false );
  pool.setSocketTO( 3000 );
  pool.setSocketConnectTO( 0 );

  // initialize the connection pool
  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 main(String[] a){
  for(int i=0;i<1000;i++){
      mcc.set( "Test_"+i, "hello world_"+i);
  }
  //第一次运行讲一下三行注释,用以上面三行进行数据插入,以后运行就将上面三行注释,运行下面三行语句来看执行效果。

  System.out.println("dd1="+(String)mcc.get( "Test_967"));
  System.out.println("dd2="+(String)mcc.get( "Test_984"));
  System.out.println("dd3="+(String)mcc.get( "Test_981"));
  
  System.out.println("End"); 
 }

}

网上参考资料:
    http://bbs.linuxpk.com/thread-13497-1-1.html
    http://www.ccvita.com/257.html
    http://www.danga.com/memcached/apis.bml
    http://www.whalin.com/memcached/   

posted on 2008-10-10 11:34 分享爱的空间 阅读(2418) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(5)

随笔档案

文章档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜