Class: ActiveSupport::Cache::MemcachedStore::Codec

Inherits:
Object
  • Object
show all
Defined in:
lib/active_support/cache/memcached_store.rb

Constant Summary collapse

SERIALIZED_FLAG =

use dalli compatible flags

0x1
COMPRESSED_FLAG =
0x2
RAW_FLAG =

Older versions of this gem would use 0 for the flags whether or not the value was marshal dumped. By setting this flag, we can tell if it were set with an older version for backwards compatible decoding.

0x10

Instance Method Summary collapse

Constructor Details

#initialize(serializer: Marshal, compressor: nil) ⇒ Codec

Returns a new instance of Codec.



26
27
28
29
# File 'lib/active_support/cache/memcached_store.rb', line 26

def initialize(serializer: Marshal, compressor: nil)
  @serializer = serializer
  @compressor = compressor
end

Instance Method Details

#decode(_key, value, flags) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_support/cache/memcached_store.rb', line 44

def decode(_key, value, flags)
  if (flags & COMPRESSED_FLAG) != 0
    value = @compressor.decompress(value)
  end

  if (flags & SERIALIZED_FLAG) != 0
    @serializer.load(value)
  elsif flags == 0 # legacy cache value
    @serializer.load(value) rescue value
  else
    value
  end
end

#encode(_key, value, flags) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_support/cache/memcached_store.rb', line 31

def encode(_key, value, flags)
  unless value.is_a?(String)
    flags |= SERIALIZED_FLAG
    value = @serializer.dump(value)
  end
  if @compressor
    flags |= COMPRESSED_FLAG
    value = @compressor.compress(value)
  end
  flags |= RAW_FLAG if flags == 0
  [value, flags]
end