Skip to content

Commit

Permalink
fix get_some validation/overwrite bug. thanks kgale and mhat
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjudd committed Sep 24, 2010
1 parent 4238b97 commit 4ce70ce
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/memcache.rb
Expand Up @@ -260,11 +260,10 @@ def get_some(keys, opts = {})
end

keys_to_fetch = keys - records.keys
method = opts[:overwrite] ? :set : :add
if keys_to_fetch.any?
yield(keys_to_fetch).each do |key, value|
begin
self.send(method, key, value, opts) unless opts[:disable] or opts[:disable_write]
set(key, value, {}) unless opts[:disable] or opts[:disable_write]
rescue Memcache::Error => e
raise if opts[:strict_write]
$stderr.puts "Memcache error in get_some: #{e.class} #{e.to_s} on key '#{key}' while storing value: #{value}"
Expand Down
25 changes: 25 additions & 0 deletions test/memcache_test.rb
Expand Up @@ -157,6 +157,31 @@ def test_get_some
end
end

def test_get_some_with_validation
100.times do |i|
m.set(i.to_s, i.odd? ? -i : i)
end
keys = (0...100).collect {|key| key.to_s}

results = m.get_some(keys, :validation => lambda {|k,v| k.to_i == v}) do |missing_keys|
assert_equal 50, missing_keys.size
r = {}
missing_keys.each do |key|
r[key] = key.to_i
end
r
end
assert_equal 100, results.size

results.each do |key, value|
assert_equal key.to_i, value
end

results = m.get_some(keys, :validation => lambda {|k,v| k.to_i == v}) do |missing_keys|
flunk "no values should be invalid"
end
end

def test_get_with_reset_expiry
m.add('foo', 'quick brown fox', :expiry => 1)
assert_equal 'quick brown fox', m.get('foo', :expiry => 2)
Expand Down

0 comments on commit 4ce70ce

Please sign in to comment.