Created
March 10, 2010 18:24
-
-
Save duncanbeevers/328166 to your computer and use it in GitHub Desktop.
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
class Memcached::MultiPartRailsCache | |
MAX_KEY_LENGTH = 255 # Actually overshooting what the client will let you send through | |
# but this means our chunk sizes will just a few bytes of headroom | |
# as we split | |
PART_SIZE = 1.megabyte - MAX_KEY_LENGTH | |
def initialize(rails_cache) | |
@rails_cache = rails_cache | |
end | |
def get(key, raw = true) # raw is discarded | |
parts = (@rails_cache.get(key) or []).map do |part_key| | |
@rails_cache.get(part_key, true) or break | |
end | |
parts.blank? ? nil : parts.join | |
end | |
def set(key, value, expiry = 0, raw = true) # raw is discarded | |
parts = split(value) | |
keys = parts.map.with_index do |p, i| | |
"%s[%i/%i]" % [ key, i + 1, parts.size ] | |
end # Key[1/3] ... | |
parts.zip(keys).each do |part, part_key| | |
@rails_cache.set(part_key, part, expiry, true) | |
end | |
@rails_cache.set(key, keys, expiry) | |
end | |
def delete(key) | |
@rails_cache.delete(key) | |
end | |
private | |
def split data | |
total_parts = (data.size/PART_SIZE.to_f).ceil | |
Array.new(total_parts) do |n| | |
data[n*PART_SIZE...(n+1)*PART_SIZE] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment