From 48a579de0e4698f52f8c5816f1948ddf96d1a8c0 Mon Sep 17 00:00:00 2001 From: Jay Pignata Date: Sat, 15 May 2010 22:17:35 +0100 Subject: [PATCH] Adding NullStore to ActiveSupport::Cache --- activesupport/lib/active_support/cache.rb | 1 + .../lib/active_support/cache/null_store.rb | 35 +++++++++++++++++++ activesupport/test/caching_test.rb | 36 ++++++++++++++++++++ 3 files changed, 72 insertions(+), 0 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 2605a3f..d81f2bd 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -18,6 +18,7 @@ module ActiveSupport autoload :MemCacheStore, 'active_support/cache/mem_cache_store' autoload :SynchronizedMemoryStore, 'active_support/cache/synchronized_memory_store' autoload :CompressedMemCacheStore, 'active_support/cache/compressed_mem_cache_store' + autoload :NullStore, 'active_support/cache/null_store' EMPTY_OPTIONS = {}.freeze 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..79e085a --- /dev/null +++ b/activesupport/lib/active_support/cache/null_store.rb @@ -0,0 +1,35 @@ +module ActiveSupport + module Cache + # A dummy cache store implementation for testing and debugging purposes. When configured, calls to + # Rails.cache will be logged but reading and writing from the cache will be disabled. + class NullStore < Store + def read_entry(key, options) + nil + end + + def write_entry(key, entry, options) + entry + end + + def delete(name, options = nil) + super do + nil + end + end + + def exist?(name, options = nil) + super do + false + end + end + + def increment(name, amount = 1, options = nil) + nil + end + + def decrement(name, amount = 1, options=nil) + nil + end + end + end +end diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index d9ff120..6061385 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -596,6 +596,42 @@ uses_memcached 'memcached backed store' do end end +class NullStoreTest < ActiveSupport::TestCase + def setup + @cache = ActiveSupport::Cache.lookup_store(:null_store) + end + + def test_write + @cache.write('foo', 'bar') + @cache.write('baz', {:a => 'b'}) + @cache.write('foz', 2) + + assert_nil @cache.fetch('foo') + assert_nil @cache.fetch('baz') + assert_nil @cache.fetch('foz') + end + + def test_fetch + assert_equal 'baz', @cache.fetch('foo') { 'baz' } + assert_equal 'bar', @cache.fetch('foo') { 'bar' } + end + + def test_increment + @cache.write('foo', 1, :raw => true) + assert_nil @cache.increment('foo') + end + + def test_decrement + @cache.write('foo', 3, :raw => true) + assert_nil @cache.decrement('foo') + end + + def test_exist + @cache.write('foo', 'bar') + assert !@cache.exist?('foo') + end +end + class CacheStoreLoggerTest < ActiveSupport::TestCase def setup @cache = ActiveSupport::Cache.lookup_store(:memory_store) -- 1.7.1