How can I disable Rails 3.1's ActiveRecord Query Caching?

Hey guys,

As most of you know, Rails 3.1 introduces query caching for
ActiveRecord. Great change all-in-all, but for those of us with IT
department mandates for using MySQL (yeah, yeah, I know...), it's kind
of a mixed bag.

I need to disable query caching in a project I'm building based on
3.1. I've tried a couple different approaches, both without any
success. I was hoping some one could enlighten me on how to do that,
exactly.

The first approach I tried was to create an initializer (config/
initializer/active_record.rb) and include this code:
ActiveRecord::Base.connection.disable_query_cache!

(Documented at http://edgeapi.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/QueryCache.html
without comments or examples, unsurprisingly).

This doesn't appear to work as I'm still seeing prepared statement
caching going on in my log file:
Started GET "/dashboard" for 127.0.0.1 at 2011-08-06 22:43:07 -0600
  Processing by DashboardController#index as HTML
  User Load (13.4ms) SELECT "users".* FROM "users" WHERE "users"."id"
= $1 LIMIT 1 [["id", 1]]
Rendered dashboard/index.html.erb within layouts/application (0.3ms)
  CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1
LIMIT 1

So my next attempt was to place it in config/environments/
development.rb (I'm of course trying to get this to work in
development mode before doing it in production, obviously):

MyApp::Application.configure do
  # ... other stuff ...
  config.active_record.disable_query_cache!
end

Same deal. I tried also changing that to
config.active_record.cache_queries = false - no such luck (and that
was just a long shot guess).

Can anyone explain to me exactly how I'm supposed to turn this off?
Thanks.

The ActiveRecord::QueryCache middleware turns on query caching during
for each request. Remove it from the middleware stack and you should
be ok ( http://guides.rubyonrails.org/configuring.html#configuring-middleware
)

Fred

As usual, Fred - you're the man. Thanks a ton. One other question,
however: how exactly can I remove that from the middleware stack?
Looking at http://api.rubyonrails.org/classes/ActionDispatch/MiddlewareStack.html,
I'm not seeing any method that I can use to "subtract" this from the
stack. My understanding is that I'd do it something like this:

# app/config/environments/development.rb
MyApp::Application.configure do
  # ... other stuff ...
  config.middleware.??? ActiveRecord::QueryCache
end

What method should I use in place of ??? ? (damn that's a lot of
question marks). The only thing I can guess at this point would be
using swap and calling nil on that, to swap that middleware with
"nil", but that seems really...off:

config.middleware.swap ActiveRecord::QueryCache, nil

In theory that would work, but is there a cleaner way to do it?

Any input here? Thanks again!

http://edgeguides.rubyonrails.org/configuring.html#configuring-middleware

HTH,

Unfortunately Hassan that doesn't really help -- that article doesn't
explain how to *remove* things from the stack. Does anyone know a
clean(er) way to do this than what I've already theorized above?

[quoting that source]

They can also be removed from the stack completely:
(example)
  config.middleware.delete ActionDispatch::BestStandardsSupport

My apologies Hassan, I looked at it and didn't realize that it was the
EDGE version of the guide and thought I was looking at the same
documentation that we'd already been through. Thanks!

Thanks again for your help, Hassan. Unfortunately it's still caching
queries. I've tried both approaches in this thread in isolation, and
together, and regardless, the query cache is still being used.

First, I created an initializer: config/initializers/active_record.rb
with ActiveRecord::Base.connection.disable_query_cache! inside it, and
then used config.middleware.delete ActiveRecord::QueryCache in config/
environments/development.rb inside the configure block. Unfortunately
I'm still left with cached queries inside the log file:

Started GET "/dashboard" for 127.0.0.1 at 2011-08-08 19:43:11 -0600
  Processing by DashboardController#index as HTML
  User Load (12.8ms) SELECT "users".* FROM "users" WHERE "users"."id"
= $1 LIMIT 1 [["id", 1]]

Do you guys think maybe it's time to submit a "bug" (for lack of a
better word) report for 3.1rc5, or is there another way I should go
about testing this?

I haven't played with 3.1 at all yet, so I'm swinging in the dark but --
does `rake middleware` list QueryCache or no?

I think you've got the wrong terminology here - you're not seeing any
more *query* caching, but the prepared statement stuff is still
active. The Rails internals seem to prefer the term "statement cache"
for that functionality, and it doesn't appear to be affected by the
switches mentioned here (which affect caching of query *results*). In
fact, there doesn't seem to be a way to switch it off at all.

I have to admit, I'm not terribly familiar with prepared statements
(other than to note that they tend to make repeated queries much
faster); if there's a situation where they are causing incorrect
operation that's DEFINITELY a bug and should be reported.

--Matt Jones