membase 客户端测试

/*
 * 创建日期 2011-4-28
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package com.mem.test;


import java.io.File;
import java.io.FileInputStream;
import java.util.Timer;
import java.util.TimerTask;

import com.danga.MemCached.MemCachedClient; 
import com.danga.MemCached.SockIOPool; 

public class Memclient {
       

    /**
     * 存入数据
     *
     */
    public void put(String key){
        try {
            MemCachedClient mc = new MemCachedClient(); 
            mc.setCompressEnable(true); 
            mc.setCompressThreshold(4096);
           
            File file = new File("d://test.htm");
            FileInputStream fis = new FileInputStream(file);
           
            byte[] content = new byte[1024*100];
            byte[] buffer = new byte[1024];
           
            int k = 0;
            int total = 0;
           
            while ((k=fis.read(buffer))>0) {
                System.arraycopy(buffer,0,content,total,k);
                total +=k;
            }
           
            fis.close();
           
            String str = new String(content);
           
            mc.set(key, str);
        } catch (Exception e) {
            e.printStackTrace();
        }
       
    }
   
   
    /**
     * get 测试
     *
     */
    public void timerGet(){
        try {           
            Timer timer = new Timer();
            timer.schedule(new MyTimeTask(),0,100);
           
        } catch (Exception e) {
            e.printStackTrace();
        }
       
    }
   

    class MyTimeTask extends TimerTask{

        public void run() {
            MemCachedClient mc = new MemCachedClient(); 
            System.out.println(mc.get("key1")); 
            System.out.println(mc.get("key2")); 
           
        }
       
       
    }
   
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
 
        SockIOPool pool = SockIOPool.getInstance(); 
        pool.setServers( new String[]{"172.25.1.128:11211"} ); 
        pool.initialize(); 
 
       
        Memclient mct = new Memclient();
        //mct.put("key1");
       
        MemCachedClient mc = new MemCachedClient(); 
        mc.setCompressEnable(true); 
        mc.setCompressThreshold(4096);
        //mc.set("key2","test is ok!");
         
        //for(int i=0;i<10000;i++) 
        //    mc.add("name"+i, "Winter Lau");
        //mc.add("key1","test is ok!");
         
        mct.timerGet();
    } 

}

 

 

/usr/local/memcached/bin/memcached -d -m 100 -u root -l 172.25.1.128 -p 11211 -c 256 -P /tmp/memcached.pid

posted @ 2011-04-28 18:28  hongweigg  阅读(1)  评论(0编辑  收藏  举报