Java---Memcache测试Dome

来源:互联网 发布:软件团队名称大全 编辑:程序博客网 时间:2024/06/05 06:00

下载 jar包
https://github.com/gwhalin/Memcached-Java-Client/downloads



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