From 3a06cc96336409a43b50706a5980498287d74f29 Mon Sep 17 00:00:00 2001 From: Jay Pignata Date: Sat, 26 Sep 2009 22:08:55 -0400 Subject: [PATCH] Adding NullStore to ActiveSupport::Cache --- activesupport/lib/active_support/cache.rb | 1 + .../lib/active_support/cache/null_store.rb | 43 ++++++++++++++++++++ activesupport/test/caching_test.rb | 37 +++++++++++++++++ 3 files changed, 81 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 25f9555..82eab76 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -17,6 +17,7 @@ module ActiveSupport autoload :SynchronizedMemoryStore, 'active_support/cache/synchronized_memory_store' autoload :MemCacheStore, 'active_support/cache/mem_cache_store' autoload :CompressedMemCacheStore, 'active_support/cache/compressed_mem_cache_store' + autoload :NullStore, 'active_support/cache/null_store' module Strategy autoload :LocalCache, 'active_support/cache/strategy/local_cache' 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..c068cd6 --- /dev/null +++ b/activesupport/lib/active_support/cache/null_store.rb @@ -0,0 +1,43 @@ +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(name, options = nil) + super do + nil + end + end + + def write(name, value, options = nil) + super do + value + end + end + + def delete(name, options = nil) + super do + nil + end + end + + def exist?(name, options = nil) + super do + false + end + end + + def increment(name, options = nil) + super do + nil + end + end + + def decrement(name, options = nil) + super do + nil + end + end + end + end +end \ No newline at end of file diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 7667f11..57a0f2b 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -342,3 +342,40 @@ uses_memcached 'memcached backed store' do include CacheStoreBehavior 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 + -- 1.6.4.2