2 module ConnectionAdapters
# :nodoc:
7 alias_method_chain
:columns, :query_cache
8 alias_method_chain
:select_all, :query_cache
11 dirties_query_cache base
, :insert, :update, :delete
14 def dirties_query_cache(base
, *method_names
)
15 method_names
.each
do |method_name
|
16 base
.class_eval
<<-end_code, __FILE__, __LINE__
17 def #{method_name}_with_query_dirty(*args)
18 clear_query_cache if @query_cache_enabled
19 #{method_name}_without_query_dirty(*args)
22 alias_method_chain :#{method_name}, :query_dirty
28 attr_reader
:query_cache, :query_cache_enabled
30 # Enable the query cache within the block.
32 old
, @query_cache_enabled = @query_cache_enabled, true
37 @query_cache_enabled = old
40 # Disable the query cache within the block.
42 old
, @query_cache_enabled = @query_cache_enabled, false
45 @query_cache_enabled = old
48 # Clears the query cache.
50 # One reason you may wish to call this method explicitly is between queries
51 # that ask the database to randomize results. Otherwise the cache would see
52 # the same SQL query and repeatedly return the same result each time, silently
53 # undermining the randomness you were expecting.
55 @query_cache.clear
if @query_cache
58 def select_all_with_query_cache(*args
)
59 if @query_cache_enabled
60 cache_sql(args
.first
) { select_all_without_query_cache(*args
) }
62 select_all_without_query_cache(*args
)
66 def columns_with_query_cache(*args
)
67 if @query_cache_enabled
68 @query_cache["SHOW FIELDS FROM #{args.first}"] ||= columns_without_query_cache(*args
)
70 columns_without_query_cache(*args
)
77 if @query_cache.has_key
?(sql
)
78 log_info(sql
, "CACHE", 0.0)
81 @query_cache[sql
] = yield
85 result
.collect
{ |row
| row
.dup
}
87 result
.duplicable
? ? result
.dup
: result