我正在使用aspymemcached客户端连接到我的membase服务器. 代码如下:
public static MemcachedClient MemcachedClient(String bucketName){ URI server1 = new URI("192.168.100.111:8091/pools"); URI server2 = new URI("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(参考: docs.couchbase/developer/java-2.1/java-intro.html ). 在cousebase客户端中,对JsonObject ref进行的所有操作:
docs.couchbase/developer/java -2.0/documents-basics.html
那么我该如何将上述两种方法迁移到沙发床客户端
解决方案如果您存储的是序列化的Object,则Java SDK提供了SerializableDocument类型(请参见 developer.couchbase/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,在其上打开Bucket,您可以在其中执行密钥/值运算,例如get,update,insert,upsert.
在您的第一个代码段中,您仅需要至少一个榻榻米数据库节点的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中所做的那样在每次调用时重新打开.
为了制作,您需要包装value:
Document doc = SerializableDocument.create(key, expiry, value); bucket.upsert(doc);(如果要创建或替换,请使用upsert,有关其他类型的kv操作请参见文档)
要获取,您需要告诉存储桶它反序列化了一个对象:
SerializableDocument doc = bucket.get(key, SerializableDocument.class); Object value = doc.content();I was using aspymemcached client to connect to my membase server. code look like :
public static MemcachedClient MemcachedClient(String bucketName){ URI server1 = new URI("192.168.100.111:8091/pools"); URI server2 = new URI("127.0.0.1:8091/pools"); ArrayList<URI> serverList = new ArrayList<URI>(); serverList.add(server1); serverList.add(server2); return new MemcachedClient(serverList, bucketName, ""); }For putting object in cache :
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); }For getting object from cache :
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; }Now I planning to upgrade membase server to couchbase server, hence I have to use couchbase client java API (Ref : docs.couchbase/developer/java-2.1/java-intro.html). In cousebase client all operation performed on JsonObject ref :
docs.couchbase/developer/java-2.0/documents-basics.html
So how can I migrate above two methods to couchbase client
解决方案if what you are storing is a serialized Object, the Java SDK offers a SerializableDocument type (see developer.couchbase/documentation/server/4.6/sdk/java/document-operations.html#story-h2-10).
It is compatible with Object stored by the 1.x client built on top of spymemcached, but it uses flags metadata and I'm not sure how migrating from Memcached to Couchbase would influence these (so you might have to write some migrating code at some point).
Compared to Spymemcached, the Couchbase SDK 2.x has an API that is closer to the Couchbase Cluster organization: you start connecting to a Cluster, on which you open a Bucket, on which you can perform key/value operations like get, update, insert, upsert.
In your first snippet, you'll only need the IPs of at least one couchbase node. Out of that you'll get a Cluster (using CouchbaseCluster.create(...)). Then open the Bucket using cluster.openBucket(bucketName). That should be pretty much like:
//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);Note Cluster and Bucket are thread-safe and should be used as singletons rather than reopened on each call like you do in makeMembaseCacheEntry and getMembaseCacheEntry...
For make you'll need to wrap your value:
Document doc = SerializableDocument.create(key, expiry, value); bucket.upsert(doc);(use upsert if you want to create-or-replace, see the docs for other types of kv operations)
For get, you'll need to tell the bucket it deserializes an Object:
SerializableDocument doc = bucket.get(key, SerializableDocument.class); Object value = doc.content();
更多推荐
从Membase服务器到Couchbase服务器的迁移:Java客户端
发布评论