public static void main(String[] args) {
if (args.length < 1) {
System.err.println("Useage:java SimpleExample [servers]");
System.exit(1);
}
MemcachedClient memcachedClient = getMemcachedClient(args[0]);
if (memcachedClient == null) {
throw new NullPointerException(
"Null MemcachedClient,please check memcached has been started");
}
try {
// add a,b,c
System.out.println("Add a,b,c");
memcachedClient.set("a", 0, "Hello,xmemcached");
memcachedClient.set("b", 0, "Hello,xmemcached");
memcachedClient.set("c", 0, "Hello,xmemcached");
// get a
String value = memcachedClient.get("a");
System.out.println("get a=" + value);
System.out.println("delete a");
// delete a
memcachedClient.delete("a");
// reget a
value = memcachedClient.get("a");
System.out.println("after delete,a=" + value);
System.out.println("Iterate all keys...");
// iterate all keys
KeyIterator it = memcachedClient.getKeyIterator(AddrUtil
.getOneAddress(args[0]));
while (it.hasNext()) {
System.out.println(it.next());
}
} catch (MemcachedException e) {
System.err.println("MemcachedClient operation fail");
e.printStackTrace();
} catch (TimeoutException e) {
System.err.println("MemcachedClient operation timeout");
e.printStackTrace();
} catch (InterruptedException e) {
// ignore
}
try {
Thread.sleep(Integer.MAX_VALUE);
memcachedClient.shutdown();
} catch (Exception e) {
System.err.println("Shutdown MemcachedClient fail");
e.printStackTrace();
}
}