cache_inspector 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: 72b484d154e9c4745bbe02887ca73aeef4db58a0f9261edc1b1e62602a1413e7
4
+ data.tar.gz: db5cce0ba7e6eee21e744efdffc83a9882003d3fe7acb05a5269ffab06b07d17
5
+ SHA512:
6
+ metadata.gz: 8d16b5ba5b95487cd95ef0471514f2ae21efc462f5145c2af582490869e34c2b3e88145e7cd1298964a9a7070599f1f2c6af61b2dca00d22affabfdcf32ae28c
7
+ data.tar.gz: d136d30fd716f880241935603a0ee28e5657236c09c28e7d3c8bbc40d921ca9b3efc693a67400954c98127a181c323035c0b7d65e234385522206d20df7d71d3
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-11-02
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Chitradevi36
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,13 @@
1
+ # 🧠 CacheInspector
2
+
3
+ Track and debug cache usage in Rails — see every cache hit, miss, and write.
4
+
5
+ ---
6
+
7
+ ## 💎 Installation
8
+
9
+ Add to your Gemfile:
10
+
11
+ ```ruby
12
+ gem "cache_inspector"
13
+ ```
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
9
+
10
+ desc "Build gem"
11
+ task :build do
12
+ sh "gem build cache_inspector.gemspec"
13
+ end
14
+
15
+ desc "Release gem to RubyGems"
16
+ task :release do
17
+ version = File.read("lib/cache_inspector/version.rb")[/VERSION\s*=\s*["'](.+?)["']/, 1]
18
+ abort("Version not found") unless version
19
+ sh "gem build cache_inspector.gemspec"
20
+ sh "git add . && git commit -m 'Release v#{version}' || true"
21
+ sh "git tag v#{version}"
22
+ sh "git push origin main --tags"
23
+ sh "gem push cache_inspector-#{version}.gem"
24
+ end
@@ -0,0 +1,6 @@
1
+ CacheInspector.configure do |config|
2
+ config.enabled_environments = %i[development test]
3
+ config.warn_large_size_kb = 100 # warn if payload > 100KB
4
+ config.log_payloads = false
5
+ config.log_prefix = "[CacheInspector]"
6
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ module CacheInspector
3
+ class Config
4
+ attr_accessor :enabled_environments, :log_prefix, :warn_large_size_kb, :log_payloads
5
+
6
+ def initialize
7
+ @enabled_environments = %i[development test]
8
+ @log_prefix = "[CacheInspector]"
9
+ @warn_large_size_kb = 100 # warn if payload > 100 KB
10
+ @log_payloads = false
11
+ end
12
+
13
+ def enabled?(env)
14
+ @enabled_environments.map(&:to_sym).include?(env.to_sym)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+ require "active_support/notifications"
3
+
4
+ module CacheInspector
5
+ module Subscriber
6
+ EVENTS = %w[
7
+ cache_read.active_support
8
+ cache_generate.active_support
9
+ cache_write.active_support
10
+ cache_delete.active_support
11
+ cache_fetch_hit.active_support
12
+ ].freeze
13
+
14
+ def self.install!(config)
15
+ return if @installed
16
+
17
+ @config = config
18
+
19
+ EVENTS.each do |event|
20
+ ActiveSupport::Notifications.subscribe(event) do |*args|
21
+ event_obj = ActiveSupport::Notifications::Event.new(*args)
22
+ handle_event(event, event_obj)
23
+ end
24
+ end
25
+
26
+ @installed = true
27
+ end
28
+
29
+ def self.handle_event(event, e)
30
+ env = rails_env
31
+ return unless @config.enabled?(env)
32
+
33
+ key = e.payload[:key]
34
+ value = e.payload[:value]
35
+ size_kb = value.to_s.bytesize / 1024.0 if value
36
+
37
+ msg = "#{@config.log_prefix} #{event} key=#{key}"
38
+
39
+ case event
40
+ when "cache_fetch_hit.active_support"
41
+ msg << " (hit)"
42
+ when "cache_generate.active_support"
43
+ msg << " (miss -> generating)"
44
+ when "cache_write.active_support"
45
+ msg << " (write)"
46
+ if size_kb && size_kb > @config.warn_large_size_kb
47
+ msg << " ⚠️ large write: #{size_kb.round(1)}KB"
48
+ end
49
+ end
50
+
51
+ if @config.log_payloads && value
52
+ msg << " payload=#{truncate(value.inspect)}"
53
+ end
54
+
55
+ logger.info(msg)
56
+ end
57
+
58
+ def self.truncate(str, max = 150)
59
+ str.length > max ? "#{str[0..max]}..." : str
60
+ end
61
+
62
+ def self.logger
63
+ if defined?(Rails) && Rails.respond_to?(:logger)
64
+ Rails.logger
65
+ else
66
+ @logger ||= Logger.new($stdout)
67
+ end
68
+ end
69
+
70
+ def self.rails_env
71
+ if defined?(Rails)
72
+ Rails.env.to_sym
73
+ else
74
+ (ENV["RACK_ENV"] || "development").to_sym
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CacheInspector
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+ require "cache_inspector/version"
3
+ require "cache_inspector/config"
4
+ require "cache_inspector/subscriber"
5
+
6
+ module CacheInspector
7
+ class << self
8
+ def config
9
+ @config ||= Config.new
10
+ end
11
+
12
+ def configure
13
+ yield config
14
+ self
15
+ end
16
+
17
+ def install!
18
+ Subscriber.install!(config)
19
+ self
20
+ end
21
+ end
22
+ end
23
+
24
+ # Auto-install in Rails
25
+ if defined?(Rails::Railtie)
26
+ class CacheInspector::Railtie < Rails::Railtie
27
+ initializer "cache_inspector.install" do
28
+ CacheInspector.install!
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ module CacheInspector
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cache_inspector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chitradevi36
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '13.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '13.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.2'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '8.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '5.2'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '8.0'
47
+ description: CacheInspector hooks into Rails cache events to log hits, misses, and
48
+ large writes for debugging and performance tuning.
49
+ email:
50
+ - chitra.rajaguru123@gmail.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - ".rspec"
56
+ - CHANGELOG.md
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - config/initializers/cache_inspector.rb
61
+ - lib/cache_inspector.rb
62
+ - lib/cache_inspector/config.rb
63
+ - lib/cache_inspector/subscriber.rb
64
+ - lib/cache_inspector/version.rb
65
+ - sig/cache_inspector.rbs
66
+ homepage: https://github.com/Chitradevi36/cache_inspector
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ allowed_push_host: https://rubygems.org
71
+ homepage_uri: https://github.com/Chitradevi36/cache_inspector
72
+ source_code_uri: https://github.com/Chitradevi36/cache_inspector
73
+ changelog_uri: https://github.com/Chitradevi36/cache_inspector/blob/main/CHANGELOG.md
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.0
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.2.3
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Inspect and monitor cache hits, misses, and writes in Rails.
93
+ test_files: []