rails-cache-debugger 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d044780076ea1a5444001c65e272ea04c23de3343be4dd80f88b0ae53610a3ca
4
+ data.tar.gz: 809334f959a100c0cef799cff3f8f7ef356bff39809954e175bba41265d074b0
5
+ SHA512:
6
+ metadata.gz: e5d889ea1be88fda518f43d646366bdb0744cba581861e8034f46aa8252a7b9b971770c78e2997ee3a277fd24a9eec703aca3d0c921ee538c81705576491aa4b
7
+ data.tar.gz: 8a2625f6a3faa57d2eaf539f1b9d683a477cb79f29dddb8725a37cc37fe081e0371bb09ba36bdc9bbcfbcbf663e36616add8ad88f0fb5941df787f71d280576b
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-05-22
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Eduardo Wanderley de Siqueira Araújo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # Rails Cache Debugger
2
+
3
+ A gem to help debug Rails cache operations by providing visibility into cache hits, misses, writes, and deletes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rails-cache-debugger'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ gem install rails-cache-debugger
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Basic Usage
28
+
29
+ ```ruby
30
+ # Configure the debugger (optional)
31
+ Rails::Cache::Debugger.configure do |config|
32
+ config.enabled = true
33
+ config.log_events = [
34
+ "cache_read.active_support",
35
+ "cache_write.active_support",
36
+ "cache_fetch_hit.active_support"
37
+ ]
38
+ end
39
+
40
+ # Use the debugger
41
+ cache = Rails.cache
42
+ debugger = Rails::Cache::Debugger.new(cache)
43
+
44
+ # Your cache operations will be automatically logged
45
+ debugger.write("key", "value")
46
+ debugger.read("key")
47
+ debugger.fetch("key") { "value" }
48
+ ```
49
+
50
+ ### Output Example
51
+
52
+ ```
53
+ [CacheDebugger] WRITE key: key (1.23ms)
54
+ [CacheDebugger] HIT key: key (0.45ms)
55
+ [CacheDebugger] FETCH_HIT key: key (0.67ms)
56
+ ```
57
+
58
+ ### Available Operations
59
+
60
+ The debugger supports all standard Rails cache operations:
61
+
62
+ - `read(key, **options)`
63
+ - `write(key, value, **options)`
64
+ - `delete(key, **options)`
65
+ - `exist?(key, **options)`
66
+ - `fetch(key, **options) { block }`
67
+
68
+ ### Configuration Options
69
+
70
+ ```ruby
71
+ Rails::Cache::Debugger.configure do |config|
72
+ # Enable/disable the debugger
73
+ config.enabled = true
74
+
75
+ # Configure which events to log
76
+ config.log_events = [
77
+ "cache_read.active_support",
78
+ "cache_write.active_support",
79
+ "cache_fetch_hit.active_support"
80
+ ]
81
+ end
82
+ ```
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
87
+
88
+ To install this gem onto your local machine, run `bundle exec rake install`.
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/eduardowanderleyde/rails-cache-debugger>.
93
+
94
+ ## License
95
+
96
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Cache
5
+ class Debugger
6
+ class Configuration
7
+ attr_accessor :enabled, :log_events
8
+
9
+ def initialize
10
+ @enabled = true
11
+ @log_events = %w[
12
+ cache_read.active_support
13
+ cache_write.active_support
14
+ cache_fetch_hit.active_support
15
+ ]
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+
5
+ module Rails
6
+ module Cache
7
+ class Debugger
8
+ class Railtie < Rails::Railtie
9
+ initializer "rails-cache-debugger.setup" do
10
+ subscriber = Subscriber.new
11
+
12
+ %w[
13
+ cache_read.active_support
14
+ cache_write.active_support
15
+ cache_fetch_hit.active_support
16
+ ].each do |event|
17
+ ActiveSupport::Notifications.subscribe(event, subscriber)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Cache
5
+ class Debugger
6
+ class Subscriber
7
+ def call(name, start, finish, _id, payload)
8
+ return unless configuration.enabled
9
+ return unless configuration.log_events.include?(name)
10
+
11
+ key = payload[:key]
12
+ duration = ((finish - start) * 1000).round(2)
13
+
14
+ case name
15
+ when "cache_read.active_support"
16
+ hit = payload[:hit]
17
+ Debugger.log "#{hit ? 'HIT' : 'MISS'} key: #{key} (#{duration}ms)"
18
+ when "cache_write.active_support"
19
+ Debugger.log "WRITE key: #{key} (#{duration}ms)"
20
+ when "cache_fetch_hit.active_support"
21
+ Debugger.log "FETCH_HIT key: #{key} (#{duration}ms)"
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def configuration
28
+ Debugger.configuration
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Cache
5
+ class Debugger
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/cache"
4
+ require "active_support/notifications"
5
+ require_relative "debugger/configuration"
6
+ require_relative "debugger/subscriber"
7
+ require_relative "debugger/railtie"
8
+
9
+ module Rails
10
+ module Cache
11
+ class Debugger
12
+ class << self
13
+ def log(message)
14
+ puts message
15
+ end
16
+
17
+ def configuration
18
+ @configuration ||= Configuration.new
19
+ end
20
+
21
+ def configure
22
+ yield configuration
23
+ end
24
+ end
25
+
26
+ def initialize(cache)
27
+ @cache = cache
28
+ end
29
+
30
+ def read(key, **options)
31
+ start_time = Time.now
32
+ value = @cache.read(key, **options)
33
+ duration = ((Time.now - start_time) * 1000).round(2)
34
+ log_cache_event(
35
+ event: value.nil? ? "cache_read.miss" : "cache_read.hit",
36
+ key: key,
37
+ value: value,
38
+ duration: duration
39
+ )
40
+ value
41
+ end
42
+
43
+ def write(key, value, **options)
44
+ start_time = Time.now
45
+ result = @cache.write(key, value, **options)
46
+ duration = ((Time.now - start_time) * 1000).round(2)
47
+ log_cache_event(
48
+ event: "cache_write",
49
+ key: key,
50
+ value: value,
51
+ duration: duration
52
+ )
53
+ result
54
+ end
55
+
56
+ def delete(key, **options)
57
+ start_time = Time.now
58
+ result = @cache.delete(key, **options)
59
+ duration = ((Time.now - start_time) * 1000).round(2)
60
+ log_cache_event(
61
+ event: "cache_delete",
62
+ key: key,
63
+ duration: duration
64
+ )
65
+ result
66
+ end
67
+
68
+ def exist?(key, **options)
69
+ start_time = Time.now
70
+ exists = @cache.exist?(key, **options)
71
+ duration = ((Time.now - start_time) * 1000).round(2)
72
+ log_cache_event(
73
+ event: "cache_exist",
74
+ key: key,
75
+ exists: exists,
76
+ duration: duration
77
+ )
78
+ exists
79
+ end
80
+
81
+ def fetch(key, **options)
82
+ start_time = Time.now
83
+ value = @cache.read(key, **options)
84
+ if value.nil?
85
+ value = yield
86
+ @cache.write(key, value, **options)
87
+ duration = ((Time.now - start_time) * 1000).round(2)
88
+ log_cache_event(
89
+ event: "cache_fetch.miss",
90
+ key: key,
91
+ value: value,
92
+ duration: duration
93
+ )
94
+ else
95
+ duration = ((Time.now - start_time) * 1000).round(2)
96
+ log_cache_event(
97
+ event: "cache_fetch.hit",
98
+ key: key,
99
+ value: value,
100
+ duration: duration
101
+ )
102
+ end
103
+ value
104
+ end
105
+
106
+ private
107
+
108
+ def log_cache_event(event:, key:, **details)
109
+ ActiveSupport::Notifications.instrument(
110
+ "cache_debugger.#{event}",
111
+ { key: key }.merge(details)
112
+ )
113
+ end
114
+ end
115
+ end
116
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-cache-debugger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eduardo Wanderley de Siqueira Araújo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 6.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 6.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ description: Provides visibility into Rails cache operations by logging cache hits,
84
+ misses, writes, and deletes
85
+ email:
86
+ - wanderley.eduardo@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - CHANGELOG.md
92
+ - LICENSE.txt
93
+ - README.md
94
+ - lib/rails/cache/debugger.rb
95
+ - lib/rails/cache/debugger/configuration.rb
96
+ - lib/rails/cache/debugger/railtie.rb
97
+ - lib/rails/cache/debugger/subscriber.rb
98
+ - lib/rails/cache/debugger/version.rb
99
+ homepage: https://github.com/yourusername/rails-cache-debugger
100
+ licenses:
101
+ - MIT
102
+ metadata:
103
+ homepage_uri: https://github.com/yourusername/rails-cache-debugger
104
+ source_code_uri: https://github.com/yourusername/rails-cache-debugger
105
+ changelog_uri: https://github.com/yourusername/rails-cache-debugger/blob/main/CHANGELOG.md
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: 3.0.0
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubygems_version: 3.4.10
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: A gem to help debug Rails cache operations
125
+ test_files: []