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

use statement cache for belongs_to relations

This commit is contained in:
Aaron Patterson 2014-04-22 11:28:21 -05:00
parent bdd64912ac
commit 86d6f05e69
3 changed files with 17 additions and 3 deletions

View file

@ -416,7 +416,7 @@ module ActiveRecord
return scope.to_a if reflection.scope_chain.any?(&:any?)
conn = klass.connection
sc = reflection.association_scope_cache(conn) do
sc = reflection.association_scope_cache(conn, owner) do
StatementCache.create(conn) { |params|
as = AssociationScope.create { params.bind }
target_scope.merge as.scope(self, conn)

View file

@ -39,7 +39,18 @@ module ActiveRecord
end
def get_records
scope.limit(1).to_a
return scope.limit(1).to_a if reflection.scope_chain.any?(&:any?)
conn = klass.connection
sc = reflection.association_scope_cache(conn, owner) do
StatementCache.create(conn) { |params|
as = AssociationScope.create { params.bind }
target_scope.merge(as.scope(self, conn)).limit(1)
}
end
binds = AssociationScope.get_bind_values(owner, reflection.chain)
sc.execute binds, klass, klass.connection
end
def find_target

View file

@ -205,8 +205,11 @@ module ActiveRecord
@scope_lock = Mutex.new
end
def association_scope_cache(conn)
def association_scope_cache(conn, owner)
key = conn.prepared_statements
if options[:polymorphic]
key = [key, owner.read_attribute(@foreign_type)]
end
@association_scope_cache[key] ||= @scope_lock.synchronize {
@association_scope_cache[key] ||= yield
}