Created
May 29, 2015 09:56
-
-
Save xiewenwei/b55070b49a73f187c284 to your computer and use it in GitHub Desktop.
read_cache_of_memcached.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'dalli' | |
require 'active_support' | |
require 'active_support/cache/dalli_store' | |
CACHE = ::ActiveSupport::Cache::DalliStore.new 'localhost', namespace: 'demo' | |
n = 10000 | |
n.times {|i| CACHE.write "user:#{i}:counter", i * i } | |
def read_in_batch(total, batch_size) | |
(1..total).each_slice(batch_size) do |slice| | |
batch = slice.map { |i| "user:#{i}:counter" } | |
CACHE.read_multi(*batch) | |
end | |
end | |
Benchmark.bmbm do |x| | |
x.report "1-1" do | |
n.times do |i| | |
CACHE.read "user:#{i}:counter" | |
end | |
end | |
x.report "2-1" do | |
read_in_batch n, 1 | |
end | |
x.report " 10" do | |
read_in_batch n, 10 | |
end | |
x.report " 30" do | |
read_in_batch n, 30 | |
end | |
x.report " 50" do | |
read_in_batch n, 50 | |
end | |
x.report "100" do | |
read_in_batch n, 100 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment