How to disable the query cache in rails 2.1

If you need to disable the query cache in rails, it’s not particularly easy to do that.There is some discussion about it (dated March 2008) here.Although you can turn all caching off, and you can turn the query cache off explicitly in your code using uncached, there isn’t a way to turn just the query cache off globally at configuration time.So, then, here’s the monkey patch you need (tested on Rails 2.1.1). Although this is not particularly optimal (in that some query caching related code is still called), it will work.  You can put this at the bottom of your environment.rb somewhere, or even better put it in its own file in the initializers directory (e.g. query_cached_off.rb).

module ActiveRecord  module ConnectionAdapters    module QueryCache      private      def cache_sql(sql)        yield      end    end  endend
Advertisements

7 thoughts on “How to disable the query cache in rails 2.1

  1. I recently had to figure out how to disable the query cache, but just for one method, so I used uncached. I wrote up that little article about how to do that in case anybody Googled it (thanks for the link!)Just curious — why do you need to disable query caching entirely?

  2. Um – I’m still very much a rails and ruby newbie, so how do you invoke/use this "monkey patch" An example would be quite helpful. This patch could save me a ton of time, so any help would be greatly appreciated!

  3. Make a file called query_cached_off.rb in the directory config/initializers. Place the code in there. The next time you restart rails the query cache will be turned off.

  4. How about this patch instead?module ActionController module Caching module SqlCache def perform_action_with_caching perform_action_without_caching end end endend

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s