一、spymemcached的使用
添加依赖
<!-- memcache客户端 spymemcached--> <dependency> <groupId>net.spy</groupId> <artifactId>spymemcached</artifactId> <version>2.12.3</version> </dependency>
创建memcached客户端,多个配置用空格隔开
MemcachedClient client = new MemcachedClient(AddrUtil.getAddresses("127.0.0.1:11220 127.0.0.1:11221"));
二、xmemcached的使用
添加依赖
<!-- memcache客户端 xmemcached--> <dependency> <groupId>com.googlecode.xmemcached</groupId> <artifactId>xmemcached</artifactId> <version>2.4.6</version> </dependency>
创建memcached客户端
MemcachedClientBuilder builder = new XMemcachedClientBuilder( AddrUtil.getAddresses("127.0.0.1:11220 127.0.0.1:11221")); // AddrUtil.getAddresses("server1:11211 server2:11211") // 宕机报警 builder.setFailureMode(true); // 使用二进制文件 builder.setCommandFactory(new BinaryCommandFactory()); /** * 设置连接池大小,即客户端个数 */ builder.setConnectionPoolSize(10); MemcachedClient client = builder.build();