Module | ActiveRecord::ConnectionAdapters::QueryCache |
In: |
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb
|
query_cache | [R] | |
query_cache_enabled | [R] |
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 14 14: def dirties_query_cache(base, *method_names) 15: method_names.each do |method_name| 16: base.class_eval "def \#{method_name}_with_query_dirty(*args) # def update_with_query_dirty(*args)\nclear_query_cache if @query_cache_enabled # clear_query_cache if @query_cache_enabled\n\#{method_name}_without_query_dirty(*args) # update_without_query_dirty(*args)\nend # end\n#\nalias_method_chain :\#{method_name}, :query_dirty # alias_method_chain :update, :query_dirty\n", __FILE__, __LINE__ 17: end 18: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 5 5: def included(base) 6: base.class_eval do 7: alias_method_chain :columns, :query_cache 8: alias_method_chain :select_all, :query_cache 9: end 10: 11: dirties_query_cache base, :insert, :update, :delete 12: end
Enable the query cache within the block.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 32 32: def cache 33: old, @query_cache_enabled = @query_cache_enabled, true 34: @query_cache ||= {} 35: yield 36: ensure 37: clear_query_cache 38: @query_cache_enabled = old 39: end
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.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 55 55: def clear_query_cache 56: @query_cache.clear if @query_cache 57: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 67 67: def columns_with_query_cache(*args) 68: if @query_cache_enabled 69: @query_cache["SHOW FIELDS FROM #{args.first}"] ||= columns_without_query_cache(*args) 70: else 71: columns_without_query_cache(*args) 72: end 73: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 59 59: def select_all_with_query_cache(*args) 60: if @query_cache_enabled 61: cache_sql(args.first) { select_all_without_query_cache(*args) } 62: else 63: select_all_without_query_cache(*args) 64: end 65: end