Skip to content

Instantly share code, notes, and snippets.

@xiewenwei
Created May 29, 2015 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiewenwei/b55070b49a73f187c284 to your computer and use it in GitHub Desktop.
Save xiewenwei/b55070b49a73f187c284 to your computer and use it in GitHub Desktop.
read_cache_of_memcached.rb
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