Permalink
Browse files

Mute log info coming from the local_cache strategy

  • Loading branch information...
1 parent 48b3060 commit c47c5af1c89a85dede432d1539ddcab5dac4e10a @lifo lifo committed Oct 9, 2009
@@ -101,6 +101,13 @@ def silence!
alias silence? silence
alias logger_off? logger_off
+ def mute
+ previous_silence, @silence = defined?(@silence) && @silence, true
+ yield
+ ensure
+ @silence = previous_silence
+ 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.
#
@@ -37,7 +37,7 @@ def read(key, options = nil)
nil
elsif value.nil?
value = super
- local_cache.write(key, value || NULL) if local_cache
+ local_cache.mute { local_cache.write(key, value || NULL) } if local_cache
value.duplicable? ? value.dup : value
else
# forcing the value to be immutable
@@ -47,12 +47,12 @@ def read(key, options = nil)
def write(key, value, options = nil)
value = value.to_s if respond_to?(:raw?) && raw?(options)
- local_cache.write(key, value || NULL) if local_cache
+ local_cache.mute { local_cache.write(key, value || NULL) } if local_cache
super
end
def delete(key, options = nil)
- local_cache.write(key, NULL) if local_cache
+ local_cache.mute { local_cache.write(key, NULL) } if local_cache
super
end
@@ -69,7 +69,7 @@ def exist(key, options = nil)
def increment(key, amount = 1)
if value = super
- local_cache.write(key, value.to_s) if local_cache
+ local_cache.mute { local_cache.write(key, value.to_s) } if local_cache
value
else
nil
@@ -78,7 +78,7 @@ def increment(key, amount = 1)
def decrement(key, amount = 1)
if value = super
- local_cache.write(key, value.to_s) if local_cache
+ local_cache.mute { local_cache.write(key, value.to_s) } if local_cache
value
else
nil
@@ -325,3 +325,22 @@ def setup
include CacheStoreBehavior
end
end
+
+class CacheStoreLoggerTest < ActiveSupport::TestCase
+ def setup
+ @cache = ActiveSupport::Cache.lookup_store(:memory_store)
+
+ @buffer = StringIO.new
+ @cache.logger = Logger.new(@buffer)
+ end
+
+ def test_logging
+ @cache.fetch('foo') { 'bar' }
+ assert @buffer.string.present?
+ end
+
+ def test_mute_logging
+ @cache.mute { @cache.fetch('foo') { 'bar' } }
+ assert @buffer.string.blank?
+ end
+end

0 comments on commit c47c5af

Please sign in to comment.