Sha256: 7a14700de9f5cc0716029b0ee293b2e8e469444a917ea0e0986f362c0cd5000e

Contents?: true

Size: 1.78 KB

Versions: 18

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

require 'forwardable'

module Dalli
  module Protocol
    ##
    # 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.
    ##
    class ValueMarshaller
      extend Forwardable

      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

      def_delegators :@value_serializer, :serializer
      def_delegators :@value_compressor, :compressor, :compression_min_size, :compress_by_default?

      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

      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

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

      def value_max_bytes
        @marshal_options[:value_max_bytes]
      end

      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
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
dalli-3.2.8 lib/dalli/protocol/value_marshaller.rb
dalli-3.2.7 lib/dalli/protocol/value_marshaller.rb
dalli-3.2.6 lib/dalli/protocol/value_marshaller.rb
dalli-3.2.5 lib/dalli/protocol/value_marshaller.rb
dalli-3.2.4 lib/dalli/protocol/value_marshaller.rb
dalli-3.2.3 lib/dalli/protocol/value_marshaller.rb
dalli-3.2.2 lib/dalli/protocol/value_marshaller.rb
dalli-3.2.1 lib/dalli/protocol/value_marshaller.rb
dalli-3.2.0 lib/dalli/protocol/value_marshaller.rb
dalli-3.1.6 lib/dalli/protocol/value_marshaller.rb
dalli-3.1.5 lib/dalli/protocol/value_marshaller.rb
dalli-3.1.4 lib/dalli/protocol/value_marshaller.rb
dalli-3.1.3 lib/dalli/protocol/value_marshaller.rb
dalli-3.1.2 lib/dalli/protocol/value_marshaller.rb
dalli-3.1.1 lib/dalli/protocol/value_marshaller.rb
dalli-3.1.0 lib/dalli/protocol/value_marshaller.rb
dalli-3.0.6 lib/dalli/protocol/value_marshaller.rb
dalli-3.0.5 lib/dalli/protocol/value_marshaller.rb