diff options
author | Tobias Lütke <tobias.luetke@gmail.com> | 2007-02-21 18:08:39 +0000 |
---|---|---|
committer | Tobias Lütke <tobias.luetke@gmail.com> | 2007-02-21 18:08:39 +0000 |
commit | 2ffbc6115e5389642f1b1c10fa5257aef0ad3920 (patch) | |
tree | 888054c888e12e935fa4fc25187a2eac64a6a260 /activerecord | |
parent | 9b854c22b10992de25291ce1b69699618634ec72 (diff) | |
download | rails-2ffbc6115e5389642f1b1c10fa5257aef0ad3920.tar.gz rails-2ffbc6115e5389642f1b1c10fa5257aef0ad3920.tar.bz2 rails-2ffbc6115e5389642f1b1c10fa5257aef0ad3920.zip |
Enable active record cache automatically for all actions
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6189 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rwxr-xr-x | activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 22 | ||||
-rw-r--r-- | activerecord/lib/active_record/query_cache.rb | 20 |
2 files changed, 27 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index d113897d46..11112106f7 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -101,6 +101,17 @@ module ActiveRecord def raw_connection @connection end + + def log_info(sql, name, runtime) + return unless @logger or !@logger.debug? + + @logger.debug( + format_log_entry( + "#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})", + sql.gsub(/ +/, " ") + ) + ) + end protected def log(sql, name) @@ -128,17 +139,6 @@ module ActiveRecord raise ActiveRecord::StatementInvalid, message end - def log_info(sql, name, runtime) - return unless @logger - - @logger.debug( - format_log_entry( - "#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})", - sql.gsub(/ +/, " ") - ) - ) - end - def format_log_entry(message, dump = nil) if ActiveRecord::Base.colorize_logging if @@row_even diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb index e2f5c1bd88..e5f3d7825c 100644 --- a/activerecord/lib/active_record/query_cache.rb +++ b/activerecord/lib/active_record/query_cache.rb @@ -10,19 +10,19 @@ module ActiveRecord end def select_all(sql, name = nil) - (@query_cache[sql] ||= @connection.select_all(sql, name)).dup + cache(sql) { @connection.select_all(sql, name) } end def select_one(sql, name = nil) - @query_cache[sql] ||= @connection.select_one(sql, name) + cache(sql) { @connection.select_one(sql, name) } end def select_values(sql, name = nil) - (@query_cache[sql] ||= @connection.select_values(sql, name)).dup + cache(sql) { @connection.select_values(sql, name) } end def select_value(sql, name = nil) - @query_cache[sql] ||= @connection.select_value(sql, name) + cache(sql) { @connection.select_value(sql, name) } end def execute(sql, name = nil) @@ -50,6 +50,18 @@ module ActiveRecord end private + + def cache(sql) + result = if @query_cache.has_key?(sql) + log_info(sql, "CACHE", 0.0) + @query_cache[sql] + else + @query_cache[sql] = yield + end + + result ? result.dup : nil + end + def method_missing(method, *arguments, &proc) @connection.send(method, *arguments, &proc) end |