手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Membase和Java入门

[日期:2011-08-16] 来源:Linux社区  作者:njchenyi [字体:大 中 小]

Membase API 概述

Membase客户端库有很多API方法,你可以用他们实现你的分布式内存魔力。www.linuxidc.com下面将MemcachedClient方法分为几个类别,为你以后的工作提供一个快速的参考。

表1. 同步方法(Synchronous Methods)

decr 减少键并返回值Decrement a key and return the value.
get 从缓存中获得一个特定值Gets a particular value from the cache.
getBulk 同时获得多个值Gets many values at the same time.
gets 获得一个值以及检查和设置支持Gets a particular value with Check And Set support.
incr 增加一个键的值Increment the value of a key.

表 2. 同步检查和设置(Synchronous Check And Set)

cas 执行检查和设置操作Perform a Check And Set operation.

表3. 异步方法(Asynchronous Methods)

add 在缓存中增加一个对象,如果taAdds an object to the cache if it does not exist already.
delete 从缓存中删除一个值Deletes a value from the cache.
flush 将所有服务器上的缓存清空Clears the cache on all servers.

表4.异步检查和设置( Asynchronous Check And Set)

append Append to an existing value in the cache.
asyncCAS Check and set values of particular keys.
asyncDecr Decrement a value.
asyncGet Get a particular value.
asyncGetBulk Get many values at the same time.
asyncGets Get a value with CAS support.
asyncIncr Increment a value.

表5. 状态方法(Status Methods)

addObserver Adds an observer to watch the connection status.
getAvailableServers Returns a list of, shocker, available servers.
getNodeLocator Returns a read only instance of the node locator.
getStats Returns connection statistics.
getTranscoder Returns the default transcoder instance.
getUnavailableServers Returns a list of the servers that are not available.
getVersions Returns the versions of all connected servers.

一个改动较大的程序A More Substantial Program

如果你对改动较大的程序有兴趣,请下载样例代码和Eclipse工程,这样你就可以在Eclipse开发环境中运行了。这个程序会更具用户配置创建多个线程,每个线程都会存入(或者读取)100个随机数。每个线程都会创建一个MemcachedClient对象实例,然后执行gets()操作查找指定的keys。如果键没有被设置,gets()操作就会返回null。这种情况下线程自己会创建值并且设置到Membase中,这样会花费100毫秒去执行。这种情况模拟了一个高昂的数据库操作。你可以在文章末尾找到这个简单程序的完整源代码。

让我们讨论这个程序中的一些细节,这样你可以理解连接Membase服务器,测试已有键值对,设置一个键值的一些基础知识。这些操作将会让你知道如何开始。

清单2. 连接到一系列的Membase服务器:

65:                 MemcachedClient client = new MemcachedClient(
 66:                         AddrUtil.getAddresses(addresses));

从这几行你可以看到你需要创建一个MemcachedClient实例。有很多的方法去创建,不过我认为涉及AddrUtil 类的构造器很重要,这个类可以解析用逗号或者空格分隔的服务器地址和端口列表,例如下面所列:

host-or-ip:port host-or-ip:port

你所连接的端口将会成为MOXI端口,11211是一个有效地代理,它知道集群中的其他所有服务器并且提供快速协议接入。所以这种集群情况下,按照下面格式提供一个地址字符串会很好的进行工作:

String addresses = "10.0.0.33:11211 10.0.0.57:11211";

清单 3 is an abridged excerpt that shows the creation of an IntegerTranscoder, which is a useful class for converting objects in Membase back to integers when needed. This is for convenience and reduces type casting. You can then see a line 82 that a the gets() method is called. This returns a CASValue<T> of type integer which is useful for checking and setting a value. If the value is null it means that membase hasn't been given a value for this key. The code then sets a value. Otherwise, we can get its value and do something with it.

清单 3. 检查和设置操作

67:                 IntegerTranscoder intTranscoder = new IntegerTranscoder();
 68:

 82:                     CASValue<Integer> value = client.gets(key,
 83:                             intTranscoder);
 84:
 85:                     if (value == null) {
 86:                         // The value doesn't exist in Membase
 87:                         client.set(key, 15, rand.nextInt(), intTranscoder);
 88:
 89:                         // Simulate the value taking time to create.
 90:                         Thread.sleep(100);
 91:
 92:                         created++;
 93:
 94:                     } else {
 95:
 99:                         int v = value.getValue();
100:
107:                     }

Setting values in Membase are done asynchronously, and the application does not have to wait for these to be completed. Sometimes, though, you may want to ensure that Membase has been sent some values, and you can do this by calling client.waitForQueues() and giving it a timeout for waiting for this to occur, as shown in Listing 4.

清单4. 等待数据设置进Membase。

109:                     client.waitForQueues(1, TimeUnit.MINUTES);
linux
相关资讯       Java  Membase  
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款