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.
metadata CHANGED
@@ -1,87 +1,122 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_sweeper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafay Qayyum
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2025-09-29 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
- name: request_store
13
+ name: activejob
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
- - - "~>"
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.1'
19
+ - - "<"
18
20
  - !ruby/object:Gem::Version
19
- version: '1.7'
21
+ version: '9'
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
23
25
  requirements:
24
- - - "~>"
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '7.1'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '9'
32
+ - !ruby/object:Gem::Dependency
33
+ name: activerecord
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '7.1'
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: '9'
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
25
47
  - !ruby/object:Gem::Version
26
- version: '1.7'
48
+ version: '7.1'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '9'
27
52
  - !ruby/object:Gem::Dependency
28
- name: rails
53
+ name: activesupport
29
54
  requirement: !ruby/object:Gem::Requirement
30
55
  requirements:
31
56
  - - ">="
32
57
  - !ruby/object:Gem::Version
33
- version: '6.0'
58
+ version: '7.1'
34
59
  - - "<"
35
60
  - !ruby/object:Gem::Version
36
- version: '8.0'
61
+ version: '9'
37
62
  type: :runtime
38
63
  prerelease: false
39
64
  version_requirements: !ruby/object:Gem::Requirement
40
65
  requirements:
41
66
  - - ">="
42
67
  - !ruby/object:Gem::Version
43
- version: '6.0'
68
+ version: '7.1'
44
69
  - - "<"
45
70
  - !ruby/object:Gem::Version
46
- version: '8.0'
71
+ version: '9'
47
72
  - !ruby/object:Gem::Dependency
48
- name: sidekiq
73
+ name: zeitwerk
49
74
  requirement: !ruby/object:Gem::Requirement
50
75
  requirements:
51
76
  - - "~>"
52
77
  - !ruby/object:Gem::Version
53
- version: '6.0'
54
- type: :development
78
+ version: '2.6'
79
+ type: :runtime
55
80
  prerelease: false
56
81
  version_requirements: !ruby/object:Gem::Requirement
57
82
  requirements:
58
83
  - - "~>"
59
84
  - !ruby/object:Gem::Version
60
- version: '6.0'
61
- description:
62
- email: rafayqayyum786@gmail.com
85
+ version: '2.6'
86
+ description: Define which model changes invalidate which cache keys in dedicated sweeper
87
+ classes. Deletes inline, batched per request or job, or asynchronously through ActiveJob.
88
+ email:
89
+ - rafayqayyum786@gmail.com
63
90
  executables: []
64
91
  extensions: []
65
92
  extra_rdoc_files: []
66
93
  files:
67
- - CODE_OF_CONDUCT.md
94
+ - CHANGELOG.md
68
95
  - LICENSE.txt
69
96
  - README.md
70
97
  - lib/cache_sweeper.rb
71
- - lib/cache_sweeper/async_worker.rb
72
98
  - lib/cache_sweeper/base.rb
73
- - lib/cache_sweeper/dsl.rb
74
- - lib/cache_sweeper/flush_middleware.rb
75
- - lib/cache_sweeper/loader.rb
76
- - lib/cache_sweeper/logger.rb
99
+ - lib/cache_sweeper/batch.rb
100
+ - lib/cache_sweeper/callbacks.rb
101
+ - lib/cache_sweeper/configuration.rb
102
+ - lib/cache_sweeper/delete_job.rb
103
+ - lib/cache_sweeper/delivery.rb
104
+ - lib/cache_sweeper/dispatcher.rb
105
+ - lib/cache_sweeper/executor_hook.rb
106
+ - lib/cache_sweeper/key.rb
107
+ - lib/cache_sweeper/log_subscriber.rb
77
108
  - lib/cache_sweeper/railtie.rb
109
+ - lib/cache_sweeper/registry.rb
110
+ - lib/cache_sweeper/rule.rb
78
111
  - lib/cache_sweeper/version.rb
79
- - lib/tasks/cache_sweeper.rake
80
112
  homepage: https://github.com/rafayqayyum/cache_sweeper
81
113
  licenses:
82
114
  - MIT
83
- metadata: {}
84
- post_install_message:
115
+ metadata:
116
+ source_code_uri: https://github.com/rafayqayyum/cache_sweeper
117
+ changelog_uri: https://github.com/rafayqayyum/cache_sweeper/blob/main/CHANGELOG.md
118
+ bug_tracker_uri: https://github.com/rafayqayyum/cache_sweeper/issues
119
+ rubygems_mfa_required: 'true'
85
120
  rdoc_options: []
86
121
  require_paths:
87
122
  - lib
@@ -89,16 +124,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
124
  requirements:
90
125
  - - ">="
91
126
  - !ruby/object:Gem::Version
92
- version: 2.6.0
127
+ version: '3.3'
93
128
  required_rubygems_version: !ruby/object:Gem::Requirement
94
129
  requirements:
95
130
  - - ">="
96
131
  - !ruby/object:Gem::Version
97
132
  version: '0'
98
133
  requirements: []
99
- rubygems_version: 3.2.33
100
- signing_key:
134
+ rubygems_version: 3.6.9
101
135
  specification_version: 4
102
- summary: Flexible, rule-based cache invalidation for Rails with batching, async jobs,
103
- and association support.
136
+ summary: Declarative, rule-based cache invalidation for ActiveRecord models.
104
137
  test_files: []
data/CODE_OF_CONDUCT.md DELETED
@@ -1,74 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
11
-
12
- ## Our Standards
13
-
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
16
-
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
22
-
23
- Examples of unacceptable behavior by participants include:
24
-
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
28
- * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
31
- * Other conduct which could reasonably be considered inappropriate in a
32
- professional setting
33
-
34
- ## Our Responsibilities
35
-
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
39
-
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
45
-
46
- ## Scope
47
-
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
-
55
- ## Enforcement
56
-
57
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at rafayqayyum786@gmail.com. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
-
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
67
-
68
- ## Attribution
69
-
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [https://contributor-covenant.org/version/1/4][version]
72
-
73
- [homepage]: https://contributor-covenant.org
74
- [version]: https://contributor-covenant.org/version/1/4/
@@ -1,114 +0,0 @@
1
- # Sidekiq worker for async cache invalidation
2
- module CacheSweeper
3
- class AsyncWorker
4
- if defined?(Sidekiq)
5
- include Sidekiq::Worker
6
- end
7
-
8
- def perform(keys, trigger = :instant)
9
- start_time = Time.current
10
- keys_array = Array(keys)
11
- log_job_start(keys_array, trigger)
12
-
13
- deleted_count = CacheSweeper.delete_cache_keys(keys_array, {
14
- job_id: jid,
15
- mode: :async,
16
- trigger: trigger
17
- })
18
- failed_count = keys_array.length - deleted_count
19
-
20
- duration = (Time.current - start_time) * 1000
21
- log_job_completion(keys_array, deleted_count, failed_count, duration, [], trigger)
22
-
23
- rescue => e
24
- duration = (Time.current - start_time) * 1000
25
- log_job_error(e, keys_array, duration, trigger)
26
- raise e
27
- end
28
-
29
- def self.perform_async(keys, trigger = :instant)
30
- if defined?(Sidekiq) && self.ancestors.include?(Sidekiq::Worker)
31
- log_job_scheduling(keys, :sidekiq, trigger)
32
- self.set(sidekiq_opts).super(keys, trigger)
33
- else
34
- # In test environment or when Sidekiq is not available, perform synchronously
35
- log_job_scheduling(keys, :synchronous, trigger)
36
- new.perform(keys, trigger)
37
- end
38
- end
39
-
40
- private
41
-
42
- def log_job_start(keys, trigger)
43
- CacheSweeper::Logger.log_async_jobs("Async job started", :info, {
44
- job_id: jid,
45
- keys_count: keys.length,
46
- keys: keys,
47
- trigger: trigger
48
- })
49
- end
50
-
51
- def log_cache_deletion(keys, status, error = nil)
52
- case status
53
- when :success
54
- CacheSweeper::Logger.log_async_jobs("Cache deleted: #{Array(keys).length} keys", :debug, {
55
- job_id: jid,
56
- keys_count: Array(keys).length,
57
- keys: Array(keys),
58
- status: 'success'
59
- })
60
- when :error
61
- CacheSweeper::Logger.log_error(error, {
62
- job_id: jid,
63
- keys_count: Array(keys).length,
64
- keys: Array(keys),
65
- status: 'error',
66
- error_type: 'cache_delete_error'
67
- })
68
- end
69
- end
70
-
71
- def log_job_completion(keys, deleted_count, failed_count, duration, errors, trigger)
72
- CacheSweeper::Logger.log_async_jobs("Async job completed", :info, {
73
- job_id: jid,
74
- keys_count: keys.length,
75
- deleted_count: deleted_count,
76
- failed_count: failed_count,
77
- duration_ms: duration.round(3),
78
- errors: errors,
79
- trigger: trigger
80
- })
81
-
82
- CacheSweeper::Logger.log_performance("async_cache_deletion", duration, {
83
- job_id: jid,
84
- keys_count: keys.length,
85
- deleted_count: deleted_count,
86
- failed_count: failed_count,
87
- trigger: trigger
88
- })
89
- end
90
-
91
- def log_job_error(error, keys, duration, trigger)
92
- CacheSweeper::Logger.log_error(error, {
93
- job_id: jid,
94
- keys: keys,
95
- duration_ms: duration.round(3),
96
- error_type: 'async_job_error',
97
- trigger: trigger
98
- })
99
- end
100
-
101
- def self.log_job_scheduling(keys, method, trigger)
102
- CacheSweeper::Logger.log_async_jobs("Async job scheduled", :info, {
103
- method: method,
104
- keys_count: Array(keys).length,
105
- keys: Array(keys),
106
- trigger: trigger
107
- })
108
- end
109
-
110
- def jid
111
- @jid ||= SecureRandom.hex(8)
112
- end
113
- end
114
- end
@@ -1,32 +0,0 @@
1
- # lib/cache_sweeper/dsl.rb
2
- module CacheSweeper
3
- module DSL
4
- def self.included(base)
5
- base.extend ClassMethods
6
- end
7
-
8
- module ClassMethods
9
- def watch(association = nil, attributes: nil, if: nil, keys: nil, trigger: nil, mode: nil, queue: nil, sidekiq_options: nil, callback: nil, on: nil)
10
- CacheSweeper.validate_async_mode(mode, "rule in #{self.name}")
11
- @cache_sweeper_rules ||= []
12
- @cache_sweeper_rules << {
13
- association: association,
14
- attributes: attributes,
15
- condition: binding.local_variable_get(:if),
16
- keys: keys,
17
- trigger: trigger,
18
- mode: mode,
19
- queue: queue,
20
- sidekiq_options: sidekiq_options,
21
- callback: callback,
22
- on: on,
23
- sweeper_class: self
24
- }
25
- end
26
-
27
- def cache_sweeper_rules
28
- @cache_sweeper_rules || []
29
- end
30
- end
31
- end
32
- end
@@ -1,147 +0,0 @@
1
- require 'request_store'
2
-
3
- # Middleware to flush pending cache keys at the end of each request if batching is enabled
4
- class CacheSweeperFlushMiddleware
5
- def initialize(app)
6
- @app = app
7
- end
8
-
9
- def call(env)
10
- start_time = Time.current
11
- request_id = SecureRandom.hex(8)
12
-
13
- log_request_start(request_id, env)
14
-
15
- status, headers, response = @app.call(env)
16
-
17
- # At end of request, flush all request-level batched keys
18
- pending = RequestStore.store[:cache_sweeper_request_pending] || []
19
-
20
- if pending.any?
21
- log_request_flush_start(request_id, pending)
22
-
23
- async_jobs_scheduled = 0
24
- instant_deletions = 0
25
- failed_deletions = 0
26
- errors = []
27
-
28
- pending.each do |entry|
29
- keys, mode, sidekiq_options = entry.values_at(:keys, :mode, :sidekiq_options)
30
-
31
- begin
32
- if mode == :async
33
- CacheSweeper::AsyncWorker.set(sidekiq_options || {}).perform_async(keys, :request)
34
- async_jobs_scheduled += 1
35
- log_batch_processing(request_id, keys, :async_scheduled, sidekiq_options)
36
- else
37
- deleted_count = CacheSweeper.delete_cache_keys(keys, {
38
- request_id: request_id,
39
- mode: :inline,
40
- trigger: :request
41
- })
42
- instant_deletions += deleted_count
43
- failed_deletions += Array(keys).length - deleted_count
44
- end
45
- rescue => e
46
- errors << { keys: keys, error: e.message }
47
- log_batch_processing(request_id, keys, :batch_error, sidekiq_options, e)
48
- end
49
- end
50
-
51
- log_request_flush_completion(request_id, {
52
- total_batches: pending.length,
53
- async_jobs_scheduled: async_jobs_scheduled,
54
- instant_deletions: instant_deletions,
55
- failed_deletions: failed_deletions,
56
- errors: errors
57
- })
58
- else
59
- log_request_no_flush(request_id)
60
- end
61
-
62
- [status, headers, response]
63
- rescue => e
64
- duration = (Time.current - start_time) * 1000
65
- log_request_error(request_id, e, duration)
66
- raise e
67
- ensure
68
- RequestStore.store[:cache_sweeper_request_pending] = []
69
- duration = (Time.current - start_time) * 1000
70
- log_request_completion(request_id, duration)
71
- end
72
-
73
- private
74
-
75
- def log_request_start(request_id, env)
76
- CacheSweeper::Logger.log_middleware("Request started", :debug, {
77
- request_id: request_id,
78
- method: env['REQUEST_METHOD'],
79
- path: env['PATH_INFO']
80
- })
81
- end
82
-
83
- def log_request_flush_start(request_id, pending)
84
- total_keys = pending.sum { |entry| Array(entry[:keys]).length }
85
- async_count = pending.count { |entry| entry[:async] }
86
- instant_count = pending.length - async_count
87
-
88
- CacheSweeper::Logger.log_middleware("Request flush started", :info, {
89
- request_id: request_id,
90
- batch_count: pending.length,
91
- total_keys: total_keys,
92
- async_batches: async_count,
93
- instant_batches: instant_count
94
- })
95
- end
96
-
97
- def log_batch_processing(request_id, keys, status, sidekiq_options = nil, error = nil)
98
- case status
99
- when :async_scheduled
100
- CacheSweeper::Logger.log_middleware("Batch scheduled async", :debug, {
101
- request_id: request_id,
102
- keys: Array(keys),
103
- sidekiq_options: sidekiq_options
104
- })
105
- when :error
106
- CacheSweeper::Logger.log_error(error, {
107
- request_id: request_id,
108
- keys: Array(keys),
109
- error_type: 'batch_processing_error'
110
- })
111
- when :batch_error
112
- CacheSweeper::Logger.log_error(error, {
113
- request_id: request_id,
114
- keys: Array(keys),
115
- sidekiq_options: sidekiq_options,
116
- error_type: 'batch_error'
117
- })
118
- end
119
- end
120
-
121
- def log_request_flush_completion(request_id, stats)
122
- CacheSweeper::Logger.log_middleware("Request flush completed", :info, {
123
- request_id: request_id,
124
- **stats
125
- })
126
- end
127
-
128
- def log_request_no_flush(request_id)
129
- CacheSweeper::Logger.log_middleware("No cache flush needed", :debug, {
130
- request_id: request_id
131
- })
132
- end
133
-
134
- def log_request_error(request_id, error, duration)
135
- CacheSweeper::Logger.log_error(error, {
136
- request_id: request_id,
137
- duration_ms: duration.round(3),
138
- error_type: 'middleware_error'
139
- })
140
- end
141
-
142
- def log_request_completion(request_id, duration)
143
- CacheSweeper::Logger.log_performance("request_processing", duration, {
144
- request_id: request_id
145
- })
146
- end
147
- end