aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/lib/action_controller/caching.rb18
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/abstract_adapter.rb22
-rw-r--r--activerecord/lib/active_record/query_cache.rb20
3 files changed, 44 insertions, 16 deletions
diff --git a/actionpack/lib/action_controller/caching.rb b/actionpack/lib/action_controller/caching.rb
index c7f3e95617..d016d09baa 100644
--- a/actionpack/lib/action_controller/caching.rb
+++ b/actionpack/lib/action_controller/caching.rb
@@ -10,7 +10,8 @@ module ActionController #:nodoc:
# Note: To turn off all caching and sweeping, set Base.perform_caching = false.
module Caching
def self.included(base) #:nodoc:
- base.send(:include, Pages, Actions, Fragments, Sweeping)
+ base.send(:include, Pages, Actions, Fragments)
+ base.send(:include, Sweeping, SqlCache) if defined?(ActiveRecord)
base.class_eval do
@@perform_caching = true
@@ -612,5 +613,20 @@ module ActionController #:nodoc:
end
end
end
+
+ if defined?(ActiveRecord)
+ module SqlCache
+ def self.included(base) #:nodoc:
+ base.alias_method_chain :perform_action, :caching
+ end
+
+ def perform_action_with_caching
+ ActiveRecord::Base.cache do
+ perform_action_without_caching
+ end
+ end
+ end
+ end
+
end
end
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