Skip to content

Instantly share code, notes, and snippets.

@ingenthr
Created February 8, 2012 18:18
Show Gist options
  • Select an option

  • Save ingenthr/1771900 to your computer and use it in GitHub Desktop.

Select an option

Save ingenthr/1771900 to your computer and use it in GitHub Desktop.
Showing the basics of CAS
package com.couchbase.sample.showingcas;
import com.couchbase.client.CouchbaseClient;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import net.spy.memcached.CASResponse;
import net.spy.memcached.CASValue;
import net.spy.memcached.ops.OperationQueueFactory;
public class App
{
public static void main( String[] args ) throws URISyntaxException, IOException
{
List<URI> baselist = new ArrayList<URI>();
baselist.add(new URI("http://127.0.0.1:8091/pools"));
OperationQueueFactory afactory;
CouchbaseClient cb = new CouchbaseClient(baselist, "default", "");
cb.set("hello", 0, "world");
System.err.println(cb.get("hello"));
cb.get("hello");
CASValue<Object> s = cb.gets("hello");
// my programming logic would then see if I want to modify the object
// by doing something with s.getValue(). deciding I do...
System.err.println("***> Show updating a document with cas");
CASResponse rv = cb.cas("hello", s.getCas(), "{\"hi\":\"there\"}");
if (rv != CASResponse.OK) {
// it can either be EXISTS or NOT_FOUND. Do something logical.
System.err.println("Failed document update owing to " + rv);
} else {
System.err.println("Updated document successfully.");
}
System.err.println("***> Show a fake failure, with a bad CAS");
CASResponse frv = cb.cas("hello", 888, "{\"hi\":\"there\"}");
if (frv != CASResponse.OK) {
System.err.println("Failed document update owing to " + frv);
} else {
System.err.println("Updated document successfully.");
}
cb.shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment