Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #306 from chendo/add-localcache-support-for-read_m…

…ulti

Add LocalCache support for read_multi
  • Loading branch information...
commit 179fc647ec7fcd2266fa8a1874888b45838fb518 2 parents c3393a0 + 785828b
@mperham authored
View
15 lib/active_support/cache/dalli_store.rb
@@ -127,11 +127,22 @@ 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
- memo[mapping[inner]] = (entry.is_a?(ActiveSupport::Cache::Entry) ? entry.value : entry)
+ value = (entry.is_a?(ActiveSupport::Cache::Entry) ? entry.value : entry)
+ memo[mapping[inner]] = value
+ local_cache.write_entry(inner, value, options) if local_cache
memo
end
end
View
57 test/benchmark_test.rb
@@ -24,7 +24,7 @@
should 'run benchmarks' do
memcached do
- Benchmark.bm(31) do |x|
+ Benchmark.bm(37) do |x|
n = 2500
@@ -44,6 +44,61 @@
end
end
+ x.report("mixed:rails-localcache:dalli") do
+ n.times do
+ @ds.with_local_cache do
+ @ds.read @key1
+ @ds.write @key2, @value
+ @ds.fetch(@key3) { @value }
+ @ds.fetch(@key2) { @value }
+ @ds.fetch(@key1) { @value }
+ @ds.write @key2, @value, :unless_exists => true
+ @ds.delete @key2
+ @ds.increment @counter, 1, :initial => 100
+ @ds.increment @counter, 1, :expires_in => 12
+ @ds.decrement @counter, 1
+ end
+ end
+ end
+
+ @ds.clear
+ sizeable_data = "<marquee>some view partial data</marquee>" * 50
+ [@key1, @key2, @key3, @key4, @key5, @key6].each do |key|
+ @ds.write(key, sizeable_data)
+ end
+
+ x.report("read_multi_big:rails:dalli") do
+ n.times do
+ @ds.read_multi @key1, @key2, @key3, @key4
+ @ds.read @key1
+ @ds.read @key2
+ @ds.read @key3
+ @ds.read @key4
+ @ds.read @key1
+ @ds.read @key2
+ @ds.read @key3
+ @ds.read_multi @key1, @key2, @key3
+ end
+ end
+
+ x.report("read_multi_big:rails-localcache:dalli") do
+ n.times do
+ @ds.with_local_cache do
+ @ds.read_multi @key1, @key2, @key3, @key4
+ @ds.read @key1
+ @ds.read @key2
+ @ds.read @key3
+ @ds.read @key4
+ end
+ @ds.with_local_cache do
+ @ds.read @key1
+ @ds.read @key2
+ @ds.read @key3
+ @ds.read_multi @key1, @key2, @key3
+ end
+ end
+ end
+
@m = Dalli::Client.new(@servers)
x.report("set:plain:dalli") do
n.times do
View
59 test/test_active_support.rb
@@ -114,6 +114,37 @@ def cache_key
end
end
+ should 'support read_multi with LocalCache' do
+ with_activesupport do
+ memcached do
+ connect
+ x = rand_key
+ y = rand_key
+ assert_equal({}, @dalli.read_multi(x, y))
+ @dalli.write(x, '123')
+ @dalli.write(y, 456)
+
+ @dalli.with_local_cache do
+ 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
+
should 'support read, write and delete' do
with_activesupport do
memcached do
@@ -147,6 +178,34 @@ def cache_key
end
end
+ should 'support read, write and delete with LocalCache' do
+ with_activesupport do
+ memcached do
+ connect
+ y = rand_key.to_s
+ @dalli.with_local_cache do
+ Dalli::Client.any_instance.expects(:get).with(y, {}).once.returns(123)
+ dres = @dalli.read(y)
+ assert_equal 123, dres
+
+ Dalli::Client.any_instance.expects(:get).with(y, {}).never
+
+ dres = @dalli.read(y)
+ assert_equal 123, dres
+
+ @dalli.write(y, 456)
+ dres = @dalli.read(y)
+ assert_equal 456, dres
+
+ @dalli.delete(y)
+ Dalli::Client.any_instance.expects(:get).with(y, {}).once.returns(nil)
+ dres = @dalli.read(y)
+ assert_equal nil, dres
+ end
+ end
+ end
+ end
+
should 'support increment/decrement commands' do
with_activesupport do
memcached do
Please sign in to comment.
Something went wrong with that request. Please try again.