net.spy.memcached
Class CASMutator<T>

java.lang.Object
  extended by net.spy.memcached.compat.SpyObject
      extended by net.spy.memcached.CASMutator<T>

public class CASMutator<T>
extends SpyObject

Object that provides mutation via CAS over a given memcache client.

Example usage (reinventing incr):

 // Get or create a client.
 MemcachedClient client=[...];

 // Get a Transcoder.
 Transcoder tc = new LongTranscoder();

 // Get a mutator instance that uses that client.
 CASMutator<Long> mutator=new CASMutator<Long>(client, tc);

 // Get a mutation that knows what to do when a value is found.
 CASMutation<Long> mutation=new CASMutation<Long>() {
     public Long getNewValue(Long current) {
         return current + 1;
     }
 };

 // Do a mutation.
 long currentValue=mutator.cas(someKey, 0L, 0, mutation);
 


Constructor Summary
CASMutator(MemcachedClientIF c, Transcoder<T> tc)
          Construct a CASMutator that uses the given client.
CASMutator(MemcachedClientIF c, Transcoder<T> tc, int max_tries)
          Construct a CASMutator that uses the given client.
 
Method Summary
 T cas(String key, T initial, int initialExp, CASMutation<T> m)
          CAS a new value in for a key.
 
Methods inherited from class net.spy.memcached.compat.SpyObject
getLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CASMutator

public CASMutator(MemcachedClientIF c,
                  Transcoder<T> tc,
                  int max_tries)
Construct a CASMutator that uses the given client.

Parameters:
c - the client
tc - the Transcoder to use
max_tries - the maximum number of attempts to get a CAS to succeed

CASMutator

public CASMutator(MemcachedClientIF c,
                  Transcoder<T> tc)
Construct a CASMutator that uses the given client.

Parameters:
c - the client
tc - the Transcoder to use
Method Detail

cas

public T cas(String key,
             T initial,
             int initialExp,
             CASMutation<T> m)
      throws Exception
CAS a new value in for a key.

Note that if initial is null, this method will only update existing values.

Parameters:
key - the key to be CASed
initial - the value to use when the object is not cached
initialExp - the expiration time to use when initializing
m - the mutation to perform on an object if a value exists for the key
Returns:
the new value that was set
Throws:
Exception