From 06a82fb767d21e60c7a0ed98552a12ad1be37ed9 Mon Sep 17 00:00:00 2001 From: Jay Pignata Date: Sat, 15 Aug 2009 19:06:06 -0400 Subject: [PATCH] Allowing ActiveSupport cache to be disabled --- activesupport/lib/active_support/cache.rb | 13 +++++++++ .../cache/compressed_mem_cache_store.rb | 12 +++++--- .../lib/active_support/cache/file_store.rb | 14 +++++++--- .../lib/active_support/cache/mem_cache_store.rb | 16 ++++++++--- .../lib/active_support/cache/memory_store.rb | 14 +++++++--- activesupport/test/caching_test.rb | 28 ++++++++++++++++++++ 6 files changed, 80 insertions(+), 17 deletions(-) diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 3f31185..ab7e777 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -1,8 +1,17 @@ require 'benchmark' module ActiveSupport + class << self + delegate :perform_caching=, :to => :'ActiveSupport::Cache' + end + # See ActiveSupport::Cache::Store for documentation. module Cache + class << self + @@perform_caching = true + cattr_accessor :perform_caching + end + autoload :FileStore, 'active_support/cache/file_store' autoload :MemoryStore, 'active_support/cache/memory_store' autoload :SynchronizedMemoryStore, 'active_support/cache/synchronized_memory_store' @@ -72,6 +81,10 @@ module ActiveSupport expanded_cache_key end + + def self.cache_configured? + perform_caching + end # An abstract cache store class. There are multiple cache store # implementations, each having its own additional features. See the classes diff --git a/activesupport/lib/active_support/cache/compressed_mem_cache_store.rb b/activesupport/lib/active_support/cache/compressed_mem_cache_store.rb index d87eb17..f9cc909 100644 --- a/activesupport/lib/active_support/cache/compressed_mem_cache_store.rb +++ b/activesupport/lib/active_support/cache/compressed_mem_cache_store.rb @@ -2,11 +2,13 @@ module ActiveSupport module Cache class CompressedMemCacheStore < MemCacheStore def read(name, options = nil) - if value = super(name, (options || {}).merge(:raw => true)) - if raw?(options) - value - else - Marshal.load(ActiveSupport::Gzip.decompress(value)) + if Cache.cache_configured? + if value = super(name, (options || {}).merge(:raw => true)) + if raw?(options) + value + else + Marshal.load(ActiveSupport::Gzip.decompress(value)) + end end end end diff --git a/activesupport/lib/active_support/cache/file_store.rb b/activesupport/lib/active_support/cache/file_store.rb index 7f34cb5..73444aa 100644 --- a/activesupport/lib/active_support/cache/file_store.rb +++ b/activesupport/lib/active_support/cache/file_store.rb @@ -9,8 +9,10 @@ module ActiveSupport end def read(name, options = nil) - super - File.open(real_file_path(name), 'rb') { |f| Marshal.load(f) } rescue nil + if Cache.cache_configured? + super + File.open(real_file_path(name), 'rb') { |f| Marshal.load(f) } rescue nil + end end def write(name, value, options = nil) @@ -43,8 +45,12 @@ module ActiveSupport end def exist?(name, options = nil) - super - File.exist?(real_file_path(name)) + if Cache.cache_configured? + super + File.exist?(real_file_path(name)) + else + false + end end private diff --git a/activesupport/lib/active_support/cache/mem_cache_store.rb b/activesupport/lib/active_support/cache/mem_cache_store.rb index 954d0f5..a737264 100644 --- a/activesupport/lib/active_support/cache/mem_cache_store.rb +++ b/activesupport/lib/active_support/cache/mem_cache_store.rb @@ -50,12 +50,16 @@ module ActiveSupport # Reads multiple keys from the cache. def read_multi(*keys) - @data.get_multi keys + if Cache.cache_configured? + @data.get_multi keys + end end def read(key, options = nil) # :nodoc: - super - @data.get(key, raw?(options)) + if Cache.cache_configured? + super + @data.get(key, raw?(options)) + end rescue MemCache::MemCacheError => e logger.error("MemCacheError (#{e}): #{e.message}") nil @@ -94,7 +98,11 @@ module ActiveSupport # Doesn't call super, cause exist? in memcache is in fact a read # But who cares? Reading is very fast anyway # Local cache is checked first, if it doesn't know then memcache itself is read from - !read(key, options).nil? + if Cache.cache_configured? + !read(key, options).nil? + else + false + end end def increment(key, amount = 1) # :nodoc: diff --git a/activesupport/lib/active_support/cache/memory_store.rb b/activesupport/lib/active_support/cache/memory_store.rb index 1b30d49..6e65e1c 100644 --- a/activesupport/lib/active_support/cache/memory_store.rb +++ b/activesupport/lib/active_support/cache/memory_store.rb @@ -20,8 +20,10 @@ module ActiveSupport end def read(name, options = nil) - super - @data[name] + if Cache.cache_configured? + super + @data[name] + end end def write(name, value, options = nil) @@ -40,8 +42,12 @@ module ActiveSupport end def exist?(name,options = nil) - super - @data.has_key?(name) + if Cache.cache_configured? + super + @data.has_key?(name) + else + false + end end def clear diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 94c130d..fb3e305 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -142,6 +142,34 @@ module CacheStoreBehavior end end +class CacheStoreDisabledBehavior < ActiveSupport::TestCase + def setup + ActiveSupport::Cache.perform_caching = false + @cache = ActiveSupport::Cache.lookup_store(:memory_store) + end + + def test_exist_should_return_false_when_caching_disabled + @cache.write('foo', 'bar') + assert_equal(@cache.exist?('foo'), false) + end + + def test_read_should_return_nil_when_caching_disabled + @cache.write('bar', 'foo') + assert_nil(@cache.read('bar')) + end + + def test_fetch_should_return_nil_when_caching_disabled + @cache.fetch('foo') { 'bar' } + assert_nil(@cache.read('foo')) + end + + def teardown + ActiveSupport::Cache.perform_caching = true + end +end + + + class FileStoreTest < ActiveSupport::TestCase def setup @cache = ActiveSupport::Cache.lookup_store(:file_store, Dir.pwd) -- 1.5.6.5