Commit c5011f2a authored by Rajko Albrecht's avatar Rajko Albrecht

cas_multi:

 * change keys
 * correct work for complete missing keys
parent 8d628078
...@@ -17,19 +17,28 @@ class Redis ...@@ -17,19 +17,28 @@ class Redis
end end
def cas_multi *keys def cas_multi *keys
options = extract_options keys #options = extract_options keys
return if keys.empty? return if keys.empty?
watch keys do watch(*keys) do
values = read_multi keys.merge(options) values = read_multi(*keys)
options = extract_options keys
valuehash = yield values valuehash = yield values
ires = multi do |multi|
valuehash.map do |name,value|
multi.set(name,value,options)
end
end
true
end end
end end
def read_multi *keys def read_multi *keys
values = mget(*keys) options = extract_options keys
keys = keys.select {|k| exists(k)}
return {} if keys.empty?
values = mget(*(keys+[options]))
resulthash = {} resulthash = {}
keys.zip(values) { |a,b| resulthash[a.to_s] = b} keys.zip(values) { |a,b| resulthash[a.to_s] = b} unless values.nil?
resulthash resulthash
end end
......
...@@ -43,4 +43,23 @@ describe Redis::Store::StoreWithCas do ...@@ -43,4 +43,23 @@ describe Redis::Store::StoreWithCas do
@store.set('k2','m2') @store.set('k2','m2')
assert_equal({"k1" => "m1","k2" => "m2"},@store.read_multi("k1","k2")) assert_equal({"k1" => "m1","k2" => "m2"},@store.read_multi("k1","k2"))
end end
def test_cas_multi
@store.set('foo', 'bar')
@store.set('fud', 'biz')
assert_equal true, (@store.cas_multi('foo', 'fud') do |hash|
assert_equal({ "foo" => "bar", "fud" => "biz" }, hash)
{ "foo" => "baz", "fud" => "buz" }
end)
assert_equal({ "foo" => "baz", "fud" => "buz" }, @store.read_multi('foo', 'fud'))
end
def test_cas_multi_with_cache_miss
assert(@store.cas_multi('not_exist') do |hash|
assert hash.empty?
{}
end)
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment