Skip to content

Instantly share code, notes, and snippets.

@sideshowcoder
Created June 6, 2014 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sideshowcoder/8d3ae572bc8e0742ae64 to your computer and use it in GitHub Desktop.
Save sideshowcoder/8d3ae572bc8e0742ae64 to your computer and use it in GitHub Desktop.
Running an incr combined with getAndLock results in a value of -1 without getAndLock it works as intended. With a locked value incr behaves like the key does not exist.
import com.couchbase.client.CouchbaseClient;
import net.spy.memcached.CASValue;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
public class CouchbaseExample {
public static void main(String[] args) throws Exception {
List<URI> hosts = Arrays.asList(new URI("http://localhost:8091/pools"));
CouchbaseClient client = new CouchbaseClient(hosts, "default", "");
long beforeValue = client.incr("my-counter", 1, 0);
System.out.println("Incremented value is: " + beforeValue); // => Incremented value is: 0
CASValue<Object> result = client.getAndLock("my-counter", 1000);
long afterValue = client.incr("my-counter", 1, 0);
System.out.println("Incremented value is: " + afterValue); // => Incremented value is: -1
client.shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment