Class: Dalli::Protocol::ValueMarshaller

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/dalli/protocol/value_marshaller.rb

Overview

Dalli::Protocol::ValueMarshaller compartmentalizes the logic for marshalling and unmarshalling unstructured data (values) to Memcached. It also enforces limits on the maximum size of marshalled data.

Constant Summary collapse

DEFAULTS =
{
  # max size of value in bytes (default is 1 MB, can be overriden with "memcached -I <size>")
  value_max_bytes: 1024 * 1024
}.freeze
OPTIONS =
DEFAULTS.keys.freeze

Instance Method Summary collapse

Constructor Details

#initialize(client_options) ⇒ ValueMarshaller

Returns a new instance of ValueMarshaller.



25
26
27
28
29
30
31
# File 'lib/dalli/protocol/value_marshaller.rb', line 25

def initialize(client_options)
  @value_serializer = ValueSerializer.new(client_options)
  @value_compressor = ValueCompressor.new(client_options)

  @marshal_options =
    DEFAULTS.merge(client_options.select { |k, _| OPTIONS.include?(k) })
end

Instance Method Details

#error_if_over_max_value_bytes(key, value) ⇒ Object



51
52
53
54
55
56
# File 'lib/dalli/protocol/value_marshaller.rb', line 51

def error_if_over_max_value_bytes(key, value)
  return if value.bytesize <= value_max_bytes

  message = "Value for #{key} over max size: #{value_max_bytes} <= #{value.bytesize}"
  raise Dalli::ValueOverMaxSize, message
end

#retrieve(value, flags) ⇒ Object



42
43
44
45
# File 'lib/dalli/protocol/value_marshaller.rb', line 42

def retrieve(value, flags)
  value = @value_compressor.retrieve(value, flags)
  @value_serializer.retrieve(value, flags)
end

#store(key, value, options = nil) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/dalli/protocol/value_marshaller.rb', line 33

def store(key, value, options = nil)
  bitflags = 0
  value, bitflags = @value_serializer.store(value, options, bitflags)
  value, bitflags = @value_compressor.store(value, options, bitflags)

  error_if_over_max_value_bytes(key, value)
  [value, bitflags]
end

#value_max_bytesObject



47
48
49
# File 'lib/dalli/protocol/value_marshaller.rb', line 47

def value_max_bytes
  @marshal_options[:value_max_bytes]
end