Permalink
Browse files

Speed up and simplify query caching.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7498 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 30fb7b8 commit bfb906a905a1e8774e438b10e8cf703a829b55dc @jeremy jeremy committed Sep 17, 2007
@@ -11,13 +11,13 @@ 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)
- if defined?(ActiveRecord)
- require 'active_record/query_cache'
- base.send(:include, Sweeping, SqlCache)
- end
-
base.class_eval do
+ include Pages, Actions, Fragments
+
+ if defined? ActiveRecord
+ include Sweeping, SqlCache
+ end
+
@@perform_caching = true
cattr_accessor :perform_caching
end
View
@@ -1,5 +1,7 @@
*SVN*
+* Speed up and simplify query caching. [Jeremy Kemper]
+
* connection.select_rows 'sql' returns an array (rows) of arrays (field values). #2329 [Michael Schuerig]
* Eager loading respects explicit :joins. #9496 [dasil003]
@@ -36,6 +36,7 @@
require 'active_record/base'
require 'active_record/observer'
+require 'active_record/query_cache'
require 'active_record/validations'
require 'active_record/callbacks'
require 'active_record/reflection'
@@ -52,6 +53,7 @@
require 'active_record/attribute_methods'
ActiveRecord::Base.class_eval do
+ extend ActiveRecord::QueryCache
include ActiveRecord::Validations
include ActiveRecord::Locking::Optimistic
include ActiveRecord::Locking::Pessimistic
@@ -10,14 +10,15 @@ def select_all(sql, name = nil)
# Returns a record hash with the column names as keys and column values
# as values.
def select_one(sql, name = nil)
- result = select(sql, name)
+ result = select_all(sql, name)
result.first if result
end
# Returns a single value from a record
def select_value(sql, name = nil)
- result = select_one(sql, name)
- result.nil? ? nil : result.values.first
+ if result = select_one(sql, name)
+ result.values.first
+ end
end
# Returns an array of the values of the first column in a select:
@@ -29,7 +30,9 @@ def select_values(sql, name = nil)
# Returns an array of arrays containing the field values.
# Order is the same as that returned by #columns.
- def select_rows(sql, name = nil) end
+ def select_rows(sql, name = nil)
+ raise NotImplementedError, "select_rows is an abstract method"
+ end
# Executes the SQL statement in the context of this connection.
def execute(sql, name = nil)
@@ -38,17 +41,17 @@ def execute(sql, name = nil)
# Returns the last auto-generated ID from the affected table.
def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
- raise NotImplementedError, "insert is an abstract method"
+ insert_sql(sql, name, pk, id_value, sequence_name)
end
# Executes the update statement and returns the number of rows affected.
def update(sql, name = nil)
- execute(sql, name)
+ update_sql(sql, name)
end
# Executes the delete statement and returns the number of rows affected.
def delete(sql, name = nil)
- update(sql, name)
+ delete_sql(sql, name)
end
# Wrap a block in a transaction. Returns result of block.
@@ -133,7 +136,7 @@ def reset_sequence!(table, column, sequence = nil)
# Inserts the given fixture into the table. Overriden in adapters that require
# something beyond a simple insert (eg. Oracle).
def insert_fixture(fixture, table_name)
- execute "INSERT INTO #{table_name} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert'
+ insert "INSERT INTO #{table_name} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert'
end
protected
@@ -142,6 +145,22 @@ def insert_fixture(fixture, table_name)
def select(sql, name = nil)
raise NotImplementedError, "select is an abstract method"
end
+
+ # Returns the last auto-generated ID from the affected table.
+ def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
+ execute(sql, name)
+ id_value
+ end
+
+ # Executes the update statement and returns the number of rows affected.
+ def update_sql(sql, name = nil)
+ execute(sql, name)
+ end
+
+ # Executes the delete statement and returns the number of rows affected.
+ def delete_sql(sql, name = nil)
+ update_sql(sql, name)
+ end
end
end
end
@@ -0,0 +1,90 @@
+module ActiveRecord
+ module ConnectionAdapters # :nodoc:
+ module QueryCache
+ class << self
+ def included(base)
+ base.class_eval do
+ attr_accessor :query_cache_enabled
+ alias_method_chain :columns, :query_cache
+ alias_method_chain :select_all, :query_cache
+ end
+
+ dirties_query_cache base, :insert, :update, :delete
+ end
+
+ def dirties_query_cache(base, *method_names)
+ method_names.each do |method_name|
+ base.class_eval <<-end_code, __FILE__, __LINE__
+ def #{method_name}_with_query_dirty(*args)
+ clear_query_cache if @query_cache_enabled
+ #{method_name}_without_query_dirty(*args)
+ end
+
+ alias_method_chain :#{method_name}, :query_dirty
+ end_code
+ end
+ end
+ end
+
+ # Enable the query cache within the block.
+ def cache
+ old, @query_cache_enabled = @query_cache_enabled, true
+ @query_cache ||= {}
+ yield
+ ensure
+ clear_query_cache
+ @query_cache_enabled = old
+ end
+
+ # Disable the query cache within the block.
+ def uncached
+ old, @query_cache_enabled = @query_cache_enabled, false
+ yield
+ ensure
+ @query_cache_enabled = old
+ end
+
+ def clear_query_cache
+ @query_cache.clear if @query_cache
+ end
+
+ def select_all_with_query_cache(*args)
+ if @query_cache_enabled
+ cache_sql(args.first) { select_all_without_query_cache(*args) }
+ else
+ select_all_without_query_cache(*args)
+ end
+ end
+
+ def columns_with_query_cache(*args)
+ if @query_cache_enabled
+ @query_cache["SHOW FIELDS FROM #{args.first}"] ||= columns_without_query_cache(*args)
+ else
+ columns_without_query_cache(*args)
+ end
+ end
+
+ private
+ def cache_sql(sql)
+ result =
+ if @query_cache.has_key?(sql)
+ log_info(sql, "CACHE", 0.0)
+ @query_cache[sql]
+ else
+ @query_cache[sql] = yield
+ end
+
+ case result
+ when Array
+ result.collect { |row| row.dup }
+ when nil, Fixnum, Float, true, false
+ result
+ else
+ result.dup
+ end
+ rescue TypeError
+ result
+ end
+ end
+ end
+end
@@ -8,6 +8,7 @@
require 'active_record/connection_adapters/abstract/database_statements'
require 'active_record/connection_adapters/abstract/quoting'
require 'active_record/connection_adapters/abstract/connection_specification'
+require 'active_record/connection_adapters/abstract/query_cache'
module ActiveRecord
module ConnectionAdapters # :nodoc:
@@ -22,6 +23,7 @@ module ConnectionAdapters # :nodoc:
# SchemaStatements#remove_column are very useful.
class AbstractAdapter
include Quoting, DatabaseStatements, SchemaStatements
+ include QueryCache
@@row_even = true
def initialize(connection, logger = nil) #:nodoc:
@@ -63,9 +63,8 @@ def select_rows(sql, name = nil)
rows
end
- def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
- execute(sql, name = nil)
- id_value || last_insert_id
+ def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
+ super || last_insert_id
end
def execute(sql, name = nil)
@@ -266,13 +266,13 @@ def execute(sql, name = nil) #:nodoc:
end
end
- def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
- execute(sql, name = nil)
+ def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
+ super sql, name
id_value || @connection.insert_id
end
- def update(sql, name = nil) #:nodoc:
- execute(sql, name)
+ def update_sql(sql, name = nil) #:nodoc:
+ super
@connection.affected_rows
end
@@ -223,11 +223,6 @@ def next_sequence_value(sequence_name)
id
end
- def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
- execute(sql, name)
- id_value
- end
-
def begin_db_transaction #:nodoc:
@connection.autocommit = false
end
@@ -374,10 +374,9 @@ def select_rows(sql, name = nil)
end
# Executes an INSERT query and returns the new record's ID
- def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
- execute(sql, name)
+ def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
table = sql.split(" ", 4)[2]
- id_value || last_insert_id(table, sequence_name || default_sequence_name(table, pk))
+ super || last_insert_id(table, sequence_name || default_sequence_name(table, pk))
end
# Queries the database and returns the results in an Array or nil otherwise.
@@ -404,8 +403,8 @@ def execute(sql, name = nil)
end
# Executes an UPDATE query and returns the number of affected tuples.
- def update(sql, name = nil)
- execute(sql, name).cmdtuples
+ def update_sql(sql, name = nil)
+ super.cmdtuples
end
# Begins a transaction.
@@ -132,37 +132,18 @@ def execute(sql, name = nil) #:nodoc:
catch_schema_changes { log(sql, name) { @connection.execute(sql) } }
end
- def update(sql, name = nil) #:nodoc:
- execute(sql, name)
+ def update_sql(sql, name = nil) #:nodoc:
+ super
@connection.changes
end
- def delete(sql, name = nil) #:nodoc:
+ def delete_sql(sql, name = nil) #:nodoc:
sql += " WHERE 1=1" unless sql =~ /WHERE/i
- execute(sql, name)
- @connection.changes
+ super sql, name
end
- def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
- execute(sql, name = nil)
- id_value || @connection.last_insert_row_id
- end
-
- def select_all(sql, name = nil) #:nodoc:
- execute(sql, name).map do |row|
- record = {}
- row.each_key do |key|
- if key.is_a?(String)
- record[key.sub(/^\w+\./, '')] = row[key]
- end
- end
- record
- end
- end
-
- def select_one(sql, name = nil) #:nodoc:
- result = select_all(sql, name)
- result.nil? ? nil : result.first
+ def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
+ super || @connection.last_insert_row_id
end
def select_rows(sql, name = nil)
@@ -267,6 +248,18 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
protected
+ def select(sql, name = nil) #:nodoc:
+ execute(sql, name).map do |row|
+ record = {}
+ row.each_key do |key|
+ if key.is_a?(String)
+ record[key.sub(/^\w+\./, '')] = row[key]
+ end
+ end
+ record
+ end
+ end
+
def table_structure(table_name)
returning structure = execute("PRAGMA table_info(#{table_name})") do
raise(ActiveRecord::StatementInvalid, "Could not find table '#{table_name}'") if structure.empty?
@@ -314,18 +314,15 @@ def columns(table_name, name = nil)
columns
end
- def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
- execute(sql, name)
- id_value || select_one("SELECT @@IDENTITY AS Ident")["Ident"]
+ def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
+ super || select_value("SELECT @@IDENTITY AS Ident")
end
- def update(sql, name = nil)
+ def update_sql(sql, name = nil)
execute(sql, name) do |handle|
handle.rows
- end || select_one("SELECT @@ROWCOUNT AS AffectedRows")["AffectedRows"]
+ end || select_value("SELECT @@ROWCOUNT AS AffectedRows")
end
-
- alias_method :delete, :update
def execute(sql, name = nil)
if sql =~ /^\s*INSERT/i && (table_name = query_requires_identity_insert?(sql))
Oops, something went wrong.

0 comments on commit bfb906a

Please sign in to comment.