cache_sweeper 0.1.1 → 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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -0
- data/README.md +125 -498
- data/lib/cache_sweeper/base.rb +11 -24
- data/lib/cache_sweeper/batch.rb +68 -0
- data/lib/cache_sweeper/callbacks.rb +13 -0
- data/lib/cache_sweeper/configuration.rb +53 -0
- data/lib/cache_sweeper/delete_job.rb +9 -0
- data/lib/cache_sweeper/delivery.rb +36 -0
- data/lib/cache_sweeper/dispatcher.rb +27 -0
- data/lib/cache_sweeper/executor_hook.rb +15 -0
- data/lib/cache_sweeper/key.rb +22 -0
- data/lib/cache_sweeper/log_subscriber.rb +25 -0
- data/lib/cache_sweeper/railtie.rb +21 -6
- data/lib/cache_sweeper/registry.rb +28 -0
- data/lib/cache_sweeper/rule.rb +77 -0
- data/lib/cache_sweeper/version.rb +3 -1
- data/lib/cache_sweeper.rb +27 -85
- metadata +66 -33
- data/CODE_OF_CONDUCT.md +0 -74
- data/lib/cache_sweeper/async_worker.rb +0 -114
- data/lib/cache_sweeper/dsl.rb +0 -32
- data/lib/cache_sweeper/flush_middleware.rb +0 -147
- data/lib/cache_sweeper/loader.rb +0 -360
- data/lib/cache_sweeper/logger.rb +0 -104
- data/lib/tasks/cache_sweeper.rake +0 -4
data/lib/cache_sweeper/base.rb
CHANGED
|
@@ -1,33 +1,20 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
2
3
|
module CacheSweeper
|
|
3
4
|
class Base
|
|
4
|
-
|
|
5
|
+
class_attribute :delivery_mode, :delivery_queue, instance_accessor: false
|
|
5
6
|
|
|
6
7
|
class << self
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def sweeper_options(options = {})
|
|
12
|
-
@trigger = options[:trigger] if options.key?(:trigger)
|
|
13
|
-
@mode = options[:mode] if options.key?(:mode)
|
|
14
|
-
@queue = options[:queue] if options.key?(:queue)
|
|
15
|
-
@sidekiq_options = options[:sidekiq_options] if options.key?(:sidekiq_options)
|
|
16
|
-
CacheSweeper.validate_async_mode(@mode, "sweeper #{self.name}")
|
|
8
|
+
def delivery(mode, queue: nil)
|
|
9
|
+
Delivery.validate!(mode, queue:)
|
|
10
|
+
self.delivery_mode = mode
|
|
11
|
+
self.delivery_queue = queue
|
|
17
12
|
end
|
|
18
13
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def call_condition(condition, *args)
|
|
24
|
-
case condition
|
|
25
|
-
when Proc
|
|
26
|
-
instance_exec(*args, &condition)
|
|
27
|
-
when Symbol, String
|
|
28
|
-
send(condition, *args)
|
|
29
|
-
else
|
|
30
|
-
true
|
|
14
|
+
def watch(model, on: Rule::EVENTS, only: nil, delivery: nil, queue: nil, **conditions, &keys)
|
|
15
|
+
CacheSweeper.registry.add(
|
|
16
|
+
Rule.new(sweeper: self, model:, events: on, attributes: only, conditions:, delivery:, queue:, keys:)
|
|
17
|
+
)
|
|
31
18
|
end
|
|
32
19
|
end
|
|
33
20
|
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CacheSweeper
|
|
4
|
+
class Batch
|
|
5
|
+
STATE_KEY = :cache_sweeper_batch
|
|
6
|
+
|
|
7
|
+
class << self
|
|
8
|
+
def current
|
|
9
|
+
ActiveSupport::IsolatedExecutionState[STATE_KEY]
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def open
|
|
13
|
+
ActiveSupport::IsolatedExecutionState[STATE_KEY] = new
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def close
|
|
17
|
+
ActiveSupport::IsolatedExecutionState.delete(STATE_KEY)&.flush
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize
|
|
22
|
+
reset
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def delete(keys)
|
|
26
|
+
@deletions.merge(keys)
|
|
27
|
+
flush if full?
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def enqueue(keys, queue)
|
|
31
|
+
@enqueues[queue].merge(keys)
|
|
32
|
+
flush if full?
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def flush
|
|
36
|
+
deletions = @deletions.to_a
|
|
37
|
+
enqueues = @enqueues.transform_values(&:to_a)
|
|
38
|
+
reset
|
|
39
|
+
flush_deletions(deletions)
|
|
40
|
+
flush_enqueues(enqueues)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def reset
|
|
46
|
+
@deletions = Set.new
|
|
47
|
+
@enqueues = Hash.new { |hash, queue| hash[queue] = Set.new }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def full?
|
|
51
|
+
@deletions.size + @enqueues.sum { |_queue, keys| keys.size } >= CacheSweeper.config.max_pending_keys
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def flush_deletions(keys)
|
|
55
|
+
Delivery::Immediate.delete(keys) if keys.any?
|
|
56
|
+
rescue StandardError => e
|
|
57
|
+
CacheSweeper.report(e, keys:)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def flush_enqueues(enqueues)
|
|
61
|
+
enqueues.each do |queue, keys|
|
|
62
|
+
Delivery::Immediate.enqueue(keys, queue)
|
|
63
|
+
rescue StandardError => e
|
|
64
|
+
CacheSweeper.report(e, keys:, queue:)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CacheSweeper
|
|
4
|
+
class Configuration
|
|
5
|
+
attr_reader :delivery, :queue, :batch_size, :max_pending_keys
|
|
6
|
+
attr_accessor :raise_errors, :force_inline, :sweeper_paths
|
|
7
|
+
attr_writer :cache_store
|
|
8
|
+
|
|
9
|
+
def initialize
|
|
10
|
+
@delivery = :inline
|
|
11
|
+
@queue = :default
|
|
12
|
+
@batch_size = 100
|
|
13
|
+
@max_pending_keys = 1_000
|
|
14
|
+
@raise_errors = false
|
|
15
|
+
@force_inline = false
|
|
16
|
+
@sweeper_paths = ["app/sweepers"]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def cache_store
|
|
20
|
+
@cache_store || rails_cache || raise(ConfigurationError, "Set CacheSweeper.config.cache_store, Rails.cache is unavailable")
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def delivery=(mode)
|
|
24
|
+
@delivery = Delivery.validate!(mode)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def queue=(name)
|
|
28
|
+
raise ConfigurationError, "queue must be a Symbol or String, got #{name.inspect}" unless name in Symbol | String
|
|
29
|
+
|
|
30
|
+
@queue = name
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def batch_size=(size)
|
|
34
|
+
@batch_size = positive_integer!(:batch_size, size)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def max_pending_keys=(count)
|
|
38
|
+
@max_pending_keys = positive_integer!(:max_pending_keys, count)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def rails_cache
|
|
44
|
+
Rails.cache if defined?(Rails.cache)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def positive_integer!(name, value)
|
|
48
|
+
return value if value.is_a?(Integer) && value.positive?
|
|
49
|
+
|
|
50
|
+
raise ConfigurationError, "#{name} must be a positive Integer, got #{value.inspect}"
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CacheSweeper
|
|
4
|
+
module Delivery
|
|
5
|
+
MODES = %i[inline batched async].freeze
|
|
6
|
+
|
|
7
|
+
class << self
|
|
8
|
+
def validate!(mode, queue: nil)
|
|
9
|
+
raise ConfigurationError, "Unknown delivery #{mode.inspect}, expected one of #{MODES.inspect}" unless MODES.include?(mode)
|
|
10
|
+
raise ConfigurationError, "queue only applies to :async delivery, got #{mode.inspect}" if queue && mode != :async
|
|
11
|
+
|
|
12
|
+
mode
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def call(keys, mode:, queue:)
|
|
16
|
+
return CacheSweeper.delete(*keys) if mode == :inline || CacheSweeper.config.force_inline
|
|
17
|
+
|
|
18
|
+
target = Batch.current || Immediate
|
|
19
|
+
mode == :async ? target.enqueue(keys, queue) : target.delete(keys)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
module Immediate
|
|
24
|
+
class << self
|
|
25
|
+
def delete(keys)
|
|
26
|
+
CacheSweeper.delete(*keys)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def enqueue(keys, queue)
|
|
30
|
+
jobs = keys.each_slice(CacheSweeper.config.batch_size).map { |slice| DeleteJob.new(slice).set(queue:) }
|
|
31
|
+
ActiveJob.perform_all_later(jobs)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CacheSweeper
|
|
4
|
+
module Dispatcher
|
|
5
|
+
class << self
|
|
6
|
+
def call(record, event)
|
|
7
|
+
CacheSweeper.registry.rules_for(record.class).each { |rule| dispatch(rule, record, event) }
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
def dispatch(rule, record, event)
|
|
13
|
+
return unless rule.matches?(record, event)
|
|
14
|
+
|
|
15
|
+
keys = rule.keys_for(record)
|
|
16
|
+
return if keys.empty?
|
|
17
|
+
|
|
18
|
+
payload = { sweeper: rule.sweeper, record:, event:, keys:, delivery: rule.delivery_mode }
|
|
19
|
+
ActiveSupport::Notifications.instrument("sweep.cache_sweeper", payload) do
|
|
20
|
+
Delivery.call(keys, mode: rule.delivery_mode, queue: rule.queue_name)
|
|
21
|
+
end
|
|
22
|
+
rescue StandardError => e
|
|
23
|
+
CacheSweeper.report(e, sweeper: rule.sweeper.name, model: record.class.name, record_id: record.id, event:)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CacheSweeper
|
|
4
|
+
# Mirrors ActiveSupport::Cache::Store#expanded_key so swept keys match the keys the store wrote.
|
|
5
|
+
module Key
|
|
6
|
+
class << self
|
|
7
|
+
def list(value)
|
|
8
|
+
Array.wrap(value).map { |key| expand(key) }.compact_blank.uniq
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def expand(key)
|
|
12
|
+
return key.cache_key.to_s if key.respond_to?(:cache_key)
|
|
13
|
+
|
|
14
|
+
case key
|
|
15
|
+
when Array then key.many? ? key.map { |part| expand(part) }.to_param : expand(key.first)
|
|
16
|
+
when Hash then key.map { |name, value| "#{name}=#{value}" }.sort!.to_param
|
|
17
|
+
else key.to_param
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CacheSweeper
|
|
4
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
|
5
|
+
def sweep(event)
|
|
6
|
+
payload = event.payload
|
|
7
|
+
debug do
|
|
8
|
+
"CacheSweeper #{payload[:sweeper]} swept #{payload[:keys].inspect} (#{payload[:delivery]}) " \
|
|
9
|
+
"on #{payload[:event]} of #{payload[:record].class}##{payload[:record].id}"
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
subscribe_log_level :sweep, :debug
|
|
13
|
+
|
|
14
|
+
def delete(event)
|
|
15
|
+
debug { "CacheSweeper deleted #{event.payload[:keys].inspect} (#{event.duration.round(1)}ms)" }
|
|
16
|
+
end
|
|
17
|
+
subscribe_log_level :delete, :debug
|
|
18
|
+
|
|
19
|
+
def failure(event)
|
|
20
|
+
payload = event.payload
|
|
21
|
+
error { "CacheSweeper failed: #{payload[:error].class}: #{payload[:error].message} #{payload.except(:error).inspect}" }
|
|
22
|
+
end
|
|
23
|
+
subscribe_log_level :failure, :error
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -1,13 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module CacheSweeper
|
|
2
4
|
class Railtie < Rails::Railtie
|
|
3
|
-
initializer "cache_sweeper.
|
|
4
|
-
CacheSweeper.
|
|
5
|
+
initializer "cache_sweeper.defaults", before: :load_config_initializers do
|
|
6
|
+
CacheSweeper.config.raise_errors = Rails.env.local?
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
initializer "cache_sweeper.log_subscriber" do
|
|
10
|
+
CacheSweeper::LogSubscriber.attach_to :cache_sweeper
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
initializer "cache_sweeper.executor" do |app|
|
|
14
|
+
app.executor.register_hook(CacheSweeper::ExecutorHook)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
initializer "cache_sweeper.reloader" do |app|
|
|
18
|
+
app.reloader.before_class_unload { CacheSweeper.registry.clear }
|
|
5
19
|
end
|
|
6
20
|
|
|
7
|
-
config.
|
|
8
|
-
CacheSweeper.
|
|
9
|
-
|
|
10
|
-
|
|
21
|
+
config.to_prepare do
|
|
22
|
+
CacheSweeper.config.sweeper_paths.each do |path|
|
|
23
|
+
directory = Rails.root.join(path)
|
|
24
|
+
Rails.autoloaders.main.eager_load_dir(directory) if directory.directory?
|
|
25
|
+
end
|
|
11
26
|
end
|
|
12
27
|
end
|
|
13
28
|
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CacheSweeper
|
|
4
|
+
class Registry
|
|
5
|
+
def initialize
|
|
6
|
+
@rules = Hash.new { |hash, model| hash[model] = [] }
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def add(rule)
|
|
10
|
+
base_class = rule.model.base_class
|
|
11
|
+
base_class.include(Callbacks) unless base_class < Callbacks
|
|
12
|
+
@rules[rule.model] << rule
|
|
13
|
+
rule
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def rules_for(model)
|
|
17
|
+
model.ancestors.take_while { |ancestor| ancestor != ActiveRecord::Base }.flat_map { |ancestor| @rules.fetch(ancestor, []) }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def rules
|
|
21
|
+
@rules.values.flatten
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def clear
|
|
25
|
+
@rules.clear
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CacheSweeper
|
|
4
|
+
class Rule
|
|
5
|
+
EVENTS = %i[create update destroy].freeze
|
|
6
|
+
CONDITIONS = %i[if unless].freeze
|
|
7
|
+
|
|
8
|
+
attr_reader :sweeper, :model, :events, :attributes, :conditions, :delivery, :queue, :keys
|
|
9
|
+
|
|
10
|
+
def initialize(sweeper:, model:, events:, attributes:, conditions:, delivery:, queue:, keys:)
|
|
11
|
+
@sweeper = sweeper
|
|
12
|
+
@model = validate_model!(model)
|
|
13
|
+
@events = validate_events!(Array(events))
|
|
14
|
+
@attributes = Array(attributes).map(&:to_s).presence
|
|
15
|
+
@conditions = validate_conditions!(conditions)
|
|
16
|
+
@delivery = delivery && Delivery.validate!(delivery, queue:)
|
|
17
|
+
@queue = queue
|
|
18
|
+
@keys = keys || raise(ConfigurationError, "#{sweeper} must pass a block to `watch #{model}` returning the cache keys")
|
|
19
|
+
freeze
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def matches?(record, event)
|
|
23
|
+
events.include?(event) && (event != :update || attributes.nil? || record.saved_changes.keys.intersect?(attributes))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def keys_for(record)
|
|
27
|
+
instance = sweeper.new
|
|
28
|
+
return [] unless conditions_met?(instance, record)
|
|
29
|
+
|
|
30
|
+
Key.list(instance.instance_exec(record, &keys))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def delivery_mode
|
|
34
|
+
delivery || sweeper.delivery_mode || CacheSweeper.config.delivery
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def queue_name
|
|
38
|
+
queue || sweeper.delivery_queue || CacheSweeper.config.queue
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def conditions_met?(instance, record)
|
|
44
|
+
conditions.all? do |kind, condition|
|
|
45
|
+
result = condition.is_a?(Symbol) ? record.public_send(condition) : instance.instance_exec(record, &condition)
|
|
46
|
+
kind == :if ? result : !result
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def validate_model!(model)
|
|
51
|
+
unless model.is_a?(Class) && model < ActiveRecord::Base && !model.abstract_class?
|
|
52
|
+
raise ConfigurationError, "#{sweeper} can only watch concrete ActiveRecord models, got #{model.inspect}"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
model
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def validate_events!(events)
|
|
59
|
+
unknown = events - EVENTS
|
|
60
|
+
raise ConfigurationError, "#{sweeper} watches unknown events #{unknown.inspect}, expected #{EVENTS.inspect}" if unknown.any?
|
|
61
|
+
raise ConfigurationError, "#{sweeper} must watch at least one of #{EVENTS.inspect}" if events.empty?
|
|
62
|
+
|
|
63
|
+
events
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def validate_conditions!(conditions)
|
|
67
|
+
unknown = conditions.keys - CONDITIONS
|
|
68
|
+
raise ConfigurationError, "#{sweeper} passed unknown options #{unknown.inspect} to `watch #{model}`" if unknown.any?
|
|
69
|
+
|
|
70
|
+
conditions.each do |kind, condition|
|
|
71
|
+
next if condition.is_a?(Symbol) || condition.is_a?(Proc)
|
|
72
|
+
|
|
73
|
+
raise ConfigurationError, "#{sweeper} `#{kind}:` must be a Symbol or Proc, got #{condition.inspect}"
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
data/lib/cache_sweeper.rb
CHANGED
|
@@ -1,105 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
require "
|
|
4
|
-
require "
|
|
5
|
-
require "
|
|
6
|
-
require "
|
|
7
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_record"
|
|
4
|
+
require "active_job"
|
|
5
|
+
require "active_support"
|
|
6
|
+
require "zeitwerk"
|
|
7
|
+
|
|
8
|
+
loader = Zeitwerk::Loader.for_gem
|
|
9
|
+
loader.ignore("#{__dir__}/cache_sweeper/railtie.rb")
|
|
10
|
+
loader.setup
|
|
8
11
|
|
|
9
12
|
module CacheSweeper
|
|
10
13
|
class Error < StandardError; end
|
|
11
|
-
|
|
12
|
-
DEFAULTS = {
|
|
13
|
-
log_level: :info,
|
|
14
|
-
trigger: :instant,
|
|
15
|
-
mode: :inline,
|
|
16
|
-
queue: :default,
|
|
17
|
-
sidekiq_options: {},
|
|
18
|
-
delete_multi_batch_size: 100
|
|
19
|
-
}.freeze
|
|
14
|
+
class ConfigurationError < Error; end
|
|
20
15
|
|
|
21
16
|
class << self
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def configure
|
|
25
|
-
yield self
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def configure_defaults
|
|
29
|
-
DEFAULTS.each { |k, v| instance_variable_set("@#{k}", v) }
|
|
30
|
-
@log_level =
|
|
31
|
-
if Rails.env.development?
|
|
32
|
-
:debug
|
|
33
|
-
elsif Rails.env.production?
|
|
34
|
-
:warn
|
|
35
|
-
else
|
|
36
|
-
:info
|
|
37
|
-
end
|
|
17
|
+
def config
|
|
18
|
+
@config ||= Configuration.new
|
|
38
19
|
end
|
|
39
20
|
|
|
40
|
-
def
|
|
41
|
-
|
|
42
|
-
unless valid_levels.include?(level)
|
|
43
|
-
raise ArgumentError,
|
|
44
|
-
"Invalid log level: #{level}. Must be one of: #{valid_levels.join(', ')}"
|
|
45
|
-
end
|
|
46
|
-
@log_level = level
|
|
21
|
+
def configure
|
|
22
|
+
yield config
|
|
47
23
|
end
|
|
48
24
|
|
|
49
|
-
def
|
|
50
|
-
@
|
|
25
|
+
def registry
|
|
26
|
+
@registry ||= Registry.new
|
|
51
27
|
end
|
|
52
28
|
|
|
53
|
-
def
|
|
54
|
-
|
|
55
|
-
|
|
29
|
+
def delete(*keys)
|
|
30
|
+
keys = Key.list(keys)
|
|
31
|
+
return if keys.empty?
|
|
56
32
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
warn "CacheSweeper Warning: #{context} has mode set to :async but Sidekiq is not available. " \
|
|
60
|
-
"Async jobs will be executed synchronously. " \
|
|
61
|
-
"Add 'gem \"sidekiq\"' to your Gemfile to enable async processing."
|
|
33
|
+
ActiveSupport::Notifications.instrument("delete.cache_sweeper", keys:) do
|
|
34
|
+
keys.each_slice(config.batch_size) { |slice| config.cache_store.delete_multi(slice) }
|
|
62
35
|
end
|
|
63
36
|
end
|
|
64
37
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return 0 if keys_array.empty?
|
|
38
|
+
def report(error, **context)
|
|
39
|
+
raise error if config.raise_errors
|
|
68
40
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
failed_count = 0
|
|
72
|
-
|
|
73
|
-
keys_array.each_slice(batch_size) do |batch|
|
|
74
|
-
begin
|
|
75
|
-
Rails.cache.delete_multi(batch)
|
|
76
|
-
deleted_count += batch.length
|
|
77
|
-
CacheSweeper::Logger.log_cache_operations("Deleted batch of #{batch.length} keys", :debug, context.merge({
|
|
78
|
-
batch_size: batch.length,
|
|
79
|
-
keys: batch
|
|
80
|
-
}))
|
|
81
|
-
rescue => e
|
|
82
|
-
failed_count += batch.length
|
|
83
|
-
CacheSweeper::Logger.log_error(e, context.merge({
|
|
84
|
-
batch_size: batch.length,
|
|
85
|
-
keys: batch,
|
|
86
|
-
error_type: 'delete_multi_error'
|
|
87
|
-
}))
|
|
88
|
-
end
|
|
41
|
+
ActiveSupport::Notifications.instrument("failure.cache_sweeper", error:, **context)
|
|
42
|
+
ActiveSupport.error_reporter.report(error, handled: true, source: "cache_sweeper", context:)
|
|
89
43
|
end
|
|
90
|
-
|
|
91
|
-
CacheSweeper::Logger.log_cache_operations("Batch deletion completed", :info, context.merge({
|
|
92
|
-
total_keys: keys_array.length,
|
|
93
|
-
deleted_count: deleted_count,
|
|
94
|
-
failed_count: failed_count,
|
|
95
|
-
batch_size: batch_size
|
|
96
|
-
}))
|
|
97
|
-
|
|
98
|
-
deleted_count
|
|
99
|
-
end
|
|
100
44
|
end
|
|
101
45
|
end
|
|
102
46
|
|
|
103
|
-
if defined?(Rails)
|
|
104
|
-
require "cache_sweeper/railtie"
|
|
105
|
-
end
|
|
47
|
+
require_relative "cache_sweeper/railtie" if defined?(Rails::Railtie)
|