1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

remove intelligence from StatementCache#initialize

This commit is contained in:
Aaron Patterson 2014-04-10 17:20:22 -07:00
parent 2d3969c82f
commit 249fd686fb
3 changed files with 18 additions and 16 deletions

View file

@ -135,7 +135,7 @@ module ActiveRecord
key = primary_key
s = find_by_statement_cache[key] || find_by_statement_cache.synchronize {
find_by_statement_cache[key] ||= StatementCache.new { |params|
find_by_statement_cache[key] ||= StatementCache.create { |params|
where(key => params[key]).limit(1)
}
}
@ -159,7 +159,7 @@ module ActiveRecord
klass = self
s = find_by_statement_cache[key] || find_by_statement_cache.synchronize {
find_by_statement_cache[key] ||= StatementCache.new { |params|
find_by_statement_cache[key] ||= StatementCache.create { |params|
wheres = key.each_with_object({}) { |param,o|
o[param] = params[param]
}

View file

@ -76,11 +76,18 @@ module ActiveRecord
attr_reader :bind_map, :query_builder
def initialize(block = Proc.new)
relation = block.call Params.new
@bind_map = BindMap.new relation.bind_values
def self.create(block = Proc.new)
relation = block.call Params.new
bind_map = BindMap.new relation.bind_values
klass = relation.klass
@query_builder = make_query_builder klass.connection, relation.arel
connection = klass.connection
query_builder = connection.cacheable_query relation.arel
new query_builder, bind_map
end
def initialize(query_builder, bind_map)
@query_builder = query_builder
@bind_map = bind_map
end
def execute(params, klass, connection)
@ -91,10 +98,5 @@ module ActiveRecord
klass.find_by_sql sql, bind_values
end
alias :call :execute
private
def make_query_builder(connection, arel)
connection.cacheable_query(arel)
end
end
end

View file

@ -15,7 +15,7 @@ module ActiveRecord
Book.create(name: "my book")
Book.create(name: "my other book")
cache = StatementCache.new do |params|
cache = StatementCache.create do |params|
Book.where(:name => params[:name])
end
@ -30,7 +30,7 @@ module ActiveRecord
b1 = Book.create(name: "my book")
b2 = Book.create(name: "my other book")
cache = StatementCache.new do |params|
cache = StatementCache.create do |params|
Book.where(id: params[:id])
end
@ -53,7 +53,7 @@ module ActiveRecord
#End
def test_statement_cache_with_simple_statement
cache = ActiveRecord::StatementCache.new do |params|
cache = ActiveRecord::StatementCache.create do |params|
Book.where(name: "my book").where("author_id > 3")
end
@ -64,7 +64,7 @@ module ActiveRecord
end
def test_statement_cache_with_complex_statement
cache = ActiveRecord::StatementCache.new do |params|
cache = ActiveRecord::StatementCache.create do |params|
Liquid.joins(:molecules => :electrons).where('molecules.name' => 'dioxane', 'electrons.name' => 'lepton')
end
@ -77,7 +77,7 @@ module ActiveRecord
end
def test_statement_cache_values_differ
cache = ActiveRecord::StatementCache.new do |params|
cache = ActiveRecord::StatementCache.create do |params|
Book.where(name: "my book")
end