3
3
module ActiveRecord
4
4
# = Active Record Query Cache
5
5
class QueryCache
6
+ # ActiveRecord::Base extends this module, so these methods are available in models.
6
7
module ClassMethods
7
8
# Enable the query cache within the block if Active Record is configured.
8
9
# If it's not, it will execute the given block.
@@ -20,11 +21,15 @@ def cache(&block)
20
21
end
21
22
end
22
23
23
- # Disable the query cache within the block if Active Record is configured.
24
- # If it's not, it will execute the given block.
24
+ # Runs the block with the query cache disabled.
25
+ #
26
+ # If the query cache was enabled before the block was executed, it is
27
+ # enabled again after it.
25
28
#
26
- # Set <tt>dirties: false</tt> to prevent query caches on all connections from being cleared by write operations.
27
- # (By default, write operations dirty all connections' query caches in case they are replicas whose cache would now be outdated.)
29
+ # Set <tt>dirties: false</tt> to prevent query caches on all connections
30
+ # from being cleared by write operations. (By default, write operations
31
+ # dirty all connections' query caches in case they are replicas whose
32
+ # cache would now be outdated.)
28
33
def uncached ( dirties : true , &block )
29
34
if connected? || !configurations . empty?
30
35
connection_pool . disable_query_cache ( dirties : dirties , &block )
@@ -34,22 +39,24 @@ def uncached(dirties: true, &block)
34
39
end
35
40
end
36
41
37
- def self . run
38
- ActiveRecord ::Base . connection_handler . each_connection_pool . reject ( &:query_cache_enabled ) . each do |pool |
39
- next if pool . db_config &.query_cache == false
40
- pool . enable_query_cache!
42
+ module ExecutorHooks # :nodoc:
43
+ def self . run
44
+ ActiveRecord ::Base . connection_handler . each_connection_pool . reject ( &:query_cache_enabled ) . each do |pool |
45
+ next if pool . db_config &.query_cache == false
46
+ pool . enable_query_cache!
47
+ end
41
48
end
42
- end
43
49
44
- def self . complete ( pools )
45
- pools . each do |pool |
46
- pool . disable_query_cache!
47
- pool . clear_query_cache
50
+ def self . complete ( pools )
51
+ pools . each do |pool |
52
+ pool . disable_query_cache!
53
+ pool . clear_query_cache
54
+ end
48
55
end
49
56
end
50
57
51
- def self . install_executor_hooks ( executor = ActiveSupport ::Executor )
52
- executor . register_hook ( self )
58
+ def self . install_executor_hooks ( executor = ActiveSupport ::Executor ) # :nodoc:
59
+ executor . register_hook ( ExecutorHooks )
53
60
end
54
61
end
55
62
end
0 commit comments