Skip to content

Commit

Permalink
Fix rails#6951. Use query cache/uncache, when using not only database…
Browse files Browse the repository at this point in the history
….yml but also DATABASE_URL.
  • Loading branch information
kennyj committed Oct 31, 2012
1 parent 7e17b0b commit a7c3c90
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##


* Use query cache/uncache when using DATABASE_URL.
Fix #6951.

*kennyj*

* Added `#none!` method for mutating `ActiveRecord::Relation` objects to a NullRelation. * Added `#none!` method for mutating `ActiveRecord::Relation` objects to a NullRelation.
It acts like `#none` but modifies relation in place. It acts like `#none` but modifies relation in place.


Expand Down
12 changes: 6 additions & 6 deletions activerecord/lib/active_record/query_cache.rb
Expand Up @@ -5,19 +5,19 @@ class QueryCache
module ClassMethods module ClassMethods
# Enable the query cache within the block if Active Record is configured. # Enable the query cache within the block if Active Record is configured.
def cache(&block) def cache(&block)
if ActiveRecord::Base.configurations.blank? if ActiveRecord::Base.connected?
yield
else
connection.cache(&block) connection.cache(&block)
else
yield
end end
end end


# Disable the query cache within the block if Active Record is configured. # Disable the query cache within the block if Active Record is configured.
def uncached(&block) def uncached(&block)
if ActiveRecord::Base.configurations.blank? if ActiveRecord::Base.connected?
yield
else
connection.uncached(&block) connection.uncached(&block)
else
yield
end end
end end
end end
Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/cases/query_cache_test.rb
Expand Up @@ -184,6 +184,17 @@ def test_cache_is_ignored_for_locked_relations
assert_queries(2) { task.lock!; task.lock! } assert_queries(2) { task.lock!; task.lock! }
end end
end end

def test_cache_is_available_when_connection_is_connected
conf = ActiveRecord::Base.configurations

ActiveRecord::Base.configurations = {}
Task.cache do
assert_queries(1) { Task.find(1); Task.find(1) }
end
ensure
ActiveRecord::Base.configurations = conf
end
end end


class QueryCacheExpiryTest < ActiveRecord::TestCase class QueryCacheExpiryTest < ActiveRecord::TestCase
Expand Down

0 comments on commit a7c3c90

Please sign in to comment.