0

I was using aspymemcached client to connect to my membase server. code look like :

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, "");
}

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 : http://docs.couchbase.com/developer/java-2.1/java-intro.html). In cousebase client all operation performed on JsonObject ref :

http://docs.couchbase.com/developer/java-2.0/documents-basics.html

So how can I migrate above two methods to couchbase client

Nitin
  • 2,701
  • 2
  • 30
  • 60
  • careful, the latest java client is 2.4 and the latest documentation is here: https://developer.couchbase.com/documentation/server/4.6/sdk/java/start-using-sdk.html (you've linked to 2 different versions of the docs, both outdated) – Simon Baslé Mar 22 '17 at 10:18

1 Answers1

1

if what you are storing is a serialized Object, the Java SDK offers a SerializableDocument type (see https://developer.couchbase.com/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();
Simon Baslé
  • 27,105
  • 5
  • 69
  • 70
  • I tried with `SerializableDocument` but it does not work as I am storing `Object` in server. you can see method signature for `makeMembaseCacheEntry` and it `Object` is not `Serializable`. eg : `public static void makeMembaseCacheEntry(final String key, final int expiryTime, final Object value, final String bucketName)` it gives compilation error : `The method create(String, int, ByteBuf) in the type BinaryDocument is not applicable for the arguments (String, int, Object)` – Nitin Mar 23 '17 at 05:09
  • you don't have a more concrete base type that is `Serializable` that you could use? Also, the compilation error seems to indicate you used `BinaryDocument.create`, not `SerializableDocument` – Simon Baslé Mar 23 '17 at 08:42
  • you don't have a more concrete base type that is Serializable that you could use? > I can restrict method to `Serializable` type as my API user may want to store any type of object that is why I put `Object` type in my argument. This is working fine with spymemcached java clinet – Nitin Mar 23 '17 at 09:08
  • In reality, spymemcached will also fail if the object is not serializable via Java Serialization mechanism (that's why we made it more explicit in the Couchbase SDK), see https://github.com/couchbase/spymemcached/blob/master/src/main/java/net/spy/memcached/transcoders/BaseSerializingTranscoder.java#L110 – Simon Baslé Mar 23 '17 at 09:16
  • Yes, you are correct. Now i am fetching this issue : https://forums.couchbase.com/t/runtimeexception-java-util-concurrent-timeoutexception/10973/18 – Nitin Mar 23 '17 at 09:38