1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activesupport/test/cache/coder_test.rb
Jean Boussier 3b71f3eb68 Implement an optimized Cache::Entry coder
Active Support's cache have for long been limited because
of its format. It directly serialize its `Entry` object with
`Marshal`, so any internal change might break the format.

The current shortcommings are:

  - The minimum entry overhead is quite ridiculous:
    `Marshal.dump(ActiveSupport::Cache::Entry.new("")).bytesize # => 107`
  - Only the internal `value` is compressed, but unless it's a String, to do so
    it first need to be serialized. So we end up with `Marshal.dump(Zlib.deflate(Marshal.dump(value)))`
    which is wasteful.
2021-04-25 08:27:58 +02:00

28 lines
1.1 KiB
Ruby

# frozen_string_literal: true
require_relative "../abstract_unit"
require "active_support/cache"
class CacheCoderTest < ActiveSupport::TestCase
def test_new_coder_can_read_legacy_payloads
entry = ActiveSupport::Cache::Entry.new("foobar", expires_in: 1.hour, version: "v42")
deserialized_entry = ActiveSupport::Cache::Coders::Rails70Coder.load(
ActiveSupport::Cache::Coders::Rails61Coder.dump(entry),
)
assert_equal entry.value, deserialized_entry.value
assert_equal entry.version, deserialized_entry.version
assert_equal entry.expires_at, deserialized_entry.expires_at
end
def test_legacy_coder_can_read_new_payloads
entry = ActiveSupport::Cache::Entry.new("foobar", expires_in: 1.hour, version: "v42")
deserialized_entry = ActiveSupport::Cache::Coders::Rails61Coder.load(
ActiveSupport::Cache::Coders::Rails70Coder.dump(entry),
)
assert_equal entry.value, deserialized_entry.value
assert_equal entry.version, deserialized_entry.version
assert_equal entry.expires_at, deserialized_entry.expires_at
end
end