Skip to content

Instantly share code, notes, and snippets.

@patrikjohansson
Created January 24, 2012 10:10
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 patrikjohansson/1669425 to your computer and use it in GitHub Desktop.
Save patrikjohansson/1669425 to your computer and use it in GitHub Desktop.
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