1. MemCachedClientUtil.java
package cn.xiyou.utils;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class MemCachedClientUtil {
private static MemCachedClient memCachedClient;
private static String[] servers = { "127.0.0.1:11211" };
static {
memCachedClient = new MemCachedClient();
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(servers);
pool.setWeights(new Integer[] { 3 });
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(200);
pool.setMaxIdle(1000 * 30 * 30);
pool.setMaintSleep(30);
pool.setNagle(false);
pool.setSocketConnectTO(30);
pool.initialize();
}
public boolean add(String key, Object value) {
return memCachedClient.add(key, value);
}
public boolean delete(String key) {
return memCachedClient.delete(key);
}
public boolean set(String key, Object value) {
return memCachedClient.set(key, value);
}
public boolean replace(String key, Object value) {
return memCachedClient.replace(key, value);
}
public Object get(String key) {
return memCachedClient.get(key);
}
}
2. TestMemcached.java
package cn.xiyou.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.xiyou.entity.User;
import cn.xiyou.utils.MemCachedClientUtil;
public class TestMemcached {
private static MemCachedClientUtil mcc = new MemCachedClientUtil();
public static void main(String[] args) {
User u = new User("xiao", 18, '男');
mcc.add("u", u);
System.out.println(mcc.get("u"));
User u2 = new User("cai", 20, '女');
List<User> list = new ArrayList<User>();
list.add(u2);
mcc.add("list", list);
System.out.println(mcc.get("list"));
User u3 = new User("cai", 20, '女');
Map<String, User> map = new HashMap<String, User>();
map.put("m", u3);
mcc.add("map", map);
System.out.println(mcc.get("map"));
User u4 = new User("cai", 20, '女');
User u5 = new User("xiao", 18, '男');
List<User> list2 = new ArrayList<User>();
list2.add(u4);
list2.add(u5);
Map<String, List<User>> map2 = new HashMap<String, List<User>>();
map2.put("l", list2);
mcc.add("map2", map2);
System.out.println(mcc.get("map2"));
}
}