Skip to content

Commit ff3c8de

Browse files
Merge pull request rails#48449 from jonathanhefner/message_pack-cache-coder-option
Use cache `:coder` option to specify `:message_pack`
2 parents 3f0971d + bd15567 commit ff3c8de

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

activesupport/CHANGELOG.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@
7070
*Jonathan Hefner*
7171

7272
* A new `7.1` cache format is available which includes an optimization for
73-
bare string values such as view fragments. The `:message_pack` cache format
74-
has also been modified to include this optimization.
73+
bare string values such as view fragments.
7574

7675
The `7.1` cache format is used by default for new apps, and existing apps
7776
can enable the format by setting `config.load_defaults 7.1` or by setting
@@ -84,18 +83,25 @@
8483
read caches from upgraded servers, leave the cache format unchanged on the
8584
first deploy, then enable the `7.1` cache format on a subsequent deploy.
8685

86+
The new `:message_pack` cache coder also includes this optimization.
87+
8788
*Jonathan Hefner*
8889

89-
* `config.active_support.cache_format_version` now accepts `:message_pack` as
90-
an option. `:message_pack` can reduce cache entry sizes and improve
90+
* The `:coder` option for Active Support cache stores now supports a
91+
`:message_pack` value:
92+
93+
```ruby
94+
config.cache_store = :redis_cache_store, { coder: :message_pack }
95+
```
96+
97+
The `:message_pack` coder can reduce cache entry sizes and improve
9198
performance, but requires the [`msgpack` gem](https://rubygems.org/gems/msgpack)
9299
(>= 1.7.0).
93100

94-
Cache entries written using the `6.1`, `7.0`, or `7.1` cache formats can be read
95-
when using the `:message_pack` cache format. Additionally, cache entries
96-
written using the `:message_pack` cache format can now be read when using
97-
the `6.1`, `7.0`, or `7.1` cache formats. These behaviors makes it easy to migrate
98-
between formats without invalidating the entire cache.
101+
The `:message_pack` coder can read cache entries written by the default
102+
coder, and the default coder can now read entries written by the
103+
`:message_pack` coder. These behaviors make it easy to migrate between
104+
coders without invalidating the entire cache.
99105

100106
*Jonathan Hefner*
101107

activesupport/lib/active_support/cache.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ def retrieve_pool_options(options)
238238
# with a custom one. The +coder+ must respond to +dump+ and +load+.
239239
# Using a custom coder disables automatic compression.
240240
#
241+
# Alternatively, you can specify <tt>coder: :message_pack</tt> to use a
242+
# preconfigured coder based on ActiveSupport::MessagePack that supports
243+
# automatic compression and includes a fallback mechanism to load old
244+
# cache entries from the default coder. However, this option requires
245+
# the +msgpack+ gem.
246+
#
241247
# Any other specified options are treated as default options for the
242248
# relevant cache operations, such as #read, #write, and #fetch.
243249
def initialize(options = nil)
@@ -246,6 +252,7 @@ def initialize(options = nil)
246252
@options[:compress_threshold] = DEFAULT_COMPRESS_LIMIT unless @options.key?(:compress_threshold)
247253

248254
@coder = @options.delete(:coder) { default_coder } || NullCoder
255+
@coder = Cache::SerializerWithFallback[@coder] if @coder.is_a?(Symbol)
249256
@coder_supports_compression = @coder.respond_to?(:dump_compressed)
250257
end
251258

activesupport/test/cache/behaviors/cache_store_format_version_behavior.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
module CacheStoreFormatVersionBehavior
66
extend ActiveSupport::Concern
77

8-
FORMAT_VERSIONS = [6.1, 7.0, 7.1, :message_pack]
8+
FORMAT_VERSIONS = [6.1, 7.0, 7.1]
99

1010
included do
1111
test "format version affects default coder" do

guides/source/configuring.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,15 +2326,11 @@ The default value depends on the `config.load_defaults` target version:
23262326
#### `config.active_support.cache_format_version`
23272327
23282328
Specifies which serialization format to use for the cache. Possible values are
2329-
`6.1`, `7.0`, `7.1`, and `:message_pack`.
2329+
`6.1`, `7.0`, and `7.1`.
23302330
2331-
The `6.1`, `7.0`, and `7.1` formats all use `Marshal`, but `7.0` uses a more
2332-
efficient representation for cache entries, and `7.1` includes an additional
2333-
optimization for bare string values such as view fragments.
2334-
2335-
The `:message_pack` format uses `ActiveSupport::MessagePack`, and may further
2336-
reduce cache entry sizes and improve performance, but requires the
2337-
[`msgpack` gem](https://rubygems.org/gems/msgpack).
2331+
The `6.1`, `7.0`, and `7.1` formats all use `Marshal` for the default coder, but
2332+
`7.0` uses a more efficient representation for cache entries, and `7.1` includes
2333+
an additional optimization for bare string values such as view fragments.
23382334
23392335
All formats are backward and forward compatible, meaning cache entries written
23402336
in one format can be read when using another format. This behavior makes it

railties/test/application/initializers/frameworks_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,7 @@ class Post < ActiveRecord::Base
402402
rails %w(db:migrate)
403403

404404
add_to_config <<~RUBY
405-
config.cache_store = :file_store, #{app_path("tmp/cache").inspect}
406-
config.active_support.cache_format_version = :message_pack
405+
config.cache_store = :file_store, #{app_path("tmp/cache").inspect}, { coder: :message_pack }
407406
RUBY
408407

409408
require "#{app_path}/config/environment"

0 commit comments

Comments
 (0)