您的位置:首页 > 数据库 > Memcache

java连接开启了sasl的memcache服务

2015-04-24 09:47 1111 查看
java连接开启了sasl的memcache服务
public class Xmemcache {

public static void main(String[] args) {
// TODO Auto-generated method stub
final String server = "192.168.7.253:49156";
final String authinfo  = "admin:oKSwWKJkbCZ2";
XMemcachedClientBuilder builder = new XMemcachedClientBuilder(server);
// 配置读取连接池大小,缺省为1
builder.setConnectionPoolSize(1);
Map<InetSocketAddress, AuthInfo> authInfoMap = new HashMap<InetSocketAddress, AuthInfo>();
authInfoMap.put(new InetSocketAddress("192.168.7.253", 49156), AuthInfo.plain("admin", "oKSwWKJkbCZ2"));
builder.setAuthInfoMap(authInfoMap);
// 添加协议工厂,启用SASL使用binary协议
CommandFactory factory = new BinaryCommandFactory();
builder.setCommandFactory(factory);
// 分布策略:一致性哈希KetamaMemcachedSessionLocator,ArraySessionLocator
MemcachedSessionLocator locator = new ArrayMemcachedSessionLocator();
builder.setSessionLocator(locator);
// 序列化转换器
Transcoder transcoder = new SerializingTranscoder();
builder.setTranscoder(transcoder);
BufferAllocator bufferAllocator = new SimpleBufferAllocator();
builder.setBufferAllocator(bufferAllocator);
builder.setFailureMode(true);
try {
MemcachedClient memcachedClient = builder.build();
System.out.println(memcachedClient.add("key", 1000, "sasl-memcache"));
System.out.println(memcachedClient.get("key"));
} catch (IOException | TimeoutException | InterruptedException | MemcachedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  memcache java sasl