2 module ConnectionAdapters # :nodoc:
7 attr_accessor :query_cache_enabled
8 alias_method_chain :columns, :query_cache
9 alias_method_chain :select_all, :query_cache
12 dirties_query_cache base, :insert, :update, :delete
15 def dirties_query_cache(base, *method_names)
16 method_names.each do |method_name|
17 base.class_eval <<-end_code, __FILE__, __LINE__
18 def #{method_name}_with_query_dirty(*args)
19 clear_query_cache if @query_cache_enabled
20 #{method_name}_without_query_dirty(*args)
23 alias_method_chain :#{method_name}, :query_dirty
29 # Enable the query cache within the block.
31 old, @query_cache_enabled = @query_cache_enabled, true
36 @query_cache_enabled = old
39 # Disable the query cache within the block.
41 old, @query_cache_enabled = @query_cache_enabled, false
44 @query_cache_enabled = old
48 @query_cache.clear if @query_cache
51 def select_all_with_query_cache(*args)
52 if @query_cache_enabled
53 cache_sql(args.first) { select_all_without_query_cache(*args) }
55 select_all_without_query_cache(*args)
59 def columns_with_query_cache(*args)
60 if @query_cache_enabled
61 @query_cache["SHOW FIELDS FROM #{args.first}"] ||= columns_without_query_cache(*args)
63 columns_without_query_cache(*args)
70 if @query_cache.has_key?(sql)
71 log_info(sql, "CACHE", 0.0)
74 @query_cache[sql] = yield
78 result.collect { |row| row.dup }
80 result.duplicable? ? result.dup : result