2007-12-21 08:48:59 +01:00
|
|
|
module ActiveRecord
|
|
|
|
module ConnectionAdapters # :nodoc:
|
|
|
|
module QueryCache
|
|
|
|
class << self
|
|
|
|
def included(base)
|
|
|
|
base.class_eval do
|
|
|
|
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__
|
2009-02-04 21:26:08 +01:00
|
|
|
def #{method_name}_with_query_dirty(*args) # def update_with_query_dirty(*args)
|
|
|
|
clear_query_cache if @query_cache_enabled # clear_query_cache if @query_cache_enabled
|
|
|
|
#{method_name}_without_query_dirty(*args) # update_without_query_dirty(*args)
|
|
|
|
end # end
|
|
|
|
#
|
|
|
|
alias_method_chain :#{method_name}, :query_dirty # alias_method_chain :update, :query_dirty
|
2007-12-21 08:48:59 +01:00
|
|
|
end_code
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-24 22:53:39 +01:00
|
|
|
attr_reader :query_cache, :query_cache_enabled
|
2008-10-27 07:47:01 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
# Enable the query cache within the block.
|
|
|
|
def cache
|
2008-11-24 22:53:39 +01:00
|
|
|
old, @query_cache_enabled = @query_cache_enabled, true
|
|
|
|
@query_cache ||= {}
|
2007-12-21 08:48:59 +01:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
clear_query_cache
|
2008-11-24 22:53:39 +01:00
|
|
|
@query_cache_enabled = old
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Disable the query cache within the block.
|
|
|
|
def uncached
|
2008-11-24 22:53:39 +01:00
|
|
|
old, @query_cache_enabled = @query_cache_enabled, false
|
2007-12-21 08:48:59 +01:00
|
|
|
yield
|
|
|
|
ensure
|
2008-11-24 22:53:39 +01:00
|
|
|
@query_cache_enabled = old
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Clears the query cache.
|
|
|
|
#
|
|
|
|
# One reason you may wish to call this method explicitly is between queries
|
|
|
|
# that ask the database to randomize results. Otherwise the cache would see
|
|
|
|
# the same SQL query and repeatedly return the same result each time, silently
|
|
|
|
# undermining the randomness you were expecting.
|
2007-12-21 08:48:59 +01:00
|
|
|
def clear_query_cache
|
2008-11-24 22:53:39 +01:00
|
|
|
@query_cache.clear if @query_cache
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def select_all_with_query_cache(*args)
|
2008-11-24 22:53:39 +01:00
|
|
|
if @query_cache_enabled
|
2007-12-21 08:48:59 +01:00
|
|
|
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)
|
2008-11-24 22:53:39 +01:00
|
|
|
if @query_cache_enabled
|
|
|
|
@query_cache["SHOW FIELDS FROM #{args.first}"] ||= columns_without_query_cache(*args)
|
2007-12-21 08:48:59 +01:00
|
|
|
else
|
|
|
|
columns_without_query_cache(*args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def cache_sql(sql)
|
|
|
|
result =
|
2008-11-24 22:53:39 +01:00
|
|
|
if @query_cache.has_key?(sql)
|
2007-12-21 08:48:59 +01:00
|
|
|
log_info(sql, "CACHE", 0.0)
|
2008-11-24 22:53:39 +01:00
|
|
|
@query_cache[sql]
|
2007-12-21 08:48:59 +01:00
|
|
|
else
|
2008-11-24 22:53:39 +01:00
|
|
|
@query_cache[sql] = yield
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if Array === result
|
|
|
|
result.collect { |row| row.dup }
|
|
|
|
else
|
|
|
|
result.duplicable? ? result.dup : result
|
|
|
|
end
|
|
|
|
rescue TypeError
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|