def cas(key, options = {})
      retries_remaining = options.delete(:retry) || 0
      loop do
        res = get(key, options)
        val = yield(res.value) # get new value from caller
        res = set(key, val, options.merge(:cas => res.cas))
        if res.error && res.error.code == Couchbase::LibraryError::LCB_KEY_EEXISTS
          if retries_remaining > 0
            retries_remaining -= 1
            next
          end
        end
        return res
      end
    end
def cas(key, options = {})
      retries_remaining = options.delete(:retry) || 0
      loop do
        res = get(key, options)
        val = yield(res.value) # get new value from caller
        res = set(key, val, options.merge(:cas => res.cas))
        if res.error && res.error.code == Couchbase::LibraryError::LCB_KEY_EEXISTS
          if retries_remaining > 0
            retries_remaining -= 1
            next
          end
        end
        return res
      end
    end
Compare and swap value.

 @since 1.0.0

 Reads a key's value from the server and yields it to a block. Replaces
 the key's value with the result of the block as long as the key hasn't
 been updated in the meantime, otherwise raises
 {Couchbase::Error::KeyExists}. CAS stands for "compare and swap", and
 avoids the need for manual key mutexing. Read more info here:

 @see http://couchbase.com/docs/memcached-api/memcached-api-protocol-text_cas.html

 Setting the +:retry+ option to a positive number will cause this method
 to rescue the {Couchbase::Error::KeyExists} error that happens when
 an update collision is detected, and automatically get a fresh copy
 of the value and retry the block. This will repeat as long as there
 continues to be conflicts, up to the maximum number of retries specified.

 @param [String, Symbol] key

 @param [Hash] options the options for "swap" part
 @option options [Fixnum] :ttl (self.default_ttl) the time to live of this key
 @option options [Symbol] :format (self.default_format) format of the value
 @option options [Fixnum] :flags (self.default_flags) flags for this key
 @option options [Fixnum] :retry (0) maximum number of times to autmatically retry upon update collision

 @yieldparam [Object] value old value
 @yieldreturn [Object] new value.

 @raise [Couchbase::Error::KeyExists] if the key was updated before the the
   code in block has been completed (the CAS value has been changed).

 @example Implement append to JSON encoded value

     c.default_format = :document
     c.set("foo", {"bar" => 1})
     c.cas("foo") do |val|
       val["baz"] = 2
       val
     end
     c.get("foo")      #=> {"bar" => 1, "baz" => 2}

 @return [Fixnum] the CAS of new value