我正在使用 aspymemcached 客户端连接到我的 membase 服务器。 代码如下:
public static MemcachedClient MemcachedClient(String bucketName){
URI server1 = new URI("http://192.168.100.111:8091/pools");
URI server2 = new URI("http://127.0.0.1:8091/pools");
ArrayList<URI> serverList = new ArrayList<URI>();
serverList.add(server1);
serverList.add(server2);
return new MemcachedClient(serverList, bucketName, "");
}
将对象放入缓存:
public static void makeMembaseCacheEntry(final String key, final int expiryTime, final Object value, final String bucketName) {
MemcachedClient client = getMembaseClient(bucketName);
if (client != null) {
client.set(key, expiryTime, value);
}
从缓存中获取对象:
public static Object getMembaseCacheEntry(String key) {
Object value = null;
try {
MemcachedClient client = getMembaseClient(bucketName);
if (client != null) {
value = client.get(key);
}
} catch (Exception e) {
}
return value;
}
现在我计划将membase服务器升级到couchbase服务器,因此我必须使用couchbase客户端java API(引用:http://docs.couchbase.com/developer/java-2.1/java-intro.html)。 在 cousebase 客户端中,所有操作均在 JsonObject ref 上执行:
http://docs.couchbase.com/developer/java-2.0/documents-basics.html
那么如何将以上两种方法迁移到 couchbase 客户端
最佳答案
如果您要存储的是序列化的Object
,Java SDK 会提供SerializedDocument
类型(请参阅 https://developer.couchbase.com/documentation/server/4.6/sdk/java/document-operations.html#story-h2-10)。
它与基于spymemcached构建的1.x客户端存储的对象兼容,但它使用标志元数据,我不确定从Memcached迁移到Couchbase会如何影响这些(所以你可能需要编写一些迁移代码在某个时刻)。
与 Spymemcached 相比,Couchbase SDK 2.x 的 API 更接近 Couchbase Cluster 组织:您开始连接到 Cluster
,在该 Cluster 上打开一个 Bucket
code>,您可以在其上执行键/值操作,例如 get
、update
、insert
、upsert
。
在您的第一个代码段中,您只需要至少一个 couchbase 节点的 IP。由此,您将获得一个Cluster
(使用CouchbaseCluster.create(...)
)。
然后使用 cluster.openBucket(bucketName)
打开 Bucket
。这应该很像:
//TODO make both of these singletons
Cluster cluster = CouchbaseCluster.create("192.168.100.111", "127.0.0.1:8091");
Bucket bucket = cluster.openBucket(bucketName, bucketPassword);
注意Cluster
和Bucket
是线程安全的,应该用作单例,而不是像在中那样在每次调用时重新打开>makeMembaseCacheEntry
和 getMembaseCacheEntry
...
对于 make,您需要包装您的值
:
Document doc = SerializableDocument.create(key, expiry, value);
bucket.upsert(doc);
(如果要创建或替换,请使用 upsert
,请参阅其他类型的 kv 操作的文档)
对于 get,您需要告诉存储桶它反序列化一个对象:
SerializableDocument doc = bucket.get(key, SerializableDocument.class);
Object value = doc.content();
关于java - membase 服务器到 couchbase 服务器迁移 : Java client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42918883/