Created
May 3, 2013 19:05
-
-
Save alevy/5512907 to your computer and use it in GitHub Desktop.
Dalli store/get large value
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' | |
module Dalli | |
class Client | |
def set_large(key, val, ttl = 0) | |
chunk_size = 1024 * 900 | |
i = 0 | |
start_idx = 0 | |
next_chunk = val[start_idx..(start_idx + chunk_size - 1)] | |
start_idx += chunk_size | |
set(key, 0, ttl, :raw => true) | |
multi do | |
while next_chunk | |
set("#{key}_#{i}", next_chunk, ttl, :raw => true) | |
incr(key) | |
i += 1 | |
next_chunk = val[start_idx..(start_idx + chunk_size - 1)] | |
start_idx += chunk_size | |
end | |
end | |
return i | |
end | |
def get_large(key) | |
num_chunks = get(key).to_i | |
keys = (0..(num_chunks - 1)).to_a.map {|i| "#{key}_#{i}" } | |
return get_multi(keys).sort.map {|k,v| v}.join | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment