Rails のキャッシュストアとセッションストアに Redis を使う
Ruby 2.7.1 + Rails v6.0.3.2 + Redis
Gemfile
... gem 'redis' gem 'hiredis' ...
開発環境用の設定
config/environments/development.rb
# Enable/disable caching. By default caching is disabled. # Run rails dev:cache to toggle caching. if Rails.root.join('tmp', 'caching-dev.txt').exist? config.action_controller.perform_caching = true config.action_controller.enable_fragment_cache_logging = true #-------------------------------------------------------------------- # config.cache_store = :memory_store config.cache_store = :redis_cache_store, { url: 'redis://localhost:6379', expires_in: 30.minutes, namespace: 'foo_cache', } config.session_store :cache_store, key: "foo_session", expire_after: 1.day.to_i #-------------------------------------------------------------------- config.public_file_server.headers = { 'Cache-Control' => "public, max-age=#{2.days.to_i}" } else config.action_controller.perform_caching = false config.cache_store = :null_store end
キャッシュを有効にする。
$ ./bin/rails dev:cache Development mode is now being cached.