首页 > 编程语言 > 详细

java连接couchbase的memcache

时间:2015-03-01 23:42:38      阅读:1559      评论:0      收藏:0      [点我收藏+]

没有参考代码的时候重新写还是有点累的,这是重温了,第一次写的时候参考了别的一些乱七八糟的代码,虽然不是和这个一样的,不过在不懂的情况下还是很快写出来了,反而是这次光是找需要哪些jar都搞了半天,最后发现extern的用一个jar就够了,addresses和auth之前也是糊里糊涂的,这次总算搞清楚了

maven引入

<dependency>
    <groupId>com.googlecode.xmemcached</groupId>
    <artifactId>xmemcached</artifactId>
    <version>1.4.3</version>
 </dependency>

先按照在前面的一篇《记一次couchbase(memcached)安装以及使用》的步骤安装好,类型选择couchbase就行了,
测试代码如下:

package Test.test;

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.auth.AuthInfo;
import net.rubyeye.xmemcached.command.BinaryCommandFactory;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.AddrUtil;
public class App 
{
    public static void main( String[] args )
    {
        try {
            MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("192.168.92.128:11211")) ;
            builder.addAuthInfo(AddrUtil.getOneAddress("192.168.92.128:11211"), AuthInfo.plain("azul", "xxxx"));
            builder.setCommandFactory(new BinaryCommandFactory());
            builder.setFailureMode(true);
            builder.setConnectionPoolSize(10); 
            MemcachedClient client = builder.build();
            client.set("lwk", 0, "hello");
            String value = client.get("key");
            System.out.println(value);
            client.shutdown();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (MemcachedException e) {
            e.printStackTrace();
        }
        System.out.println( "end..." );
    }
}

执行结果:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
hello
end...

管理界面上可以看到:
技术分享

它是密文存储的

技术分享

如果不用sasl验证的话先把服务器上的验证去掉然后这样写

package Test.test;

import net.spy.memcached.AddrUtil;
import net.spy.memcached.MemcachedClient;

public class App 
{
    public static void main( String[] args )
    {
        try {
            MemcachedClient client = new MemcachedClient(AddrUtil.getAddresses("192.168.92.128:11211"));
            client.set("lwk", 0, "hello");
            Object value = client.get("lwk");
            System.out.println(value);
            client.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println( "end..." );
    }
}

结果:

2015-03-01 23:26:38.268 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=/192.168.92.128:11202, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2015-03-01 23:26:38.271 INFO net.spy.memcached.MemcachedConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@be7667
2015-03-01 23:26:38.276 WARN net.spy.memcached.MemcachedConnection:  Could not redistribute to another node, retrying primary node for lwk.
hello
end...
2015-03-01 23:26:38.284 INFO net.spy.memcached.MemcachedConnection:  Shut down memcached client

这里是通过java client访问,一会儿用命令行试一下。

 

java连接couchbase的memcache

原文:http://www.cnblogs.com/azul0906/p/4307742.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!