Class: Dalli::Protocol::ValueMarshaller
- Inherits:
-
Object
- Object
- Dalli::Protocol::ValueMarshaller
- 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
- #error_if_over_max_value_bytes(key, value) ⇒ Object
-
#initialize(client_options) ⇒ ValueMarshaller
constructor
A new instance of ValueMarshaller.
- #retrieve(value, flags) ⇒ Object
- #store(key, value, options = nil) ⇒ Object
- #value_max_bytes ⇒ Object
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() @value_serializer = ValueSerializer.new() @value_compressor = ValueCompressor.new() @marshal_options = DEFAULTS.merge(.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 = "Value for #{key} over max size: #{value_max_bytes} <= #{value.bytesize}" raise Dalli::ValueOverMaxSize, 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, = nil) bitflags = 0 value, bitflags = @value_serializer.store(value, , bitflags) value, bitflags = @value_compressor.store(value, , bitflags) error_if_over_max_value_bytes(key, value) [value, bitflags] end |
#value_max_bytes ⇒ Object
47 48 49 |
# File 'lib/dalli/protocol/value_marshaller.rb', line 47 def value_max_bytes @marshal_options[:value_max_bytes] end |