Reputation: 26075
I noticed that my queries aren't being cached. I ran bundle exec rake middleware | grep Cache
and it's empty. I tried adding the following in config/application.rb
:
config.middleware.use ActiveRecord::QueryCache
However, I'm getting:
rake aborted!
ArgumentError: wrong number of arguments (given 1, expected 0)
/home/l/.rvm/gems/ruby-2.6.5/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/stack.rb:37:in `initialize'
/home/l/.rvm/gems/ruby-2.6.5/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/stack.rb:37:in `new'
/home/l/.rvm/gems/ruby-2.6.5/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/stack.rb:37:in `build'
/home/l/.rvm/gems/ruby-2.6.5/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/stack.rb:131:in `block in build'
/home/l/.rvm/gems/ruby-2.6.5/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/stack.rb:127:in `each'
/home/l/.rvm/gems/ruby-2.6.5/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/stack.rb:127:in `inject'
/home/l/.rvm/gems/ruby-2.6.5/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/stack.rb:127:in `build'
/home/l/.rvm/gems/ruby-2.6.5/gems/railties-6.0.2.1/lib/rails/engine.rb:512:in `block in app'
/home/l/.rvm/gems/ruby-2.6.5/gems/railties-6.0.2.1/lib/rails/engine.rb:508:in `synchronize'
/home/l/.rvm/gems/ruby-2.6.5/gems/railties-6.0.2.1/lib/rails/engine.rb:508:in `app'
/home/l/.rvm/gems/ruby-2.6.5/gems/railties-6.0.2.1/lib/rails/application/finisher.rb:97:in `block in <module:Finisher>'
/home/l/.rvm/gems/ruby-2.6.5/gems/railties-6.0.2.1/lib/rails/initializable.rb:32:in `instance_exec'
/home/l/.rvm/gems/ruby-2.6.5/gems/railties-6.0.2.1/lib/rails/initializable.rb:32:in `run'
/home/l/.rvm/gems/ruby-2.6.5/gems/railties-6.0.2.1/lib/rails/initializable.rb:61:in `block in run_initializers'
/home/l/.rvm/gems/ruby-2.6.5/gems/railties-6.0.2.1/lib/rails/initializable.rb:60:in `run_initializers'
/home/l/.rvm/gems/ruby-2.6.5/gems/railties-6.0.2.1/lib/rails/application.rb:363:in `initialize!'
/mnt/d/Dev/temp/blog/config/environment.rb:5:in `<main>'
/home/l/.rvm/gems/ruby-2.6.5/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
/home/l/.rvm/gems/ruby-2.6.5/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
/home/l/.rvm/gems/ruby-2.6.5/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/home/l/.rvm/gems/ruby-2.6.5/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
/home/l/.rvm/gems/ruby-2.6.5/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
/home/l/.rvm/gems/ruby-2.6.5/gems/zeitwerk-2.2.2/lib/zeitwerk/kernel.rb:23:in `require'
/home/l/.rvm/gems/ruby-2.6.5/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:325:in `block in require'
/home/l/.rvm/gems/ruby-2.6.5/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:291:in `load_dependency'
/home/l/.rvm/gems/ruby-2.6.5/gems/activesupport-6.0.2.1/lib/active_support/dependencies.rb:325:in `require'
/home/l/.rvm/gems/ruby-2.6.5/gems/railties-6.0.2.1/lib/rails/application.rb:339:in `require_environment!'
/home/l/.rvm/gems/ruby-2.6.5/gems/railties-6.0.2.1/lib/rails/application.rb:515:in `block in run_tasks_blocks'
/home/l/.rvm/gems/ruby-2.6.5/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
/home/l/.rvm/gems/ruby-2.6.5/bin/ruby_executable_hooks:24:in `eval'
/home/l/.rvm/gems/ruby-2.6.5/bin/ruby_executable_hooks:24:in `<main>'
Tasks: TOP => middleware => environment
(See full trace by running task with --trace)
I thought QueryCache was supposed to be enabled by default? Why isn't it enabled? How can I enable it?
I'm using WSL.
Upvotes: 0
Views: 661
Reputation: 3352
The query cache doesn't work like that in the rails. It is applied only inside the request and cache is destroyed at the end of the request, it does not stay alive in between requests.
See the rails guide for the SQL caching https://guides.rubyonrails.org/caching_with_rails.html#sql-caching
However, it's important to note that query caches are created at the start of an action and destroyed at the end of that action and thus persist only for the duration of the action. If you'd like to store query results in a more persistent fashion, you can with low level caching.
If you want to persist your query cache in between requests, you have to implement it by yourself using low level caching https://guides.rubyonrails.org/caching_with_rails.html#low-level-caching
Upvotes: 1