日期:2014-05-16  浏览次数:20422 次

Membase和Java入门2

客户端


与Membase的交互类似与Memcached的交互。我们将会使用SpyMemcached Java 客户端。请从?这儿下载。

代码

?


本项目中所有用到的代码都提供在GitHub :?https://github.com/sujee/membase-tutorial
这一个elipse工程,并且已经可以运行。

?

?

package org.shine.membase.test;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

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

public class Main {

    public static void main(String[] args) {

        MemcachedClient client;

        try {
            client = new MemcachedClient(AddrUtil.getAddresses("192.168.2.17:11211"));
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        Object spoon = client.get("spoon");
        User u = (User)client.get("membase");
        String username = "";
        if(u != null) {
        	username = u.getUsername();
        }
        if (username == "") {
            System.out.println("There is no spoon.");
            client.add("spoon", 10, "Hello World!");
            User user = new User();
            user.setUsername("membase");
            user.setPassword("123456");
            client.add("membase", 11, user);
           
        } else {
        	System.out.println((String)spoon + username);
            client.delete("spoon");
            client.delete("membase");
        }

        client.waitForQueues(1, TimeUnit.MINUTES);

        System.exit(0);
    }

}