package com.boonya.mecache;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.AddrUtil;
public class TestXMemCached {
public static void main(String[] args) {
MemcachedClientBuilder builder=new XMemcachedClientBuilder(
AddrUtil.getAddresses("192.168.20.37:11211"));
try {
MemcachedClient memCacheClient=builder.build();
try {
memCacheClient.set("key", 0, "data");
String value=memCacheClient.get("key");
System.out.println("key:"+value);
memCacheClient.delete("key");
value=memCacheClient.get("key");
System.out.println("key:"+value);
memCacheClient.shutdown();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MemcachedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4、使用建议
(1)、 memcached client for java不断更新,性能上得到提升,并且稳定。
(2)、 xmemcached使用广泛,其有如下特点:高性能、支持完整的协议、支持客户端分布、允许设置节点权重、动态增删节点、支持JMX、Spring框架和Hibernate-memcached的集成、客户端连接池、可扩展性好等。