In addition, the spymemcached library also
supports a range of asynchronous methods that can be used to
store, update and retrieve values without having to explicitly
wait for a response.
The asynchronous methods use a Java standard Future object which is returned by the initial method call for the operation. The communication with the Membase server will continue in the background so that the main program loop can continue. You can recover the status of the operation by using a method to check the status on the returned Future object. For example, rather than synchronously getting a key, an asynchronous call might look like this:
Future<Object> future = membase.asyncGet("someKey");
This will populate the Future object future
with the response from the server. The Future object class is
defined
here.
The primary methods are:
cancel()
Cancels the operation, if the operation has not already been completed.
get()
Gets the object returned by the operation if the method was synchronous rather than asynchronous.
get(timeout, TimeUnit)
Gets the object waiting for a maximum time specified by
timeout and the corresponding
TimeUnit.
isDone()
The operation has been completed successfully.
For example, you can use the timeout method to obtain the value or cancel the operation:
Future<Object> future = membase.asyncGet("someKey"); Object myObj; try { myObj = future.get(5, TimeUnit.SECONDS); } catch(TimeoutException e) { future.cancel(false); }
Alternatively, you can wait for a response by using the
get() method:
Object myObj; myObj = future.get();