From 94cf6675d516a0196f0222694f26dcd4c29c49c6 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 17 Jul 2008 15:29:30 -0500 Subject: Cleanup ActiveSupport::Cache::ThreadSafety module and add test coverage --- activesupport/lib/active_support/cache.rb | 32 +++++++++++-------------------- 1 file changed, 11 insertions(+), 21 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 3e3dc18263..57c6a6331d 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -21,7 +21,7 @@ module ActiveSupport expanded_cache_key = namespace ? "#{namespace}/" : "" if ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"] - expanded_cache_key << "#{ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]}/" + expanded_cache_key << "#{ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]}/" end expanded_cache_key << case @@ -40,13 +40,8 @@ module ActiveSupport class Store cattr_accessor :logger - def initialize - end - def threadsafe! - @mutex = Mutex.new - self.class.send :include, ThreadSafety - self + extend ThreadSafety end # Pass :force => true to force a cache miss. @@ -110,29 +105,24 @@ module ActiveSupport nil end end - + private def log(operation, key, options) logger.debug("Cache #{operation}: #{key}#{options ? " (#{options.inspect})" : ""}") if logger && !@logger_off end end - module ThreadSafety #:nodoc: - def read(key, options = nil) #:nodoc: - @mutex.synchronize { super } - end - - def write(key, value, options = nil) #:nodoc: - @mutex.synchronize { super } - end - - def delete(key, options = nil) #:nodoc: - @mutex.synchronize { super } + def self.extended(object) #:nodoc: + object.instance_variable_set(:@mutex, Mutex.new) end - def delete_matched(matcher, options = nil) #:nodoc: - @mutex.synchronize { super } + %w(read write delete delete_matched exist? increment decrement).each do |method| + module_eval <<-EOS, __FILE__, __LINE__ + def #{method}(*args) + @mutex.synchronize { super } + end + EOS end end end -- cgit v1.2.3 From 0eef4e554db9b1af47305f5a8c51c90c15ac6c04 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 17 Jul 2008 16:00:59 -0500 Subject: Allow ActiveSupport::Cache logger to be silenced --- activesupport/lib/active_support/cache.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 57c6a6331d..5a064f8bea 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -36,7 +36,6 @@ module ActiveSupport expanded_cache_key end - class Store cattr_accessor :logger @@ -44,6 +43,11 @@ module ActiveSupport extend ThreadSafety end + def silence! + @silence = true + self + end + # Pass :force => true to force a cache miss. def fetch(key, options = {}) @logger_off = true @@ -108,7 +112,7 @@ module ActiveSupport private def log(operation, key, options) - logger.debug("Cache #{operation}: #{key}#{options ? " (#{options.inspect})" : ""}") if logger && !@logger_off + logger.debug("Cache #{operation}: #{key}#{options ? " (#{options.inspect})" : ""}") if logger && !@silence && !@logger_off end end -- cgit v1.2.3