Class: ActiveSupport::Cache::Coder
Do not use. This class is for internal use only.
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Inherits: | Object |
Defined in: | activesupport/lib/active_support/cache/coder.rb |
Constant Summary
-
COMPRESSED_FLAG =
# File 'activesupport/lib/active_support/cache/coder.rb', line 760x80
-
MARSHAL_SIGNATURE =
# File 'activesupport/lib/active_support/cache/coder.rb', line 84"\x04\x08".b.freeze
-
OBJECT_DUMP_TYPE =
# File 'activesupport/lib/active_support/cache/coder.rb', line 680x01
-
PACKED_EXPIRES_AT_TEMPLATE =
# File 'activesupport/lib/active_support/cache/coder.rb', line 80"@#{[0].pack(PACKED_TYPE_TEMPLATE).bytesize}E"
-
PACKED_TEMPLATE =
# File 'activesupport/lib/active_support/cache/coder.rb', line 78"CEl<"
-
PACKED_TYPE_TEMPLATE =
# File 'activesupport/lib/active_support/cache/coder.rb', line 79"@#{SIGNATURE.bytesize}C"
-
PACKED_VERSION_INDEX =
# File 'activesupport/lib/active_support/cache/coder.rb', line 82[0].pack(PACKED_VERSION_LENGTH_TEMPLATE).bytesize
-
PACKED_VERSION_LENGTH_TEMPLATE =
# File 'activesupport/lib/active_support/cache/coder.rb', line 81"@#{[0].pack(PACKED_EXPIRES_AT_TEMPLATE).bytesize}l<"
-
SIGNATURE =
# File 'activesupport/lib/active_support/cache/coder.rb', line 66"\x00\x11".b.freeze
-
STRING_DESERIALIZERS =
# File 'activesupport/lib/active_support/cache/coder.rb', line 96STRING_ENCODINGS.transform_values { |encoding| StringDeserializer.new(encoding) }
-
STRING_ENCODINGS =
# File 'activesupport/lib/active_support/cache/coder.rb', line 70{ 0x02 => Encoding::UTF_8, 0x03 => Encoding::BINARY, 0x04 => Encoding::US_ASCII, }
Class Method Summary
Instance Method Summary
Constructor Details
.new(serializer, compressor, legacy_serializer: false) ⇒ Coder
# File 'activesupport/lib/active_support/cache/coder.rb', line 8
def initialize(serializer, compressor, legacy_serializer: false) @serializer = serializer @compressor = compressor @legacy_serializer = legacy_serializer end
Instance Method Details
#dump(entry)
[ GitHub ]# File 'activesupport/lib/active_support/cache/coder.rb', line 14
def dump(entry) return @serializer.dump(entry) if @legacy_serializer dump_compressed(entry, Float::INFINITY) end
#dump_compressed(entry, threshold)
[ GitHub ]# File 'activesupport/lib/active_support/cache/coder.rb', line 20
def dump_compressed(entry, threshold) return @serializer.dump_compressed(entry, threshold) if @legacy_serializer # If value is a string with a supported encoding, use it as the payload # instead of passing it through the serializer. if type = type_for_string(entry.value) payload = entry.value.b else type = OBJECT_DUMP_TYPE payload = @serializer.dump(entry.value) end if compressed = try_compress(payload, threshold) payload = compressed type = type | COMPRESSED_FLAG end expires_at = entry.expires_at || -1.0 version = dump_version(entry.version) if entry.version version_length = version&.bytesize || -1 packed = SIGNATURE.b packed << [type, expires_at, version_length].pack(PACKED_TEMPLATE) packed << version if version packed << payload end
#dump_version(version)
[ GitHub ]#load(dumped)
[ GitHub ]# File 'activesupport/lib/active_support/cache/coder.rb', line 48
def load(dumped) return @serializer.load(dumped) if !signature?(dumped) type = dumped.unpack1(PACKED_TYPE_TEMPLATE) expires_at = dumped.unpack1(PACKED_EXPIRES_AT_TEMPLATE) version_length = dumped.unpack1(PACKED_VERSION_LENGTH_TEMPLATE) expires_at = nil if expires_at < 0 version = load_version(dumped.byteslice(PACKED_VERSION_INDEX, version_length)) if version_length >= 0 payload = dumped.byteslice((PACKED_VERSION_INDEX + [version_length, 0].max)..) compressor = @compressor if type & COMPRESSED_FLAG > 0 serializer = STRING_DESERIALIZERS[type & ~COMPRESSED_FLAG] || @serializer LazyEntry.new(serializer, compressor, payload, version: version, expires_at: expires_at) end
#load_version(dumped_version)
[ GitHub ]# File 'activesupport/lib/active_support/cache/coder.rb', line 144
def load_version(dumped_version) if dumped_version.start_with?(MARSHAL_SIGNATURE) Marshal.load(dumped_version) else dumped_version.force_encoding(Encoding::UTF_8) end end
#signature?(dumped) ⇒ Boolean
#try_compress(string, threshold)
[ GitHub ]# File 'activesupport/lib/active_support/cache/coder.rb', line 129
def try_compress(string, threshold) if @compressor && string.bytesize >= threshold compressed = @compressor.deflate(string) compressed if compressed.bytesize < string.bytesize end end
#type_for_string(value)
[ GitHub ]# File 'activesupport/lib/active_support/cache/coder.rb', line 125
def type_for_string(value) STRING_ENCODINGS.key(value.encoding) if value.instance_of?(String) end