Commit 56b1e7a2 authored by Rajko Albrecht's avatar Rajko Albrecht

* Improved cas_multi

* description and test for cas_multi with TTL
parent 79b3b79a
......@@ -52,25 +52,26 @@ class Redis
# newhashedvalues
# end
# @example
# storewithcas.cas_multi('k1','k2', :expire_in => 1200) do |currenthash| #=> ttl for all keys swapped
# {'k1' => '1', 'k2' => 2}
# end
# @yield [values] a key=>value hash with values of given keys. keys not existing are not yielded
# @yieldparam [Array] keys the keys to change
# @yieldreturn [Hash] key-value-pairs to change. Keys not given in parameter or not existing in redis are ignored.
# @return [Boolean] true if tried making changes, nil when keylist empty
# @param keys [Array] the keys to set. Must not be nil and keys must exists in Redis
# @param keys [Array] the keys to set. Must not be nil and keys must exists in Redis. After keys list you may append hash with options for redis.
def cas_multi *keys
#options = extract_options keys
return if keys.empty?
options = extract_options keys
watch(*keys) do
values = read_multi(*keys)
options = extract_options keys
values = read_multi(*keys,options)
valuehash = yield values
result = {}
v = do |name,value|
ires = multi do |multi|
multi.set(name,value,options) if values.key?(name)
result[name] = value if return_value(ires) do |name,value|
multi do |multi|
end if values.key?(name)
......@@ -68,6 +68,16 @@ describe Redis::StoreWithCas do
assert_equal({ "foo" => "baz", "fud" => "buz" }, @store.read_multi('foo', 'fud'))
def test_cas_multi_with_ttl
@store.set('foo', 'bar')
@store.set('fud', 'biz')
@store.cas_multi('foo','fud',{:expires_in => 3600}) do |hash|
{ "foo" => "baz", "fud" => "buz" }
assert @store.ttl('foo') > 0
assert @store.ttl('fud') > 0
def test_cas_multi_with_cache_miss
assert(@store.cas_multi('not_exist') do |hash|
assert hash.empty?
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment