提交 20fd254a 编写于 作者: A Aaron Patterson

cache queries in the Model.find(id) path

上级 77b18d7e
......@@ -90,6 +90,8 @@ def self.type_cast_config_to_boolean(config)
end
end
attr_reader :prepared_statements
def initialize(connection, logger = nil, pool = nil) #:nodoc:
super()
......
......@@ -117,6 +117,32 @@ def inherited(child_class)
super
end
def find(*ids)
# We don't have cache keys for this stuff yet
return super unless ids.length == 1
return super if block_given? ||
primary_key.nil? ||
default_scopes.any? ||
columns_hash.include?(inheritance_column) ||
!connection.prepared_statements ||
ids.first.kind_of?(Array)
id = ids.first
id = id.id if ActiveRecord::Base === id
key = primary_key
s = find_by_statement_cache[key] || find_by_statement_cache.synchronize {
find_by_statement_cache[key] ||= StatementCache.new { |params|
where(key => params[key]).limit(1)
}
}
record = s.execute(key => id).first
unless record
raise RecordNotFound, "Couldn't find #{name} with '#{primary_key}'=#{id}"
end
record
end
def find_by(*args)
return super if current_scope || args.length > 1 || reflect_on_all_aggregations.any?
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册