packagenet.rubyeye.xmemcached.test;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importjava.io.Serializable;importnet.rubyeye.xmemcached.XMemcachedClient;classNameimplementsSerializable {
String firstName;
String lastName;intage;intmoney;publicName(String firstName, String lastName,intage,intmoney) {super();this.firstName=firstName;this.lastName=lastName;this.age=age;this.money=money;
}publicString toString() {return"["+firstName+""+lastName+",age="+age+",money="+money+"]";
}
}publicclassExample {publicstaticvoidmain(String[] args) {try{
String ip="192.168.222.100";intport=11211;
XMemcachedClient client=newXMemcachedClient(ip, port);//存储操作if(!client.set("hello",0,"dennis")) {
System.err.println("set error");
}
client.add("hello",0,"dennis");
client.replace("hello",0,"dennis");//get操作String name=(String) client.get("hello");
System.out.println(name);//批量获取Listkeys=newArrayList();
keys.add("hello");
keys.add("test");
Mapmap=client.get(keys);
System.out.println("map size:"+map.size());//delete操作if(!client.delete("hello",1000)) {
System.err.println("delete error");
}//incr,decrclient.incr("a",4);
client.decr("a",4);//versionString version=client.version();
System.out.println(version);//增删改查自定义对象Name dennis=newName("dennis","zhuang",26,-1);
System.out.println("dennis:"+dennis);
client.set("dennis",0, dennis);
Name cachedPerson=(Name) client.get("dennis");
System.out.println("cachedPerson:"+cachedPerson);
cachedPerson.money=-10000;
client.replace("dennis",0, cachedPerson);
Name cachedPerson2=(Name) client.get("dennis");
System.out.println("cachedPerson2:"+cachedPerson2);//deleteclient.delete("dennis");
System.out.println("after delete:"+client.get("dennis"));
client.shutdown();
}catch(Exception e) {
e.printStackTrace();
}
}
}