From 8f59d7a8d8e736d7f4b6730020c197d008fb0779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 9 Oct 2009 08:22:42 -0300 Subject: Instrument cache store events only if required. --- activesupport/lib/active_support/cache.rb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index dfd53462af..a2717499e7 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -105,7 +105,7 @@ module ActiveSupport # cache.write("city", "Duckburgh") # cache.read("city") # => "Duckburgh" class Store - cattr_accessor :logger + cattr_accessor :logger, :instance_writter => false attr_reader :silence alias :silence? :silence @@ -122,6 +122,15 @@ module ActiveSupport @silence = previous_silence end + # Set to true if cache stores should be instrumented. By default is false. + def self.instrument=(boolean) + Thread.current[:instrument_cache_store] = boolean + end + + def self.instrument + Thread.current[:instrument_cache_store] || false + end + # Fetches data from the cache, using the given key. If there is data in # the cache with the given key, then that data is returned. # @@ -242,12 +251,15 @@ module ActiveSupport end def instrument(operation, key, options, &block) - payload = { :key => key } - payload.merge!(options) if options.is_a?(Hash) + log(operation, key, options) - # Cache events should be logged or not? - # log(operation, key, options) - ActiveSupport::Orchestra.instrument(:"cache_#{operation}", payload, &block) + if self.class.instrument + payload = { :key => key } + payload.merge!(options) if options.is_a?(Hash) + ActiveSupport::Orchestra.instrument(:"cache_#{operation}", payload, &block) + else + yield + end end def log(operation, key, options) -- cgit v1.2.3