@@ -9,6 +9,56 @@ def prefetch_associations(includes, records)
99 Cached ::Prefetcher . prefetch ( self , includes , records )
1010 end
1111
12+ # Get only the columns whose values are needed to manually expire caches
13+ # after updating or deleting rows without triggering after_commit callbacks.
14+ #
15+ # 1. Pass the returned columns into Active Record's `select` or `pluck` query
16+ # method on the scope that will be used to modify the database in order to
17+ # query original for these rows that will be modified.
18+ # 2. Update or delete the rows
19+ # 3. Use {expire_cache_for_update} or {expire_cache_for_delete} to expires the
20+ # caches, passing in the values from the query in step 1 as the indexed_values.
21+ #
22+ # @return [Array<Symbol>] the array of column names
23+ def cache_indexed_columns
24+ @cache_indexed_columns ||= begin
25+ check_for_unsupported_parent_expiration_entries
26+ columns = Set . new
27+ columns << primary_key . to_sym if primary_cache_index_enabled
28+ cache_indexes . each do |cached_attribute |
29+ columns . merge ( cached_attribute . key_fields )
30+ end
31+ columns . to_a . freeze
32+ end
33+ end
34+
35+ def expire_cache_for_update ( old_indexed_values , changes )
36+ if primary_cache_index_enabled
37+ id = old_indexed_values . fetch ( primary_key . to_sym )
38+ expire_primary_key_cache_index ( id )
39+ end
40+ cache_indexes . each do |cached_attribute |
41+ cached_attribute . expire_for_update ( old_indexed_values , changes )
42+ end
43+ parent_expiration_entries . each do ||
44+ check_for_unsupported_parent_expiration_entries
45+ end
46+
47+ private def expire_cache_for_insert_or_delete ( indexed_values )
48+ if primary_cache_index_enabled
49+ id = indexed_values . fetch ( primary_key . to_sym )
50+ expire_primary_key_cache_index ( id )
51+ end
52+ cache_indexes . each do |cached_attribute |
53+ cached_attribute . expire_for_values ( indexed_values )
54+ end
55+ check_for_unsupported_parent_expiration_entries
56+ end
57+
58+ alias_method :expire_cache_for_insert , :expire_cache_for_insert_or_delete
59+
60+ alias_method :expire_cache_for_delete , :expire_cache_for_insert_or_delete
61+
1262 # @api private
1363 def cached_association ( name ) # :nodoc:
1464 cached_has_manys [ name ] || cached_has_ones [ name ] || cached_belongs_tos . fetch ( name )
0 commit comments