はじめに
Rails 6 に追加されそうな新機能を試す第33段。 今回は、 cache versioning error
編です。
Rails 6 では、 cache versioning をサポートしていない cache store を使っていると RuntimeError を raise するようになりました。
Ruby 2.6.3, Rails 6.0.0.rc1 で確認しました。Rails 6.0.0.rc1 は gem install rails --prerelease
でインストールできます。
$ rails --version
Rails 6.0.0.rc1
今回は、cache store に dalli を使います。
memcached は、1.5.16 を使ってます。
rails プロジェクトを作る
$ rails new rails6_0_0rc1
$ cd rails6_0_0rc1
dalli を Gemfile に追加する
今回は、RuntimeError を raise させるためにわざと古い 2.7.9 の dalli を使います。
...
gem 'dalli', '2.7.9'
bundle install を実行する
$ bundle install
cache の設定をする
config/environments/development.rb
に、cache の設定を追加します。
ここで、 'memcached'
は memcached が動作しているサーバーのホスト名です。
...
config.cache_store = :dalli_store, 'memcached'
end
rails console を実行する
rails c
を実行すると、RuntimeError が raise されます。
$ bin/rails c
/usr/local/bundle/gems/activerecord-6.0.0.rc1/lib/active_record/railtie.rb:104:in `block (3 levels) in <class:Railtie>':
You're using a cache store that doesn't support native cache versioning.
Your best option is to upgrade to a newer version of ActiveSupport::Cache::DalliStore
that supports cache versioning (ActiveSupport::Cache::DalliStore.supports_cache_versioning? #=> true).
Next best, switch to a different cache store that does support cache versioning:
https://guides.rubyonrails.org/caching_with_rails.html#cache-stores.
To keep using the current cache store, you can turn off cache versioning entirely:
config.active_record.cache_versioning = false`
最新版の dalli では?
dalli を最新版にして、同じことをするとエラーが出ません。
なお、dalli は cache versioning を 2.7.9 からサポートしているようです。
ですが、Rails 6 の RuntimeError を出さないように対応しているバージョンは、 2.7.10 以降となります。
- rails 5.2 cache_versioning support
- Advertise support for "cache versioning"
- Cache Invalidation Complexity: Rails 5.2 and Dalli Cache Store
試したソース
試したソースは以下にあります。
https://github.com/suketa/rails6_0_0rc1/tree/try033_cache_error