Methods
- cache
- clear_query_cache
- columns_with_query_cache
- dirties_query_cache
- included
- select_all_with_query_cache
- uncached
Public Class methods
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 15
15: def dirties_query_cache(base, *method_names)
16: method_names.each do |method_name|
17: base.class_eval "def \#{method_name}_with_query_dirty(*args)\nclear_query_cache if @query_cache_enabled\n\#{method_name}_without_query_dirty(*args)\nend\n\nalias_method_chain :\#{method_name}, :query_dirty\n", __FILE__, __LINE__
18: end
19: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 5
5: def included(base)
6: base.class_eval do
7: attr_accessor :query_cache_enabled
8: alias_method_chain :columns, :query_cache
9: alias_method_chain :select_all, :query_cache
10: end
11:
12: dirties_query_cache base, :insert, :update, :delete
13: end
Public Instance methods
Enable the query cache within the block.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 31
31: def cache
32: old, @query_cache_enabled = @query_cache_enabled, true
33: @query_cache ||= {}
34: yield
35: ensure
36: clear_query_cache
37: @query_cache_enabled = old
38: 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.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 54
54: def clear_query_cache
55: @query_cache.clear if @query_cache
56: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 66
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)
69: else
70: columns_without_query_cache(*args)
71: end
72: end
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 58
58: def select_all_with_query_cache(*args)
59: if @query_cache_enabled
60: cache_sql(args.first) { select_all_without_query_cache(*args) }
61: else
62: select_all_without_query_cache(*args)
63: end
64: end
Disable the query cache within the block.
[ show source ]
# File activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb, line 41
41: def uncached
42: old, @query_cache_enabled = @query_cache_enabled, false
43: yield
44: ensure
45: @query_cache_enabled = old
46: end