Module: CacheInspector::Subscriber
- Defined in:
- lib/cache_inspector/subscriber.rb
Constant Summary collapse
- EVENTS =
%w[ cache_read.active_support cache_generate.active_support cache_write.active_support cache_delete.active_support cache_fetch_hit.active_support ].freeze
Class Method Summary collapse
- .handle_event(event, e) ⇒ Object
- .install!(config) ⇒ Object
- .logger ⇒ Object
- .rails_env ⇒ Object
- .truncate(str, max = 150) ⇒ Object
Class Method Details
.handle_event(event, e) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cache_inspector/subscriber.rb', line 29 def self.handle_event(event, e) env = rails_env return unless @config.enabled?(env) key = e.payload[:key] value = e.payload[:value] size_kb = value.to_s.bytesize / 1024.0 if value msg = "#{@config.log_prefix} #{event} key=#{key}" case event when "cache_fetch_hit.active_support" msg << " (hit)" when "cache_generate.active_support" msg << " (miss -> generating)" when "cache_write.active_support" msg << " (write)" if size_kb && size_kb > @config.warn_large_size_kb msg << " ⚠️ large write: #{size_kb.round(1)}KB" end end if @config.log_payloads && value msg << " payload=#{truncate(value.inspect)}" end logger.info(msg) end |
.install!(config) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/cache_inspector/subscriber.rb', line 14 def self.install!(config) return if @installed @config = config EVENTS.each do |event| ActiveSupport::Notifications.subscribe(event) do |*args| event_obj = ActiveSupport::Notifications::Event.new(*args) handle_event(event, event_obj) end end @installed = true end |
.logger ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/cache_inspector/subscriber.rb', line 62 def self.logger if defined?(Rails) && Rails.respond_to?(:logger) Rails.logger else @logger ||= Logger.new($stdout) end end |
.rails_env ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/cache_inspector/subscriber.rb', line 70 def self.rails_env if defined?(Rails) Rails.env.to_sym else (ENV["RACK_ENV"] || "development").to_sym end end |
.truncate(str, max = 150) ⇒ Object
58 59 60 |
# File 'lib/cache_inspector/subscriber.rb', line 58 def self.truncate(str, max = 150) str.length > max ? "#{str[0..max]}..." : str end |