Java---Memcache测试Dome

下载 jar包



public class MClient {
    MemCachedClient mc;
    @Before
    public void init(){

        // 服务器列表和其权重
        String[] servers = {"192.168.17.99:11211"};
        Integer[] weights = {3};//多台服务器时的相应权重

        // 获取socke连接池的实例对象
        SockIOPool pool = SockIOPool.getInstance();

        // 设置服务器信息
        pool.setServers( servers );
        pool.setWeights( weights );

        // 设置初始连接数、最小和最大连接数以及最大处理时间
        pool.setInitConn( 5 );
        pool.setMinConn( 5 );
        pool.setMaxConn( 250 );
        pool.setMaxIdle( (1000 * 60 * 60L) );


        pool.setMaintSleep( 30 ); // 设置主线程的睡眠时间,每30秒苏醒一次,维持连接池大小

        // 设置TCP的参数,连接超时等
        pool.setNagle( false ); //关闭套接字缓存
        pool.setSocketTO( 3000 ); //连接建立后的超时时间
        pool.setSocketConnectTO(0);//连接建立时的超时时间

        /*
        *  如果失效的服务器 恢复运行,客户端会返回到原来连接的服务器
        *  如果你不想用这个功能,设置下面的参数
        *  pool.setFailover(false);
            pool.setFailback(false);
        *
        * */
        // 初始化连接池
        pool.initialize();

        mc = new MemCachedClient();
        mc.setPrimitiveAsString( true );    //不序列表,字符串存储

    }
    @Test
    public void testAddCache(){
        for(int i = 0; i  < 10; i++){
            mc.set("id" + i, i);
        }
    }

    @Test
    public void getFromCache(){
        for(int i = 0; i  < 10; i++){
            System.out.println(mc.get("id" + i));;
        }
    }


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值