class Dalli::Protocol::ValueSerializer

Dalli::Protocol::ValueSerializer compartmentalizes the logic for managing serialization and deserialization of stored values. It manages interpreting relevant options from both client and request, determining whether to serialize/deserialize on store/retrieve, and processes bitflags as necessary.

Constants

ARGUMENT_ERR_REGEXP
DEFAULTS
FLAG_SERIALIZED

www.hjp.at/zettel/m/memcached_flags.rxml Looks like most clients use bit 0 to indicate native language serialization

NAME_ERR_STR
OPTIONS
TYPE_ERR_REGEXP

TODO: Some of these error messages need to be validated. It’s not obvious that all of them are actually generated by the invoked code in current systems rubocop:disable Layout/LineLength

Attributes

serialization_options[RW]

Public Class Methods

new(protocol_options) click to toggle source
# File lib/dalli/protocol/value_serializer.rb, line 24
def initialize(protocol_options)
  @serialization_options =
    DEFAULTS.merge(protocol_options.select { |k, _| OPTIONS.include?(k) })
end

Public Instance Methods

filter_argument_error(err) click to toggle source
# File lib/dalli/protocol/value_serializer.rb, line 62
def filter_argument_error(err)
  raise err unless ARGUMENT_ERR_REGEXP.match?(err.message)

  raise UnmarshalError, "Unable to unmarshal value: #{err.message}"
end
filter_name_error(err) click to toggle source
# File lib/dalli/protocol/value_serializer.rb, line 68
def filter_name_error(err)
  raise err unless err.message.include?(NAME_ERR_STR)

  raise UnmarshalError, "Unable to unmarshal value: #{err.message}"
end
filter_type_error(err) click to toggle source
# File lib/dalli/protocol/value_serializer.rb, line 56
def filter_type_error(err)
  raise err unless TYPE_ERR_REGEXP.match?(err.message)

  raise UnmarshalError, "Unable to unmarshal value: #{err.message}"
end
retrieve(value, bitflags) click to toggle source

rubocop:enable Layout/LineLength

# File lib/dalli/protocol/value_serializer.rb, line 45
def retrieve(value, bitflags)
  serialized = (bitflags & FLAG_SERIALIZED) != 0
  serialized ? serializer.load(value) : value
rescue TypeError => e
  filter_type_error(e)
rescue ArgumentError => e
  filter_argument_error(e)
rescue NameError => e
  filter_name_error(e)
end
serialize_value(value) click to toggle source
# File lib/dalli/protocol/value_serializer.rb, line 78
def serialize_value(value)
  serializer.dump(value)
rescue Timeout::Error => e
  raise e
rescue StandardError => e
  # Serializing 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(e.message)
  exc.set_backtrace e.backtrace
  raise exc
end
serializer() click to toggle source
# File lib/dalli/protocol/value_serializer.rb, line 74
def serializer
  @serialization_options[:serializer]
end
store(value, req_options, bitflags) click to toggle source
# File lib/dalli/protocol/value_serializer.rb, line 29
def store(value, req_options, bitflags)
  do_serialize = !(req_options && req_options[:raw])
  store_value = do_serialize ? serialize_value(value) : value.to_s
  bitflags |= FLAG_SERIALIZED if do_serialize
  [store_value, bitflags]
end