You can now use cache in instance hierachies. This allows ActiveRecord::Base.cache { } usage to cache everything
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6179 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
e105653066
commit
b5419cd66e
|
@ -63,16 +63,28 @@ module ActiveRecord
|
|||
def query_caches
|
||||
(Thread.current[:query_cache] ||= {})
|
||||
end
|
||||
|
||||
def query_cache
|
||||
if query_caches[self]
|
||||
query_caches[self]
|
||||
elsif superclass.respond_to?(:query_cache)
|
||||
superclass.query_cache
|
||||
end
|
||||
end
|
||||
|
||||
def query_cache=(cache)
|
||||
query_caches[self] = cache
|
||||
end
|
||||
|
||||
def cache
|
||||
query_caches[self] = QueryCache.new(connection)
|
||||
self.query_cache = QueryCache.new(connection_without_query_cache)
|
||||
yield
|
||||
ensure
|
||||
query_caches[self] = nil
|
||||
self.query_cache = nil
|
||||
end
|
||||
|
||||
def connection
|
||||
query_caches[self] || connection_without_query_cache
|
||||
query_cache || connection_without_query_cache
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,10 +3,8 @@ require 'fixtures/topic'
|
|||
require 'fixtures/reply'
|
||||
require 'fixtures/task'
|
||||
|
||||
|
||||
class QueryCacheTest < Test::Unit::TestCase
|
||||
fixtures :tasks
|
||||
|
||||
|
||||
def test_find_queries
|
||||
assert_queries(2) { Task.find(1); Task.find(1) }
|
||||
|
@ -23,14 +21,32 @@ class QueryCacheTest < Test::Unit::TestCase
|
|||
assert_queries(1) { Task.find(1); Task.find(1) }
|
||||
end
|
||||
end
|
||||
|
||||
def test_query_cache_returned
|
||||
assert_not_equal ActiveRecord::QueryCache, Task.connection.class
|
||||
Task.cache do
|
||||
assert_equal ActiveRecord::QueryCache, Task.connection.class
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def test_cache_is_scoped_on_actual_class_only
|
||||
Task.cache do
|
||||
assert_queries(2) { Topic.find(1); Topic.find(1) }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def test_cache_is_scoped_on_all_descending_classes
|
||||
ActiveRecord::Base.cache do
|
||||
assert_queries(1) { Task.find(1); Task.find(1) }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
uses_mocha('QueryCacheExpiryTest') do
|
||||
|
||||
class QueryCacheExpiryTest < Test::Unit::TestCase
|
||||
|
|
Loading…
Reference in New Issue