Class: Dalli::Protocol::ValueCompressor
- Inherits:
-
Object
- Object
- Dalli::Protocol::ValueCompressor
- Defined in:
- lib/dalli/protocol/value_compressor.rb
Overview
Dalli::Protocol::ValueCompressor compartmentalizes the logic for managing compression and decompression of stored values. It manages interpreting relevant options from both client and request, determining whether to compress/decompress on store/retrieve, and processes bitflags as necessary.
Constant Summary collapse
- DEFAULTS =
{ compress: true, compressor: ::Dalli::Compressor, # min byte size to attempt compression compression_min_size: 4 * 1024 # 4K }.freeze
- OPTIONS =
DEFAULTS.keys.freeze
- FLAG_COMPRESSED =
www.hjp.at/zettel/m/memcached_flags.rxml Looks like most clients use bit 1 to indicate gzip compression.
0x2
Instance Method Summary collapse
- #compress_by_default? ⇒ Boolean
-
#compress_value?(value, req_options) ⇒ Boolean
Checks whether we should apply compression when serializing a value based on the specified options.
- #compression_min_size ⇒ Object
- #compressor ⇒ Object
-
#initialize(client_options) ⇒ ValueCompressor
constructor
A new instance of ValueCompressor.
- #retrieve(value, bitflags) ⇒ Object
- #store(value, req_options, bitflags) ⇒ Object
Constructor Details
#initialize(client_options) ⇒ ValueCompressor
Returns a new instance of ValueCompressor.
27 28 29 30 |
# File 'lib/dalli/protocol/value_compressor.rb', line 27 def initialize() @compression_options = DEFAULTS.merge(.slice(*OPTIONS)) end |
Instance Method Details
#compress_by_default? ⇒ Boolean
51 52 53 |
# File 'lib/dalli/protocol/value_compressor.rb', line 51 def compress_by_default? @compression_options[:compress] end |
#compress_value?(value, req_options) ⇒ Boolean
Checks whether we should apply compression when serializing a value based on the specified options. Returns false unless the value is greater than the minimum compression size. Otherwise returns based on a method-level option if specified, falling back to the server default.
68 69 70 71 72 73 |
# File 'lib/dalli/protocol/value_compressor.rb', line 68 def compress_value?(value, ) return false unless value.bytesize >= compression_min_size return compress_by_default? unless && ![:compress].nil? [:compress] end |
#compression_min_size ⇒ Object
59 60 61 |
# File 'lib/dalli/protocol/value_compressor.rb', line 59 def compression_min_size @compression_options[:compression_min_size] end |
#compressor ⇒ Object
55 56 57 |
# File 'lib/dalli/protocol/value_compressor.rb', line 55 def compressor @compression_options[:compressor] end |
#retrieve(value, bitflags) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dalli/protocol/value_compressor.rb', line 40 def retrieve(value, bitflags) compressed = bitflags.anybits?(FLAG_COMPRESSED) compressed ? compressor.decompress(value) : value # TODO: We likely want to move this rescue into the Dalli::Compressor / Dalli::GzipCompressor # itself, since not all compressors necessarily use Zlib. For now keep it here, so the behavior # of custom compressors doesn't change. rescue Zlib::Error raise UnmarshalError, "Unable to uncompress value: #{$ERROR_INFO.}" end |
#store(value, req_options, bitflags) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/dalli/protocol/value_compressor.rb', line 32 def store(value, , bitflags) do_compress = compress_value?(value, ) store_value = do_compress ? compressor.compress(value) : value bitflags |= FLAG_COMPRESSED if do_compress [store_value, bitflags] end |