xmemcached特性
1、网络采用nio实现
2、支持完整的memcached文本协议,二进制协议
3、支持memcached分布:余数分布和一致性哈希分布。
4、支持memcached节点的动态增减,还可以设置主从节点。
5、 允许设置节点权重,设置的权重越高,该memcached节点存储的数据将越多
6、支持JMX。XMemcached通过JMX暴露的一些接口,支持client本身的监控和调整,允许动态设置调优参数、查看统计数据、动态增删节点等。
7、与Spring框架和Hibernate-memcached的集成
8、 客户端连接池。刚才已经提到javanio通常对一个memcached节点使用一个连接,而XMemcached同样提供了设置连接池的功能
9、 可扩展性。XMemcached是基于javanio框架yanf4j实现的,因此在实现上结构相对清楚,分层比较明晰
1.import static junit.framework.Assert.*;
2.
3.import java.io.IOException;
4.import java.util.concurrent.TimeoutException;
5.
6.import net.rubyeye.xmemcached.MemcachedClient;
7.import net.rubyeye.xmemcached.MemcachedClientBuilder;
8.import net.rubyeye.xmemcached.XMemcachedClientBuilder;
9.import net.rubyeye.xmemcached.command.BinaryCommandFactory;
10.import net.rubyeye.xmemcached.exception.MemcachedException;
11.import net.rubyeye.xmemcached.utils.AddrUtil;
12.
13.import org.junit.Test;
14.
15.public class MemcachedClientTest {
16. @Test
17. public void test() {
18. MemcachedClientBuilder builder = new XMemcachedClientBuilder(
19. AddrUtil.getAddresses("10.11.155.26:11211 10.11.155.41:11211 10.10.76.31:11211 10.10.76.35:11211"),
20. new int[] { 1, 1, 1, 1 });
21.
22. // 设置连接池大小,即客户端个数
23. builder.setConnectionPoolSize(50);
24.
25. // 宕机报警
26. builder.setFailureMode(true);
27.
28. // 使用二进制文件
29. builder.setCommandFactory(new BinaryCommandFactory());
30.
31. MemcachedClient memcachedClient = null;
32. try {
33. memcachedClient = builder.build();
34. try {
35. // 设置/获取
36. memcachedClient.set("zlex", 36000, "set/get");
37. assertEquals("set/get", memcachedClient.get("zlex"));
38.
39. // 替换
40. memcachedClient.replace("zlex", 36000, "replace");
41. assertEquals("replace", memcachedClient.get("zlex"));
42.
43. // 移除
44. memcachedClient.delete("zlex");
45. assertNull(memcachedClient.get("zlex"));
46. } catch (TimeoutException e) {
47. // TODO Auto-generated catch block
48. e.printStackTrace();
49. } catch (InterruptedException e) {
50. // TODO Auto-generated catch block
51. e.printStackTrace();
52. } catch (MemcachedException e) {
53. // TODO Auto-generated catch block
54. e.printStackTrace();
55. }
56.
57. } catch (IOException e) {
58. // TODO Auto-generated catch block
59. e.printStackTrace();
60. } finally {
61. if (memcachedClient != null) {
62. try {
63. memcachedClient.shutdown();
64. } catch (IOException e) {
65. // TODO Auto-generated catch block
66. e.printStackTrace();
67. }
68. }
69. }
70. }
71.}