Skip to content

Instantly share code, notes, and snippets.

@alevy
Created July 16, 2012 20:21
Show Gist options
  • Save alevy/3124786 to your computer and use it in GitHub Desktop.
Save alevy/3124786 to your computer and use it in GitHub Desktop.
Storing large objects in memcache
require 'dalli'
require 'benchmark'
client = Dalli::Client.new
# As an example, generate a large random string
@mylargeobject = (0...5000).map{ ('a'..'z').to_a[rand(26)] }.join
@mykey = "mykey"
Benchmark.bm do |b|
b.report("multi_set") do
1000.times do
client.multi do
i = 0
while (j = i * 1000; increment = @mylargeobject[j..(j + 999)]; increment.size > 0)
client.set("#{@mykey}_#{i}", increment)
i += 1
end
client.set(@mykey, i)
end
end
end
b.report("multi_get") do
1000.times do
i = client.get(@mykey)
keys = (0..(i-1)).map {|j| "#{@mykey}_#{j}"}
client.get_multi(keys).values.join("")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment