Method: ActiveSupport::Cache::Entry#compressed

Defined in:
lib/active_support/cache/entry.rb

#compressed(compress_threshold) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_support/cache/entry.rb', line 76

def compressed(compress_threshold)
  return self if compressed?

  case @value
  when nil, true, false, Numeric
    uncompressed_size = 0
  when String
    uncompressed_size = @value.bytesize
  else
    serialized = Marshal.dump(@value)
    uncompressed_size = serialized.bytesize
  end

  if uncompressed_size >= compress_threshold
    serialized ||= Marshal.dump(@value)
    compressed = Zlib::Deflate.deflate(serialized)

    if compressed.bytesize < uncompressed_size
      return Entry.new(compressed, compressed: true, expires_at: expires_at, version: version)
    end
  end
  self
end