Skip to content

Instantly share code, notes, and snippets.

@peter
Created May 3, 2010 11:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save peter/387978 to your computer and use it in GitHub Desktop.
Save peter/387978 to your computer and use it in GitHub Desktop.
# This patch gives us an easy way to globally disable/enable the cache
Rails.cache.class.class_eval do
attr_accessor :enabled
def read_with_enabled(*args, &block)
enabled ? read_without_enabled(*args, &block) : nil
end
alias_method_chain :read, :enabled
end
if File.basename($0) == "rake" && ARGV.include?("db:migrate")
# The cache might mess up migrations sometimes so don't use it
Rails.cache.enabled = false
else
# Keep caching enabled in development and test environments since this might help us find issues
# with the cache. We use Rails.cache.clear in-between tests.
Rails.cache.enabled = true
end
describe "Rails.cache" do
it "can be disabled and enabled" do
Rails.cache.enabled = true
Rails.cache.enabled.should be_true
Rails.cache.write("foobar", "foo")
Rails.cache.read("foobar").should == "foo"
Rails.cache.enabled = false
Rails.cache.enabled.should be_false
Rails.cache.write("foobar", "foo")
Rails.cache.read("foobar").should be_nil
Rails.cache.enabled = true
Rails.cache.read("foobar").should == "foo"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment