Forum: Ruby on Rails How can I disable Rails 3.1's ActiveRecord Query Caching?

Ef2b8b885f5aa34581fb2b12549c7f9e?d=identicon&s=25 Phoenix Rising (Guest)
on 2011-08-07 06:57
(Received via mailing list)
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/ActiveRecor...
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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2011-08-07 10:53
(Received via mailing list)
On Aug 7, 5:55am, Phoenix Rising <polarisris...@gmail.com> wrote:
> exactly.
> Started GET "/dashboard" for 127.0.0.1 at 2011-08-06 22:43:07 -0600
>
> 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#con...
)

Fred
Ef2b8b885f5aa34581fb2b12549c7f9e?d=identicon&s=25 Phoenix Rising (Guest)
on 2011-08-08 06:59
(Received via mailing list)
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/...,
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!

On Aug 7, 2:52am, Frederick Cheung <frederick.che...@gmail.com>
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2011-08-08 16:06
(Received via mailing list)
On Sun, Aug 7, 2011 at 9:57 PM, Phoenix Rising <polarisrising@gmail.com>
wrote:
> 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/...,
> I'm not seeing any method that I can use to "subtract" this from the
> stack.

http://edgeguides.rubyonrails.org/configuring.html...

HTH,
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Ef2b8b885f5aa34581fb2b12549c7f9e?d=identicon&s=25 Phoenix Rising (Guest)
on 2011-08-09 01:32
(Received via mailing list)
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?

On Aug 8, 8:05am, Hassan Schroeder <hassan.schroe...@gmail.com>
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2011-08-09 01:52
(Received via mailing list)
On Mon, Aug 8, 2011 at 4:30 PM, Phoenix Rising <polarisrising@gmail.com>
wrote:
> 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?

>> http://edgeguides.rubyonrails.org/configuring.html......

[quoting that source]

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

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Ef2b8b885f5aa34581fb2b12549c7f9e?d=identicon&s=25 Phoenix Rising (Guest)
on 2011-08-09 02:26
(Received via mailing list)
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!

On Aug 8, 5:51pm, Hassan Schroeder <hassan.schroe...@gmail.com>
Ef2b8b885f5aa34581fb2b12549c7f9e?d=identicon&s=25 Phoenix Rising (Guest)
on 2011-08-09 03:49
(Received via mailing list)
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?
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2011-08-09 16:59
(Received via mailing list)
On Mon, Aug 8, 2011 at 6:46 PM, Phoenix Rising <polarisrising@gmail.com>
wrote:
> 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.

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?

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2011-08-10 17:18
(Received via mailing list)
On Aug 8, 9:46pm, Phoenix Rising <polarisris...@gmail.com> wrote:
> 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 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.