Enabling QueryCache in ActiveJob (and/or other places)


We were doing some optimizations and noticed that the QueryCache is not being used in our worker process (resulting in some repeated DB calls).

class TestJob < ActiveJob::Base
  def perform
    puts ActiveRecord::Base.connection.query_cache_enabled
=> false

It looks like QueryCache is only enabled on each request via the ActiveSupport::Executor. Would it make sense to enable/disable the QueryCache around every job perform? Or wrap calls with the AS::Executor? Also I’d love to hear if there are other places where this would make sense (I’m thinking runner/rake task/console).

Tangentially, this is my first post on this list: many thanks to the Rails team for their continued work, support and leadership over the many many years! I hope you know that through Rails and your dedication to open source you have enabled the company that I work for to bring software solutions to non-profits that otherwise would not have had the possibility to.


Yeah, it would: rails/railtie.rb at 8a4192fa3d799b795ddd342e4eb1092825366dce · rails/rails · GitHub

(reloader.wrap does an executor.wrap internally)


Hopefully that’s at least a clue for you to investigate further… sometimes knowing that it’s supposed to work is enough to help track down why it’s not. :crossed_fingers:

That’s strange, the query cache is definitely enabled in Active Job in our app. We have tests making sure it is at GitHub. Could there be another gem or code in your application causing the query cache to be turned off for you?

1 Like

Thank you @eileencodes and @matthewd for your help! I confirmed with a clean setup that the QueryCache works in production with ActiveJob. I’ll have to investigate further to see what’s happening in our setup/in the development environment, and hopefully bring a productive contribution (to the docs :sweat_smile:).