aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/core.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2014-02-18 11:45:42 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2014-02-18 11:45:42 -0800
commit20fd254a5bdf35347d231dcc44d7b94cc5c00c1e (patch)
treed0e51e84e424a84dff50ab95cab1bf00389dbbb5 /activerecord/lib/active_record/core.rb
parent77b18d7ef47f4dd6bf39853ac83cc19a3593406f (diff)
downloadrails-20fd254a5bdf35347d231dcc44d7b94cc5c00c1e.tar.gz
rails-20fd254a5bdf35347d231dcc44d7b94cc5c00c1e.tar.bz2
rails-20fd254a5bdf35347d231dcc44d7b94cc5c00c1e.zip
cache queries in the Model.find(id) path
Diffstat (limited to 'activerecord/lib/active_record/core.rb')
-rw-r--r--activerecord/lib/active_record/core.rb26
1 files changed, 26 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index adc9383bdc..941ff6bffd 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -117,6 +117,32 @@ module ActiveRecord
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?