cache_sweeper 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,368 +0,0 @@
1
- require 'request_store'
2
- require 'set'
3
-
4
- module CacheSweeper
5
- module Loader
6
- SWEEPER_PATH = 'app/sweepers'.freeze
7
-
8
- class << self
9
- # Configuration resolution methods
10
- def resolve_trigger(rule, sweeper)
11
- rule[:trigger] || sweeper.trigger || CacheSweeper.trigger
12
- end
13
-
14
- def resolve_mode(rule, sweeper)
15
- rule[:mode] || sweeper.mode || CacheSweeper.mode
16
- end
17
-
18
- def resolve_queue(rule, sweeper)
19
- rule[:queue] || sweeper.queue || CacheSweeper.queue
20
- end
21
-
22
- def resolve_sidekiq_options(rule, sweeper)
23
- # Merge options in order of precedence: rule > sweeper > global
24
- options = CacheSweeper.sidekiq_options.dup
25
- options.merge!(sweeper.sidekiq_options) if sweeper.sidekiq_options
26
- options.merge!(rule[:sidekiq_options]) if rule[:sidekiq_options]
27
-
28
- # Add queue to options if specified
29
- queue = resolve_queue(rule, sweeper)
30
- options[:queue] = queue if queue && queue != :default
31
-
32
- options
33
- end
34
- end
35
-
36
- def self.collect_pending_key(key, sweeper = nil, mode = :request)
37
- if mode == :request
38
- RequestStore.store["cache_sweeper_pending_keys_#{sweeper&.name || 'global'}"] ||= Set.new
39
- RequestStore.store["cache_sweeper_pending_keys_#{sweeper&.name || 'global'}"] << key
40
- elsif mode == :job
41
- RequestStore.store["cache_sweeper_job_keys_#{sweeper&.name || 'global'}"] ||= Set.new
42
- RequestStore.store["cache_sweeper_job_keys_#{sweeper&.name || 'global'}"] << key
43
- end
44
- end
45
-
46
- def self.flush_pending_keys(sweeper: nil)
47
- start_time = Time.current
48
- sweeper_name = sweeper&.name || 'global'
49
-
50
- CacheSweeper::Logger.log_cache_operations("Flushing pending keys for sweeper: #{sweeper_name}", :debug, {
51
- sweeper: sweeper_name
52
- })
53
-
54
- # Flush request-level keys
55
- request_key = "cache_sweeper_pending_keys_#{sweeper_name}"
56
- keys = RequestStore.store[request_key]
57
- if keys&.any?
58
- if defined?(Rails) && Rails.respond_to?(:cache)
59
- deleted_count = CacheSweeper.delete_cache_keys(keys, {
60
- sweeper: sweeper_name,
61
- mode: :inline,
62
- trigger: :request
63
- })
64
- failed_count = keys.length - deleted_count
65
- else
66
- deleted_count = 0
67
- failed_count = keys.length
68
- end
69
-
70
- CacheSweeper::Logger.log_cache_operations("Flushed request-level keys", :info, {
71
- sweeper: sweeper_name,
72
- deleted_count: deleted_count,
73
- failed_count: failed_count,
74
- total_keys: keys.length
75
- })
76
-
77
- RequestStore.store[request_key] = Set.new
78
- end
79
-
80
- # Flush job-level keys
81
- job_key = "cache_sweeper_job_keys_#{sweeper_name}"
82
- job_keys = RequestStore.store[job_key]
83
- if job_keys&.any?
84
- begin
85
- CacheSweeper::AsyncWorker.perform_async(job_keys.to_a, :request)
86
- CacheSweeper::Logger.log_cache_operations("Scheduled async job for pending keys", :info, {
87
- sweeper: sweeper_name,
88
- keys_count: job_keys.length,
89
- keys: job_keys.to_a
90
- })
91
- rescue => e
92
- CacheSweeper::Logger.log_error(e, {
93
- sweeper: sweeper_name,
94
- keys: job_keys.to_a,
95
- error_type: 'async_job_schedule_error'
96
- })
97
- end
98
- RequestStore.store[job_key] = Set.new
99
- end
100
-
101
- duration = (Time.current - start_time) * 1000
102
- CacheSweeper::Logger.log_performance("flush_pending_keys", duration, {
103
- sweeper: sweeper_name,
104
- request_keys_flushed: keys&.length || 0,
105
- job_keys_scheduled: job_keys&.length || 0
106
- })
107
- end
108
-
109
- def self.load_sweepers!
110
- Dir[Rails.root.join(SWEEPER_PATH, '**', '*_sweeper.rb')].each { |file| require_dependency file }
111
- end
112
-
113
- def self.hook_sweepers!
114
- start_time = Time.current
115
- CacheSweeper::Logger.log_initialization("Starting model attachment process")
116
-
117
- sweeper_count = 0
118
- rule_count = 0
119
- error_count = 0
120
- CacheSweeper::Base.descendants.each do |sweeper|
121
- sweeper_count += 1
122
- CacheSweeper::Logger.log_initialization("Processing sweeper: #{sweeper.name}", { sweeper: sweeper.name, rule_count: sweeper.cache_sweeper_rules.length })
123
-
124
- sweeper.cache_sweeper_rules.each do |rule|
125
- rule_count += 1
126
- association = rule[:association]
127
- attributes = rule[:attributes]
128
- condition = rule[:condition]
129
- keys = rule[:keys]
130
-
131
- begin
132
- if association.nil?
133
- model_name = sweeper.name.sub('Sweeper', '')
134
- model = model_name.constantize
135
- CacheSweeper::Logger.log_initialization("Attaching direct model callback: #{model_name}", {
136
- sweeper: sweeper.name,
137
- model: model_name,
138
- attributes: attributes,
139
- callback: rule[:callback] || :after_commit,
140
- events: rule[:on] || [:create, :update, :destroy]
141
- })
142
- attach_callbacks(model, sweeper, attributes, condition, keys, rule)
143
- else
144
- parent_model_name = sweeper.name.sub('Sweeper', '')
145
- parent_model = parent_model_name.constantize
146
- assoc_reflection = parent_model.reflect_on_association(association)
147
-
148
- unless assoc_reflection
149
- CacheSweeper::Logger.warn("Association not found: #{parent_model_name}##{association}", {
150
- sweeper: sweeper.name,
151
- parent_model: parent_model_name,
152
- association: association
153
- })
154
- next
155
- end
156
-
157
- assoc_model = assoc_reflection.klass
158
- CacheSweeper::Logger.log_initialization("Attaching association callback: #{parent_model_name}##{association} -> #{assoc_model.name}", {
159
- sweeper: sweeper.name,
160
- parent_model: parent_model_name,
161
- association: association,
162
- assoc_model: assoc_model.name,
163
- attributes: attributes,
164
- callback: rule[:callback] || :after_commit,
165
- events: rule[:on] || [:create, :update, :destroy]
166
- })
167
- attach_callbacks(assoc_model, sweeper, attributes, condition, keys, rule, parent_model, association)
168
- end
169
- rescue NameError => e
170
- error_count += 1
171
- CacheSweeper::Logger.log_error(e, {
172
- sweeper: sweeper.name,
173
- association: association,
174
- error_type: 'model_not_found'
175
- })
176
- # Model doesn't exist yet, skip for now
177
- next
178
- rescue => e
179
- error_count += 1
180
- CacheSweeper::Logger.log_error(e, {
181
- sweeper: sweeper.name,
182
- association: association,
183
- error_type: 'attachment_error'
184
- })
185
- next
186
- end
187
- end
188
- end
189
-
190
- duration = (Time.current - start_time) * 1000
191
- CacheSweeper::Logger.log_initialization("Model attachment completed", {
192
- sweeper_count: sweeper_count,
193
- rule_count: rule_count,
194
- error_count: error_count,
195
- duration_ms: duration.round(3)
196
- })
197
- end
198
-
199
- def self.attach_callbacks(model, sweeper, attributes, condition, keys, rule, parent_model = nil, association = nil)
200
- callback = lambda do |record|
201
- start_time = Time.current
202
- sweeper_instance = sweeper.new
203
-
204
- # Log rule execution start
205
- CacheSweeper::Logger.log_rule_execution(rule, record, 'started', {
206
- event: record.previous_changes.keys.any? ? 'update' : (record.persisted? ? 'create' : 'destroy'),
207
- changed_attributes: record.saved_changes.keys
208
- })
209
-
210
- begin
211
- # Check attribute changes
212
- if attributes
213
- changed = (record.saved_changes.keys.map(&:to_sym) & attributes.map(&:to_sym)).any?
214
- CacheSweeper::Logger.log_rule_execution(rule, record, "attribute_check: #{changed}", {
215
- watched_attributes: attributes,
216
- changed_attributes: record.saved_changes.keys,
217
- relevant_changes: record.saved_changes.keys.map(&:to_sym) & attributes.map(&:to_sym)
218
- })
219
- next unless changed
220
- end
221
-
222
- # Check condition
223
- if condition
224
- condition_result = sweeper_instance.call_condition(condition, record)
225
- CacheSweeper::Logger.log_rule_execution(rule, record, "condition_check: #{condition_result}", {
226
- condition: condition.class.name,
227
- condition_result: condition_result
228
- })
229
- next unless condition_result
230
- end
231
-
232
- # Generate cache keys
233
- cache_keys = if keys.is_a?(Proc)
234
- begin
235
- generated_keys = keys.call(record)
236
- CacheSweeper::Logger.log_rule_execution(rule, record, "keys_generated", {
237
- keys_count: Array(generated_keys).length,
238
- keys: Array(generated_keys)
239
- })
240
- generated_keys
241
- rescue => e
242
- CacheSweeper::Logger.log_error(e, {
243
- sweeper: sweeper.name,
244
- record_class: record.class.name,
245
- record_id: record.id,
246
- error_type: 'key_generation_error'
247
- })
248
- next
249
- end
250
- else
251
- Array(keys)
252
- end
253
-
254
- # Execute cache invalidation
255
- if parent_model && association
256
- # For association rules, we need to find the parent records
257
- # that have this record in their association
258
- parents = parent_model.joins(association).where(association => { id: record.id })
259
- CacheSweeper::Logger.log_rule_execution(rule, record, "association_processing", {
260
- parent_count: parents.count,
261
- association: association
262
- })
263
- Array(parents).each do |parent|
264
- CacheSweeper::Loader.invalidate_cache(cache_keys, record, rule)
265
- end
266
- else
267
- CacheSweeper::Loader.invalidate_cache(cache_keys, record, rule)
268
- end
269
-
270
- # Log successful completion
271
- duration = (Time.current - start_time) * 1000
272
- CacheSweeper::Logger.log_rule_execution(rule, record, "completed", {
273
- duration_ms: duration.round(3),
274
- cache_keys_processed: Array(cache_keys).length
275
- })
276
-
277
- rescue => e
278
- duration = (Time.current - start_time) * 1000
279
- CacheSweeper::Logger.log_error(e, {
280
- sweeper: sweeper.name,
281
- record_class: record.class.name,
282
- record_id: record.id,
283
- duration_ms: duration.round(3),
284
- error_type: 'rule_execution_error'
285
- })
286
- end
287
- end
288
-
289
- callback_type = rule[:callback] || :after_commit
290
- events = rule[:on] || [:create, :update, :destroy]
291
- model.send(callback_type, callback, on: events)
292
- end
293
-
294
- def self.invalidate_cache(keys, *args, rule)
295
- start_time = Time.current
296
- sweeper = rule[:sweeper_class]
297
- trigger = resolve_trigger(rule, sweeper)
298
- mode = resolve_mode(rule, sweeper)
299
- sidekiq_opts = resolve_sidekiq_options(rule, sweeper)
300
-
301
- CacheSweeper::Logger.debug("Cache invalidation started", {
302
- sweeper: sweeper&.name,
303
- trigger: trigger,
304
- mode: mode,
305
- keys_count: Array(keys).length
306
- })
307
-
308
- begin
309
- if trigger == :request
310
- RequestStore.store[:cache_sweeper_request_pending] ||= []
311
- RequestStore.store[:cache_sweeper_request_pending] << { keys: keys, mode: mode, sidekiq_options: sidekiq_opts }
312
- CacheSweeper::Logger.log_cache_operations("Batched for request: #{Array(keys).inspect} (mode: #{mode})", :info, {
313
- keys: Array(keys),
314
- mode: mode,
315
- batch_size: RequestStore.store[:cache_sweeper_request_pending].length
316
- })
317
- else
318
- if mode == :async
319
- CacheSweeper::AsyncWorker.set(sidekiq_opts).perform_async(keys, trigger)
320
- CacheSweeper::Logger.log_cache_operations("Scheduled async job for: #{Array(keys).inspect}", :info, {
321
- keys: Array(keys),
322
- sidekiq_options: sidekiq_opts
323
- })
324
- else
325
- if defined?(Rails) && Rails.respond_to?(:cache)
326
- deleted_count = CacheSweeper.delete_cache_keys(keys, {
327
- sweeper: sweeper&.name,
328
- mode: :inline,
329
- trigger: :instant
330
- })
331
- failed_count = Array(keys).length - deleted_count
332
- else
333
- CacheSweeper::Logger.log_cache_operations("Rails cache not available for #{Array(keys).length} keys", :warn, {
334
- keys: Array(keys)
335
- })
336
- deleted_count = 0
337
- failed_count = Array(keys).length
338
- end
339
-
340
- CacheSweeper::Logger.log_cache_operations("Instant deletion completed", :info, {
341
- deleted_count: deleted_count,
342
- failed_count: failed_count,
343
- total_keys: Array(keys).length
344
- })
345
- end
346
- end
347
-
348
- duration = (Time.current - start_time) * 1000
349
- CacheSweeper::Logger.log_performance("cache_invalidation", duration, {
350
- trigger: trigger,
351
- mode: mode,
352
- keys_count: Array(keys).length
353
- })
354
-
355
- rescue => e
356
- duration = (Time.current - start_time) * 1000
357
- CacheSweeper::Logger.log_error(e, {
358
- sweeper: sweeper&.name,
359
- trigger: trigger,
360
- mode: mode,
361
- keys: Array(keys),
362
- duration_ms: duration.round(3),
363
- error_type: 'cache_invalidation_error'
364
- })
365
- end
366
- end
367
- end
368
- end
@@ -1,104 +0,0 @@
1
- # lib/cache_sweeper/logger.rb
2
- module CacheSweeper
3
- class Logger
4
- class << self
5
- def log(level, message, context = {})
6
- return unless should_log?(level)
7
-
8
- formatted_message = format_message(message, level, context)
9
- CacheSweeper.logger.send(level, formatted_message)
10
- end
11
-
12
- def debug(message, context = {})
13
- log(:debug, message, context)
14
- end
15
-
16
- def info(message, context = {})
17
- log(:info, message, context)
18
- end
19
-
20
- def warn(message, context = {})
21
- log(:warn, message, context)
22
- end
23
-
24
- def error(message, context = {})
25
- log(:error, message, context)
26
- end
27
-
28
- def log_initialization(message, context = {})
29
- info("Initialization: #{message}", context)
30
- end
31
-
32
- def log_rule_execution(rule, record, result, context = {})
33
- sweeper_name = rule[:sweeper_class]&.name || 'Unknown'
34
- model_name = record.class.name
35
- record_id = record.respond_to?(:id) ? record.id : 'unknown'
36
-
37
- message = "Rule execution: #{sweeper_name} -> #{model_name}##{record_id}"
38
- context.merge!({
39
- sweeper: sweeper_name,
40
- model: model_name,
41
- record_id: record_id,
42
- association: rule[:association],
43
- attributes: rule[:attributes],
44
- condition_result: result,
45
- batching_mode: rule[:batching_mode],
46
- async: rule[:async]
47
- })
48
-
49
- debug(message, context)
50
- end
51
-
52
- def log_performance(operation, duration, context = {})
53
- message = "Performance: #{operation} took #{duration.round(3)}ms"
54
- context.merge!({
55
- operation: operation,
56
- duration_ms: duration.round(3)
57
- })
58
-
59
- debug(message, context)
60
- end
61
-
62
- def log_error(error, context = {})
63
- message = "Error: #{error.class.name}: #{error.message}"
64
- context.merge!({
65
- error_class: error.class.name,
66
- error_message: error.message,
67
- backtrace: error.backtrace&.first(5)
68
- })
69
-
70
- error(message, context)
71
- end
72
-
73
- def log_cache_operations(message, level = :info, context = {})
74
- log(level, message, context)
75
- end
76
-
77
- def log_async_jobs(message, level = :info, context = {})
78
- log(level, message, context)
79
- end
80
-
81
- def log_middleware(message, level = :info, context = {})
82
- log(level, message, context)
83
- end
84
-
85
- private
86
-
87
- def should_log?(level)
88
- return false unless CacheSweeper.logger
89
-
90
- level_priority = { debug: 0, info: 1, warn: 2, error: 3 }
91
- current_level_priority = level_priority[CacheSweeper.log_level] || 1
92
- requested_level_priority = level_priority[level] || 1
93
-
94
- requested_level_priority >= current_level_priority
95
- end
96
-
97
- def format_message(message, level, context)
98
- timestamp = Time.current.strftime("%Y-%m-%d %H:%M:%S.%3N")
99
- context_str = context.any? ? " #{context.inspect}" : ""
100
- "[CacheSweeper] [#{timestamp}] [#{level.to_s.upcase}] #{message}#{context_str}"
101
- end
102
- end
103
- end
104
- end
@@ -1,4 +0,0 @@
1
- require 'rake'
2
-
3
- namespace :cache_sweeper do
4
- end