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.



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

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

Instance Method Details

#decode(_key, value, flags) ⇒ Object



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

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



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

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