Skip to content

Commit

Permalink
read_multi reads from LocalStore when it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
chendo committed Dec 13, 2012
1 parent 6cefc8b commit 4399ffd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
11 changes: 10 additions & 1 deletion lib/active_support/cache/dalli_store.rb
Expand Up @@ -127,7 +127,16 @@ def read_multi(*names)
names = names.flatten
mapping = names.inject({}) { |memo, name| memo[expanded_key(name)] = name; memo }
instrument(:read_multi, names) do
results = @data.get_multi(mapping.keys)
results = {}
if local_cache
mapping.keys.each do |key|
if value = local_cache.read_entry(key, options)
results[key] = value
end
end
end

results.merge!(@data.get_multi(mapping.keys - results.keys))
results.inject({}) do |memo, (inner, _)|
entry = results[inner]
# NB Backwards data compatibility, to be removed at some point
Expand Down
14 changes: 12 additions & 2 deletions test/test_active_support.rb
Expand Up @@ -122,15 +122,25 @@ def cache_key
y = rand_key
assert_equal({}, @dalli.read_multi(x, y))
@dalli.write(x, '123')
@dalli.write(y, 123)
@dalli.write(y, 456)

@dalli.with_local_cache do
assert_equal({ x => '123', y => 123 }, @dalli.read_multi(x, y))
assert_equal({ x => '123', y => 456 }, @dalli.read_multi(x, y))
Dalli::Client.any_instance.expects(:get).with(any_parameters).never

dres = @dalli.read(x)
assert_equal dres, '123'
end

Dalli::Client.any_instance.unstub(:get)

# Fresh LocalStore
@dalli.with_local_cache do
@dalli.read(x)
Dalli::Client.any_instance.expects(:get_multi).with([y.to_s]).returns(y.to_s => 456)

assert_equal({ x => '123', y => 456}, @dalli.read_multi(x, y))
end
end
end
end
Expand Down

0 comments on commit 4399ffd

Please sign in to comment.