代码如下:
package com.zyy; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; public class MemcachedTest { private static MemCachedClient mcc = new MemCachedClient(); public static MemCachedClient getInstance() { return mcc; } // 设置与缓存服务器的连接池 static { // 服务器列表和其权重 String[] servers = { "127.0.0.1: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 * 60 * 6); // 设置主线程的睡眠时间 pool.setMaintSleep(30); // 设置TCP的参数,连接超时等 pool.setNagle(false); pool.setSocketTO(3000); pool.setSocketConnectTO(0); // 初始化连接池 pool.initialize(); } public static void main(String[] args) { MemCachedClient mcc = MemcachedTest.getInstance(); mcc.add("hi", "hello world!"); System.out.println(mcc.get("hi")); mcc.replace("hi", "张阳阳"); System.out.println(mcc.get("hi")); } }