Method: Cache#cas

Defined in:
lib/cache.rb

#cas(k, ttl = nil, &blk) ⇒ Object Also known as: compare_and_swap

Get the current value (if any), pass it into a block, and set the result.

Example:

cache.cas 'hello' { |current| 'world' }


151
152
153
154
155
156
157
158
159
# File 'lib/cache.rb', line 151

def cas(k, ttl = nil, &blk)
  handle_fork
  if blk and _exist?(k)
    old_v = _get k
    new_v = blk.call old_v
    _set k, new_v, extract_ttl(ttl)
    new_v
  end
end