From ae51d5ccd3a37536f67902e475bd9549ca2d8d2c Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Thu, 10 Feb 2011 16:36:13 -0600 Subject: [PATCH] Add ActiveSupport::Cache::NullStore to expose caching interface without actually caching for development and test environments. [#6128 state:resolved] --- activesupport/lib/active_support/cache.rb | 3 +- .../lib/active_support/cache/null_store.rb | 39 ++++++++++++++++ activesupport/test/caching_test.rb | 47 ++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletions(-) create mode 100644 activesupport/lib/active_support/cache/null_store.rb diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index b4f0c42..deac13c 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -16,6 +16,7 @@ module ActiveSupport autoload :FileStore, 'active_support/cache/file_store' autoload :MemoryStore, 'active_support/cache/memory_store' autoload :MemCacheStore, 'active_support/cache/mem_cache_store' + autoload :NullStore, 'active_support/cache/null_store' autoload :SynchronizedMemoryStore, 'active_support/cache/synchronized_memory_store' autoload :CompressedMemCacheStore, 'active_support/cache/compressed_mem_cache_store' @@ -68,7 +69,7 @@ module ActiveSupport end store_class.new(*parameters) when nil - ActiveSupport::Cache::MemoryStore.new + ActiveSupport::Cache::NullStore.new else store end diff --git a/activesupport/lib/active_support/cache/null_store.rb b/activesupport/lib/active_support/cache/null_store.rb new file mode 100644 index 0000000..7c49d21 --- /dev/null +++ b/activesupport/lib/active_support/cache/null_store.rb @@ -0,0 +1,39 @@ +module ActiveSupport + module Cache + # A cache store implementation which doesn't actually store anything. Useful in + # development and test environments where you don't want caching turned on but + # need to go through the caching interface. + class NullStore < Store + def initialize(options = nil) + super(options) + end + + def clear(options = nil) + end + + def cleanup(options = nil) + end + + def increment(name, amount = 1, options = nil) + end + + def decrement(name, amount = 1, options = nil) + end + + def delete_matched(matcher, options = nil) + end + + protected + def read_entry(key, options) # :nodoc: + end + + def write_entry(key, entry, options) # :nodoc: + true + end + + def delete_entry(key, options) # :nodoc: + false + end + end + end +end diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 579d5da..86ab488 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -648,6 +648,53 @@ uses_memcached 'memcached backed store' do end end +class NullStoreTest < ActiveSupport::TestCase + def setup + @cache = ActiveSupport::Cache.lookup_store(:null_store) + end + + def test_clear + @cache.clear + end + + def test_cleanup + @cache.cleanup + end + + def test_write + assert_equal true, @cache.write("name", "value") + end + + def test_read + @cache.write("name", "value") + assert_nil @cache.read("name") + end + + def test_delete + @cache.write("name", "value") + assert_equal false, @cache.delete("name") + end + + def test_increment + @cache.write("name", 1, :raw => true) + assert_nil @cache.increment("name") + end + + def test_decrement + @cache.write("name", 1, :raw => true) + assert_nil @cache.increment("name") + end + + def test_delete_matched + @cache.write("name", "value") + @cache.delete_matched(/name/) + end + + def test_setting_nil_cache_store + assert ActiveSupport::Cache.lookup_store.class, ActiveSupport::Cache::NullStore + end +end + class CacheStoreLoggerTest < ActiveSupport::TestCase def setup @cache = ActiveSupport::Cache.lookup_store(:memory_store) -- 1.7.3.4