net.spy.memcached
Class MemcachedClient

java.lang.Object
  extended by java.lang.Thread
      extended by net.spy.SpyThread
          extended by net.spy.memcached.MemcachedClient
All Implemented Interfaces:
java.lang.Runnable, MemcachedClientIF

public final class MemcachedClient
extends net.spy.SpyThread
implements MemcachedClientIF

Client to a memcached server.

Basic usage

  MemcachedClient c=new MemcachedClient(
      new InetSocketAddress("hostname", portNum));

  // Store a value (async) for one hour
  c.set("someKey", 3600, someObject);
  // Retrieve a value.
  Object myObject=c.get("someKey");
  

Advanced Usage

MemcachedClient may be processing a great deal of asynchronous messages or possibly dealing with an unreachable memcached, which may delay processing. If a memcached is disabled, for example, MemcachedConnection will continue to attempt to reconnect and replay pending operations until it comes back up. To prevent this from causing your application to hang, you can use one of the asynchronous mechanisms to time out a request and cancel the operation to the server.

  // Get a memcached client connected to several servers
  MemcachedClient c=new MemcachedClient(
      AddrUtil.getAddresses("server1:11211 server2:11211"));

  // Try to get a value, for up to 5 seconds, and cancel if it doesn't return
  Object myObj=null;
  Future<Object> f=c.asyncGet("someKey");
  try {
      myObj=f.get(5, TimeUnit.SECONDS);
  } catch(TimeoutException e) {
      // Since we don't need this, go ahead and cancel the operation.  This
      // is not strictly necessary, but it'll save some work on the server.
      f.cancel();
      // Do other timeout related stuff
  }
 


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Fields inherited from interface net.spy.memcached.MemcachedClientIF
MAX_KEY_LENGTH
 
Constructor Summary
MemcachedClient(ConnectionFactory cf, java.util.List<java.net.InetSocketAddress> addrs)
          Get a memcache client over the specified memcached locations.
MemcachedClient(java.net.InetSocketAddress... ia)
          Get a memcache client operating on the specified memcached locations.
MemcachedClient(java.util.List<java.net.InetSocketAddress> addrs)
          Get a memcache client over the specified memcached locations.
 
Method Summary
 java.util.concurrent.Future<java.lang.Boolean> add(java.lang.String key, int exp, java.lang.Object o)
          Add an object to the cache (using the default transcoder) iff it does not exist already.
<T> java.util.concurrent.Future<java.lang.Boolean>
add(java.lang.String key, int exp, T o, Transcoder<T> tc)
          Add an object to the cache iff it does not exist already.
 java.util.concurrent.Future<java.lang.Boolean> append(long cas, java.lang.String key, java.lang.Object val)
          Append to an existing value in the cache.
<T> java.util.concurrent.Future<java.lang.Boolean>
append(long cas, java.lang.String key, T val, Transcoder<T> tc)
          Append to an existing value in the cache.
 java.util.concurrent.Future<CASResponse> asyncCAS(java.lang.String key, long casId, java.lang.Object value)
          Asynchronous CAS operation using the default transcoder.
<T> java.util.concurrent.Future<CASResponse>
asyncCAS(java.lang.String key, long casId, T value, Transcoder<T> tc)
          Asynchronous CAS operation.
 java.util.concurrent.Future<java.lang.Long> asyncDecr(java.lang.String key, int by)
          Asynchronous decrement.
 java.util.concurrent.Future<java.lang.Object> asyncGet(java.lang.String key)
          Get the given key asynchronously and decode with the default transcoder.
<T> java.util.concurrent.Future<T>
asyncGet(java.lang.String key, Transcoder<T> tc)
          Get the given key asynchronously.
 java.util.concurrent.Future<java.util.Map<java.lang.String,java.lang.Object>> asyncGetBulk(java.util.Collection<java.lang.String> keys)
          Asynchronously get a bunch of objects from the cache and decode them with the given transcoder.
<T> java.util.concurrent.Future<java.util.Map<java.lang.String,T>>
asyncGetBulk(java.util.Collection<java.lang.String> keys, Transcoder<T> tc)
          Asynchronously get a bunch of objects from the cache.
 java.util.concurrent.Future<java.util.Map<java.lang.String,java.lang.Object>> asyncGetBulk(java.lang.String... keys)
          Varargs wrapper for asynchronous bulk gets with the default transcoder.
<T> java.util.concurrent.Future<java.util.Map<java.lang.String,T>>
asyncGetBulk(Transcoder<T> tc, java.lang.String... keys)
          Varargs wrapper for asynchronous bulk gets.
 java.util.concurrent.Future<CASValue<java.lang.Object>> asyncGets(java.lang.String key)
          Gets (with CAS support) the given key asynchronously and decode using the default transcoder.
<T> java.util.concurrent.Future<CASValue<T>>
asyncGets(java.lang.String key, Transcoder<T> tc)
          Gets (with CAS support) the given key asynchronously.
 java.util.concurrent.Future<java.lang.Long> asyncIncr(java.lang.String key, int by)
          Asychronous increment.
 CASResponse cas(java.lang.String key, long casId, java.lang.Object value)
          Perform a synchronous CAS operation with the default transcoder.
<T> CASResponse
cas(java.lang.String key, long casId, T value, Transcoder<T> tc)
          Perform a synchronous CAS operation.
 long decr(java.lang.String key, int by)
          Decrement the given key by the given value.
 long decr(java.lang.String key, int by, long def)
          Decrement the given counter, returning the new value.
 java.util.concurrent.Future<java.lang.Boolean> delete(java.lang.String key)
          Delete the given key from the cache.
 java.util.concurrent.Future<java.lang.Boolean> delete(java.lang.String key, int hold)
          Deprecated. Hold values are no longer honored.
 java.util.concurrent.Future<java.lang.Boolean> flush()
          Flush all caches from all servers immediately.
 java.util.concurrent.Future<java.lang.Boolean> flush(int delay)
          Flush all caches from all servers with a delay of application.
 java.lang.Object get(java.lang.String key)
          Get with a single key and decode using the default transcoder.
<T> T
get(java.lang.String key, Transcoder<T> tc)
          Get with a single key.
 java.util.Collection<java.net.SocketAddress> getAvailableServers()
          Get the addresses of available servers.
 java.util.Map<java.lang.String,java.lang.Object> getBulk(java.util.Collection<java.lang.String> keys)
          Get the values for multiple keys from the cache.
<T> java.util.Map<java.lang.String,T>
getBulk(java.util.Collection<java.lang.String> keys, Transcoder<T> tc)
          Get the values for multiple keys from the cache.
 java.util.Map<java.lang.String,java.lang.Object> getBulk(java.lang.String... keys)
          Get the values for multiple keys from the cache.
<T> java.util.Map<java.lang.String,T>
getBulk(Transcoder<T> tc, java.lang.String... keys)
          Get the values for multiple keys from the cache.
 CASValue<java.lang.Object> gets(java.lang.String key)
          Gets (with CAS support) with a single key using the default transcoder.
<T> CASValue<T>
gets(java.lang.String key, Transcoder<T> tc)
          Gets (with CAS support) with a single key.
 java.util.Map<java.net.SocketAddress,java.util.Map<java.lang.String,java.lang.String>> getStats()
          Get all of the stats from all of the connections.
 java.util.Map<java.net.SocketAddress,java.util.Map<java.lang.String,java.lang.String>> getStats(java.lang.String arg)
          Get a set of stats from all connections.
 Transcoder<java.lang.Object> getTranscoder()
          Get the default transcoder that's in use.
 java.util.Collection<java.net.SocketAddress> getUnavailableServers()
          Get the addresses of unavailable servers.
 java.util.Map<java.net.SocketAddress,java.lang.String> getVersions()
          Get the versions of all of the connected memcacheds.
 long incr(java.lang.String key, int by)
          Increment the given key by the given amount.
 long incr(java.lang.String key, int by, long def)
          Increment the given counter, returning the new value.
 java.util.concurrent.Future<java.lang.Boolean> prepend(long cas, java.lang.String key, java.lang.Object val)
          Prepend to an existing value in the cache.
<T> java.util.concurrent.Future<java.lang.Boolean>
prepend(long cas, java.lang.String key, T val, Transcoder<T> tc)
          Prepend to an existing value in the cache.
 java.util.concurrent.Future<java.lang.Boolean> replace(java.lang.String key, int exp, java.lang.Object o)
          Replace an object with the given value (transcoded with the default transcoder) iff there is already a value for the given key.
<T> java.util.concurrent.Future<java.lang.Boolean>
replace(java.lang.String key, int exp, T o, Transcoder<T> tc)
          Replace an object with the given value iff there is already a value for the given key.
 void run()
          Infinitely loop processing IO.
 java.util.concurrent.Future<java.lang.Boolean> set(java.lang.String key, int exp, java.lang.Object o)
          Set an object in the cache (using the default transcoder) regardless of any existing value.
<T> java.util.concurrent.Future<java.lang.Boolean>
set(java.lang.String key, int exp, T o, Transcoder<T> tc)
          Set an object in the cache regardless of any existing value.
 void setTranscoder(Transcoder<java.lang.Object> tc)
          Set the default transcoder for managing the cache representations of objects going in and out of the cache.
 void shutdown()
          Shut down immediately.
 boolean shutdown(long timeout, java.util.concurrent.TimeUnit unit)
          Shut down this client gracefully.
 boolean waitForQueues(long timeout, java.util.concurrent.TimeUnit unit)
          Wait for the queues to die down.
 
Methods inherited from class net.spy.SpyThread
getLogger
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MemcachedClient

public MemcachedClient(java.net.InetSocketAddress... ia)
                throws java.io.IOException
Get a memcache client operating on the specified memcached locations.

Parameters:
ia - the memcached locations
Throws:
java.io.IOException - if connections cannot be established

MemcachedClient

public MemcachedClient(java.util.List<java.net.InetSocketAddress> addrs)
                throws java.io.IOException
Get a memcache client over the specified memcached locations.

Parameters:
addrs - the socket addrs
Throws:
java.io.IOException - if connections cannot be established

MemcachedClient

public MemcachedClient(ConnectionFactory cf,
                       java.util.List<java.net.InetSocketAddress> addrs)
                throws java.io.IOException
Get a memcache client over the specified memcached locations.

Parameters:
bufSize - read buffer size per connection (in bytes)
addrs - the socket addresses
Throws:
java.io.IOException - if connections cannot be established
Method Detail

getAvailableServers

public java.util.Collection<java.net.SocketAddress> getAvailableServers()
Get the addresses of available servers.

This is based on a snapshot in time so shouldn't be considered completely accurate, but is a useful for getting a feel for what's working and what's not working.

Specified by:
getAvailableServers in interface MemcachedClientIF

getUnavailableServers

public java.util.Collection<java.net.SocketAddress> getUnavailableServers()
Get the addresses of unavailable servers.

This is based on a snapshot in time so shouldn't be considered completely accurate, but is a useful for getting a feel for what's working and what's not working.

Specified by:
getUnavailableServers in interface MemcachedClientIF

setTranscoder

public void setTranscoder(Transcoder<java.lang.Object> tc)
Set the default transcoder for managing the cache representations of objects going in and out of the cache.

Specified by:
setTranscoder in interface MemcachedClientIF

getTranscoder

public Transcoder<java.lang.Object> getTranscoder()
Get the default transcoder that's in use.

Specified by:
getTranscoder in interface MemcachedClientIF

append

public java.util.concurrent.Future<java.lang.Boolean> append(long cas,
                                                             java.lang.String key,
                                                             java.lang.Object val)
Append to an existing value in the cache.

Specified by:
append in interface MemcachedClientIF
Parameters:
cas - cas identifier (ignored in the ascii protocol)
key - the key to whose value will be appended
val - the value to append
Returns:
a future indicating success

append

public <T> java.util.concurrent.Future<java.lang.Boolean> append(long cas,
                                                                 java.lang.String key,
                                                                 T val,
                                                                 Transcoder<T> tc)
Append to an existing value in the cache.

Specified by:
append in interface MemcachedClientIF
Parameters:
cas - cas identifier (ignored in the ascii protocol)
key - the key to whose value will be appended
val - the value to append
tc - the transcoder to serialize and unserialize the value
Returns:
a future indicating success

prepend

public java.util.concurrent.Future<java.lang.Boolean> prepend(long cas,
                                                              java.lang.String key,
                                                              java.lang.Object val)
Prepend to an existing value in the cache.

Specified by:
prepend in interface MemcachedClientIF
Parameters:
cas - cas identifier (ignored in the ascii protocol)
key - the key to whose value will be prepended
val - the value to append
Returns:
a future indicating success

prepend

public <T> java.util.concurrent.Future<java.lang.Boolean> prepend(long cas,
                                                                  java.lang.String key,
                                                                  T val,
                                                                  Transcoder<T> tc)
Prepend to an existing value in the cache.

Specified by:
prepend in interface MemcachedClientIF
Parameters:
cas - cas identifier (ignored in the ascii protocol)
key - the key to whose value will be prepended
val - the value to append
tc - the transcoder to serialize and unserialize the value
Returns:
a future indicating success

asyncCAS

public <T> java.util.concurrent.Future<CASResponse> asyncCAS(java.lang.String key,
                                                             long casId,
                                                             T value,
                                                             Transcoder<T> tc)
Asynchronous CAS operation.

Specified by:
asyncCAS in interface MemcachedClientIF
Parameters:
key - the key
casId - the CAS identifier (from a gets operation)
value - the new value
tc - the transcoder to serialize and unserialize the value
Returns:
a future that will indicate the status of the CAS

asyncCAS

public java.util.concurrent.Future<CASResponse> asyncCAS(java.lang.String key,
                                                         long casId,
                                                         java.lang.Object value)
Asynchronous CAS operation using the default transcoder.

Specified by:
asyncCAS in interface MemcachedClientIF
Parameters:
key - the key
casId - the CAS identifier (from a gets operation)
value - the new value
Returns:
a future that will indicate the status of the CAS

cas

public <T> CASResponse cas(java.lang.String key,
                           long casId,
                           T value,
                           Transcoder<T> tc)
                throws OperationTimeoutException
Perform a synchronous CAS operation.

Specified by:
cas in interface MemcachedClientIF
Parameters:
key - the key
casId - the CAS identifier (from a gets operation)
value - the new value
tc - the transcoder to serialize and unserialize the value
Returns:
a CASResponse
Throws:
OperationTimeoutException - if global operation timeout is exceeded

cas

public CASResponse cas(java.lang.String key,
                       long casId,
                       java.lang.Object value)
                throws OperationTimeoutException
Perform a synchronous CAS operation with the default transcoder.

Specified by:
cas in interface MemcachedClientIF
Parameters:
key - the key
casId - the CAS identifier (from a gets operation)
value - the new value
Returns:
a CASResponse
Throws:
OperationTimeoutException - if the global operation timeout is exceeded

add

public <T> java.util.concurrent.Future<java.lang.Boolean> add(java.lang.String key,
                                                              int exp,
                                                              T o,
                                                              Transcoder<T> tc)
Add an object to the cache iff it does not exist already.

The exp value is passed along to memcached exactly as given, and will be processed per the memcached protocol specification:

The actual value sent may either be Unix time (number of seconds since January 1, 1970, as a 32-bit value), or a number of seconds starting from current time. In the latter case, this number of seconds may not exceed 60*60*24*30 (number of seconds in 30 days); if the number sent by a client is larger than that, the server will consider it to be real Unix time value rather than an offset from current time.

Specified by:
add in interface MemcachedClientIF
Parameters:
key - the key under which this object should be added.
exp - the expiration of this object
o - the object to store
tc - the transcoder to serialize and unserialize the value
Returns:
a future representing the processing of this operation

add

public java.util.concurrent.Future<java.lang.Boolean> add(java.lang.String key,
                                                          int exp,
                                                          java.lang.Object o)
Add an object to the cache (using the default transcoder) iff it does not exist already.

The exp value is passed along to memcached exactly as given, and will be processed per the memcached protocol specification:

The actual value sent may either be Unix time (number of seconds since January 1, 1970, as a 32-bit value), or a number of seconds starting from current time. In the latter case, this number of seconds may not exceed 60*60*24*30 (number of seconds in 30 days); if the number sent by a client is larger than that, the server will consider it to be real Unix time value rather than an offset from current time.

Specified by:
add in interface MemcachedClientIF
Parameters:
key - the key under which this object should be added.
exp - the expiration of this object
o - the object to store
Returns:
a future representing the processing of this operation

set

public <T> java.util.concurrent.Future<java.lang.Boolean> set(java.lang.String key,
                                                              int exp,
                                                              T o,
                                                              Transcoder<T> tc)
Set an object in the cache regardless of any existing value.

The exp value is passed along to memcached exactly as given, and will be processed per the memcached protocol specification:

The actual value sent may either be Unix time (number of seconds since January 1, 1970, as a 32-bit value), or a number of seconds starting from current time. In the latter case, this number of seconds may not exceed 60*60*24*30 (number of seconds in 30 days); if the number sent by a client is larger than that, the server will consider it to be real Unix time value rather than an offset from current time.

Specified by:
set in interface MemcachedClientIF
Parameters:
key - the key under which this object should be added.
exp - the expiration of this object
o - the object to store
tc - the transcoder to serialize and unserialize the value
Returns:
a future representing the processing of this operation

set

public java.util.concurrent.Future<java.lang.Boolean> set(java.lang.String key,
                                                          int exp,
                                                          java.lang.Object o)
Set an object in the cache (using the default transcoder) regardless of any existing value.

The exp value is passed along to memcached exactly as given, and will be processed per the memcached protocol specification:

The actual value sent may either be Unix time (number of seconds since January 1, 1970, as a 32-bit value), or a number of seconds starting from current time. In the latter case, this number of seconds may not exceed 60*60*24*30 (number of seconds in 30 days); if the number sent by a client is larger than that, the server will consider it to be real Unix time value rather than an offset from current time.

Specified by:
set in interface MemcachedClientIF
Parameters:
key - the key under which this object should be added.
exp - the expiration of this object
o - the object to store
Returns:
a future representing the processing of this operation

replace

public <T> java.util.concurrent.Future<java.lang.Boolean> replace(java.lang.String key,
                                                                  int exp,
                                                                  T o,
                                                                  Transcoder<T> tc)
Replace an object with the given value iff there is already a value for the given key.

The exp value is passed along to memcached exactly as given, and will be processed per the memcached protocol specification:

The actual value sent may either be Unix time (number of seconds since January 1, 1970, as a 32-bit value), or a number of seconds starting from current time. In the latter case, this number of seconds may not exceed 60*60*24*30 (number of seconds in 30 days); if the number sent by a client is larger than that, the server will consider it to be real Unix time value rather than an offset from current time.

Specified by:
replace in interface MemcachedClientIF
Parameters:
key - the key under which this object should be added.
exp - the expiration of this object
o - the object to store
tc - the transcoder to serialize and unserialize the value
Returns:
a future representing the processing of this operation

replace

public java.util.concurrent.Future<java.lang.Boolean> replace(java.lang.String key,
                                                              int exp,
                                                              java.lang.Object o)
Replace an object with the given value (transcoded with the default transcoder) iff there is already a value for the given key.

The exp value is passed along to memcached exactly as given, and will be processed per the memcached protocol specification:

The actual value sent may either be Unix time (number of seconds since January 1, 1970, as a 32-bit value), or a number of seconds starting from current time. In the latter case, this number of seconds may not exceed 60*60*24*30 (number of seconds in 30 days); if the number sent by a client is larger than that, the server will consider it to be real Unix time value rather than an offset from current time.

Specified by:
replace in interface MemcachedClientIF
Parameters:
key - the key under which this object should be added.
exp - the expiration of this object
o - the object to store
Returns:
a future representing the processing of this operation

asyncGet

public <T> java.util.concurrent.Future<T> asyncGet(java.lang.String key,
                                                   Transcoder<T> tc)
Get the given key asynchronously.

Specified by:
asyncGet in interface MemcachedClientIF
Parameters:
key - the key to fetch
tc - the transcoder to serialize and unserialize value
Returns:
a future that will hold the return value of the fetch

asyncGet

public java.util.concurrent.Future<java.lang.Object> asyncGet(java.lang.String key)
Get the given key asynchronously and decode with the default transcoder.

Specified by:
asyncGet in interface MemcachedClientIF
Parameters:
key - the key to fetch
Returns:
a future that will hold the return value of the fetch

asyncGets

public <T> java.util.concurrent.Future<CASValue<T>> asyncGets(java.lang.String key,
                                                              Transcoder<T> tc)
Gets (with CAS support) the given key asynchronously.

Specified by:
asyncGets in interface MemcachedClientIF
Parameters:
key - the key to fetch
tc - the transcoder to serialize and unserialize value
Returns:
a future that will hold the return value of the fetch

asyncGets

public java.util.concurrent.Future<CASValue<java.lang.Object>> asyncGets(java.lang.String key)
Gets (with CAS support) the given key asynchronously and decode using the default transcoder.

Specified by:
asyncGets in interface MemcachedClientIF
Parameters:
key - the key to fetch
Returns:
a future that will hold the return value of the fetch

gets

public <T> CASValue<T> gets(java.lang.String key,
                            Transcoder<T> tc)
                 throws OperationTimeoutException
Gets (with CAS support) with a single key.

Specified by:
gets in interface MemcachedClientIF
Parameters:
key - the key to get
tc - the transcoder to serialize and unserialize value
Returns:
the result from the cache and CAS id (null if there is none)
Throws:
OperationTimeoutException - if global operation timeout is exceeded

gets

public CASValue<java.lang.Object> gets(java.lang.String key)
                                throws OperationTimeoutException
Gets (with CAS support) with a single key using the default transcoder.

Specified by:
gets in interface MemcachedClientIF
Parameters:
key - the key to get
Returns:
the result from the cache and CAS id (null if there is none)
Throws:
OperationTimeoutException - if the global operation timeout is exceeded

get

public <T> T get(java.lang.String key,
                 Transcoder<T> tc)
      throws OperationTimeoutException
Get with a single key.

Specified by:
get in interface MemcachedClientIF
Parameters:
key - the key to get
tc - the transcoder to serialize and unserialize value
Returns:
the result from the cache (null if there is none)
Throws:
OperationTimeoutException - if the global operation timeout is exceeded

get

public java.lang.Object get(java.lang.String key)
                     throws OperationTimeoutException
Get with a single key and decode using the default transcoder.

Specified by:
get in interface MemcachedClientIF
Parameters:
key - the key to get
Returns:
the result from the cache (null if there is none)
Throws:
OperationTimeoutException - if the global operation timeout is exceeded

asyncGetBulk

public <T> java.util.concurrent.Future<java.util.Map<java.lang.String,T>> asyncGetBulk(java.util.Collection<java.lang.String> keys,
                                                                                       Transcoder<T> tc)
Asynchronously get a bunch of objects from the cache.

Specified by:
asyncGetBulk in interface MemcachedClientIF
Parameters:
keys - the keys to request
tc - the transcoder to serialize and unserialize value
Returns:
a Future result of that fetch

asyncGetBulk

public java.util.concurrent.Future<java.util.Map<java.lang.String,java.lang.Object>> asyncGetBulk(java.util.Collection<java.lang.String> keys)
Asynchronously get a bunch of objects from the cache and decode them with the given transcoder.

Specified by:
asyncGetBulk in interface MemcachedClientIF
Parameters:
keys - the keys to request
Returns:
a Future result of that fetch

asyncGetBulk

public <T> java.util.concurrent.Future<java.util.Map<java.lang.String,T>> asyncGetBulk(Transcoder<T> tc,
                                                                                       java.lang.String... keys)
Varargs wrapper for asynchronous bulk gets.

Specified by:
asyncGetBulk in interface MemcachedClientIF
Parameters:
tc - the transcoder to serialize and unserialize value
keys - one more more keys to get
Returns:
the future values of those keys

asyncGetBulk

public java.util.concurrent.Future<java.util.Map<java.lang.String,java.lang.Object>> asyncGetBulk(java.lang.String... keys)
Varargs wrapper for asynchronous bulk gets with the default transcoder.

Specified by:
asyncGetBulk in interface MemcachedClientIF
Parameters:
keys - one more more keys to get
Returns:
the future values of those keys

getBulk

public <T> java.util.Map<java.lang.String,T> getBulk(java.util.Collection<java.lang.String> keys,
                                                     Transcoder<T> tc)
                                          throws OperationTimeoutException
Get the values for multiple keys from the cache.

Specified by:
getBulk in interface MemcachedClientIF
Parameters:
keys - the keys
tc - the transcoder to serialize and unserialize value
Returns:
a map of the values (for each value that exists)
Throws:
OperationTimeoutException - if the global operation timeout is exceeded

getBulk

public java.util.Map<java.lang.String,java.lang.Object> getBulk(java.util.Collection<java.lang.String> keys)
                                                         throws OperationTimeoutException
Get the values for multiple keys from the cache.

Specified by:
getBulk in interface MemcachedClientIF
Parameters:
keys - the keys
Returns:
a map of the values (for each value that exists)
Throws:
OperationTimeoutException - if the global operation timeout is exceeded

getBulk

public <T> java.util.Map<java.lang.String,T> getBulk(Transcoder<T> tc,
                                                     java.lang.String... keys)
                                          throws OperationTimeoutException
Get the values for multiple keys from the cache.

Specified by:
getBulk in interface MemcachedClientIF
Parameters:
tc - the transcoder to serialize and unserialize value
keys - the keys
Returns:
a map of the values (for each value that exists)
Throws:
OperationTimeoutException - if the global operation timeout is exceeded

getBulk

public java.util.Map<java.lang.String,java.lang.Object> getBulk(java.lang.String... keys)
                                                         throws OperationTimeoutException
Get the values for multiple keys from the cache.

Specified by:
getBulk in interface MemcachedClientIF
Parameters:
keys - the keys
Returns:
a map of the values (for each value that exists)
Throws:
OperationTimeoutException - if the global operation timeout is exceeded

getVersions

public java.util.Map<java.net.SocketAddress,java.lang.String> getVersions()
Get the versions of all of the connected memcacheds.

Specified by:
getVersions in interface MemcachedClientIF

getStats

public java.util.Map<java.net.SocketAddress,java.util.Map<java.lang.String,java.lang.String>> getStats()
Get all of the stats from all of the connections.

Specified by:
getStats in interface MemcachedClientIF

getStats

public java.util.Map<java.net.SocketAddress,java.util.Map<java.lang.String,java.lang.String>> getStats(java.lang.String arg)
Get a set of stats from all connections.

Specified by:
getStats in interface MemcachedClientIF
Parameters:
arg - which stats to get
Returns:
a Map of the server SocketAddress to a map of String stat keys to String stat values.

incr

public long incr(java.lang.String key,
                 int by)
          throws OperationTimeoutException
Increment the given key by the given amount.

Specified by:
incr in interface MemcachedClientIF
Parameters:
key - the key
by - the amount to increment
Returns:
the new value (-1 if the key doesn't exist)
Throws:
OperationTimeoutException - if the global operation timeout is exceeded

decr

public long decr(java.lang.String key,
                 int by)
          throws OperationTimeoutException
Decrement the given key by the given value.

Specified by:
decr in interface MemcachedClientIF
Parameters:
key - the key
by - the value
Returns:
the new value (-1 if the key doesn't exist)
Throws:
OperationTimeoutException - if the global operation timeout is exceeded

asyncIncr

public java.util.concurrent.Future<java.lang.Long> asyncIncr(java.lang.String key,
                                                             int by)
Asychronous increment.

Specified by:
asyncIncr in interface MemcachedClientIF
Returns:
a future with the incremented value, or -1 if the increment failed.

asyncDecr

public java.util.concurrent.Future<java.lang.Long> asyncDecr(java.lang.String key,
                                                             int by)
Asynchronous decrement.

Specified by:
asyncDecr in interface MemcachedClientIF
Returns:
a future with the decremented value, or -1 if the increment failed.

incr

public long incr(java.lang.String key,
                 int by,
                 long def)
          throws OperationTimeoutException
Increment the given counter, returning the new value.

Specified by:
incr in interface MemcachedClientIF
Parameters:
key - the key
by - the amount to increment
def - the default value (if the counter does not exist)
Returns:
the new value, or -1 if we were unable to increment or add
Throws:
OperationTimeoutException - if the global operation timeout is exceeded

decr

public long decr(java.lang.String key,
                 int by,
                 long def)
          throws OperationTimeoutException
Decrement the given counter, returning the new value.

Specified by:
decr in interface MemcachedClientIF
Parameters:
key - the key
by - the amount to decrement
def - the default value (if the counter does not exist)
Returns:
the new value, or -1 if we were unable to decrement or add
Throws:
OperationTimeoutException - if the global operation timeout is exceeded

delete

@Deprecated
public java.util.concurrent.Future<java.lang.Boolean> delete(java.lang.String key,
                                                                        int hold)
Deprecated. Hold values are no longer honored.

Delete the given key from the cache.

The hold argument specifies the amount of time in seconds (or Unix time until which) the client wishes the server to refuse "add" and "replace" commands with this key. For this amount of item, the item is put into a delete queue, which means that it won't possible to retrieve it by the "get" command, but "add" and "replace" command with this key will also fail (the "set" command will succeed, however). After the time passes, the item is finally deleted from server memory.

Parameters:
key - the key to delete
hold - how long the key should be unavailable to add commands

delete

public java.util.concurrent.Future<java.lang.Boolean> delete(java.lang.String key)
Delete the given key from the cache.

Specified by:
delete in interface MemcachedClientIF
Parameters:
key - the key to delete

flush

public java.util.concurrent.Future<java.lang.Boolean> flush(int delay)
Flush all caches from all servers with a delay of application.

Specified by:
flush in interface MemcachedClientIF

flush

public java.util.concurrent.Future<java.lang.Boolean> flush()
Flush all caches from all servers immediately.

Specified by:
flush in interface MemcachedClientIF

run

public void run()
Infinitely loop processing IO.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

shutdown

public void shutdown()
Shut down immediately.

Specified by:
shutdown in interface MemcachedClientIF

shutdown

public boolean shutdown(long timeout,
                        java.util.concurrent.TimeUnit unit)
Shut down this client gracefully.

Specified by:
shutdown in interface MemcachedClientIF

waitForQueues

public boolean waitForQueues(long timeout,
                             java.util.concurrent.TimeUnit unit)
Wait for the queues to die down.

Specified by:
waitForQueues in interface MemcachedClientIF