Created
January 24, 2012 10:10
-
-
Save patrikjohansson/1669425 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
def serialize(key, value, options=nil) | |
marshalled = false | |
value = unless options && options[:raw] | |
marshalled = true | |
begin | |
Marshal.dump(value) | |
rescue => ex | |
# Marshalling can throw several different types of generic Ruby exceptions. | |
# Convert to a specific exception so we can special case it higher up the stack. | |
exc = Dalli::MarshalError.new(ex.message) | |
exc.set_backtrace ex.backtrace | |
raise exc | |
end | |
else | |
value.to_s | |
end | |
compressed = false | |
if @options[:compression] && value.bytesize >= COMPRESSION_MIN_SIZE | |
value = Zlib::Deflate.deflate(value) | |
compressed = true | |
end | |
raise Dalli::DalliError, "Value too large, memcached can only store #{@options[:value_max_bytes]} bytes per key [key: #{key}, size: #{value.bytesize}]" if value.bytesize > @options[:value_max_bytes] | |
flags = 0 | |
flags |= FLAG_COMPRESSED if compressed | |
flags |= FLAG_MARSHALLED if marshalled | |
[value, flags] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment