Skip to content
This repository
Browse code

read_multi reads from LocalStore when it exists

  • Loading branch information...
commit 4399ffd7978ab515c7095ea0bac544f78bf80b1b 1 parent 6cefc8b
Jack Chen chendo authored
11 lib/active_support/cache/dalli_store.rb
@@ -127,7 +127,16 @@ def read_multi(*names)
127 127 names = names.flatten
128 128 mapping = names.inject({}) { |memo, name| memo[expanded_key(name)] = name; memo }
129 129 instrument(:read_multi, names) do
130   - results = @data.get_multi(mapping.keys)
  130 + results = {}
  131 + if local_cache
  132 + mapping.keys.each do |key|
  133 + if value = local_cache.read_entry(key, options)
  134 + results[key] = value
  135 + end
  136 + end
  137 + end
  138 +
  139 + results.merge!(@data.get_multi(mapping.keys - results.keys))
131 140 results.inject({}) do |memo, (inner, _)|
132 141 entry = results[inner]
133 142 # NB Backwards data compatibility, to be removed at some point
14 test/test_active_support.rb
@@ -122,15 +122,25 @@ def cache_key
122 122 y = rand_key
123 123 assert_equal({}, @dalli.read_multi(x, y))
124 124 @dalli.write(x, '123')
125   - @dalli.write(y, 123)
  125 + @dalli.write(y, 456)
126 126
127 127 @dalli.with_local_cache do
128   - assert_equal({ x => '123', y => 123 }, @dalli.read_multi(x, y))
  128 + assert_equal({ x => '123', y => 456 }, @dalli.read_multi(x, y))
129 129 Dalli::Client.any_instance.expects(:get).with(any_parameters).never
130 130
131 131 dres = @dalli.read(x)
132 132 assert_equal dres, '123'
133 133 end
  134 +
  135 + Dalli::Client.any_instance.unstub(:get)
  136 +
  137 + # Fresh LocalStore
  138 + @dalli.with_local_cache do
  139 + @dalli.read(x)
  140 + Dalli::Client.any_instance.expects(:get_multi).with([y.to_s]).returns(y.to_s => 456)
  141 +
  142 + assert_equal({ x => '123', y => 456}, @dalli.read_multi(x, y))
  143 + end
134 144 end
135 145 end
136 146 end

0 comments on commit 4399ffd

Please sign in to comment.
Something went wrong with that request. Please try again.