New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support replacing cache compressor #48451
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "entry" | ||
|
||
module ActiveSupport | ||
module Cache | ||
class Coder # :nodoc: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be worth making this public to provide a migration path from the |
||
def initialize(serializer, compressor, legacy_serializer: false) | ||
@serializer = serializer | ||
@compressor = compressor | ||
@legacy_serializer = legacy_serializer | ||
end | ||
|
||
def dump(entry) | ||
return @serializer.dump(entry) if @legacy_serializer | ||
|
||
dump_compressed(entry, Float::INFINITY) | ||
end | ||
|
||
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 | ||
|
||
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)..) | ||
|
||
payload = @compressor.inflate(payload) if type & COMPRESSED_FLAG > 0 | ||
|
||
if string_encoding = STRING_ENCODINGS[type & ~COMPRESSED_FLAG] | ||
value = payload.force_encoding(string_encoding) | ||
else | ||
value = @serializer.load(payload) | ||
end | ||
|
||
Cache::Entry.new(value, version: version, expires_at: expires_at) | ||
end | ||
|
||
private | ||
SIGNATURE = "\x00\x11".b.freeze | ||
|
||
OBJECT_DUMP_TYPE = 0x01 | ||
|
||
STRING_ENCODINGS = { | ||
0x02 => Encoding::UTF_8, | ||
0x03 => Encoding::BINARY, | ||
0x04 => Encoding::US_ASCII, | ||
} | ||
|
||
COMPRESSED_FLAG = 0x80 | ||
|
||
PACKED_TEMPLATE = "CEl<" | ||
PACKED_TYPE_TEMPLATE = "@#{SIGNATURE.bytesize}C" | ||
PACKED_EXPIRES_AT_TEMPLATE = "@#{[0].pack(PACKED_TYPE_TEMPLATE).bytesize}E" | ||
PACKED_VERSION_LENGTH_TEMPLATE = "@#{[0].pack(PACKED_EXPIRES_AT_TEMPLATE).bytesize}l<" | ||
PACKED_VERSION_INDEX = [0].pack(PACKED_VERSION_LENGTH_TEMPLATE).bytesize | ||
|
||
MARSHAL_SIGNATURE = "\x04\x08".b.freeze | ||
|
||
def signature?(dumped) | ||
dumped.is_a?(String) && dumped.start_with?(SIGNATURE) | ||
end | ||
|
||
def type_for_string(value) | ||
STRING_ENCODINGS.key(value.encoding) if value.instance_of?(String) | ||
end | ||
|
||
def try_compress(string, threshold) | ||
if @compressor && string.bytesize >= threshold | ||
compressed = @compressor.deflate(string) | ||
compressed if compressed.bytesize < string.bytesize | ||
end | ||
end | ||
|
||
def dump_version(version) | ||
if version.encoding != Encoding::UTF_8 || version.start_with?(MARSHAL_SIGNATURE) | ||
Marshal.dump(version) | ||
else | ||
version.b | ||
end | ||
end | ||
|
||
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 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jonathanhefner Hi there! 👋🏻
Sorry to ping you on an already merged PR, but hopefully you'll see this! 🤞🏻
This change is breaking some stuff here at Shopify. We have some places that initialize a memory store this way:
I understand I can fix our code by changing it to this:
However, do you think this code could stay backwards compatible and allow setting a
coder
as a symbol?One could for example add this line:
(Note this line existed as-is at LOC 272 before this PR.)
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidstosik we should fix our code. Passing a coder as a symbol make no sense.
I can help you if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kinda misread your message. I see what passing a symbol was supported for a bit, I need to check if it was supported in 7.0. If it wasn't then we should fix our code. If it was we should fix Rails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so this wasn't supported in 7.0, this was added in 7.1 alpha and removed before release, so let's fix the Shopify side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference, the way to achieve this behavior in 7.0 was to specify
coder: nil
, and that still works in 7.1. However, I don't think that is documented anywhere. Is that something we officially support? If so, I can add documentation and a regression test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would make sense. It's a fairly niche use case but can be useful with MemoryStore as a perf optimization if you know your values aren't mutable, or with MemcachedStore if you wish to use a serializer at the Dalli level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@casperisfine Thanks for digging that up! I already have a fix for our code so I think ignoring my message above is okay. 👍🏻