From 5576f6a1042838f5b39a19a3ee94f7ec0f9b206e 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 ++++++++++++++++++++ railties/guides/source/caching_with_rails.textile | 8 +++ 4 files changed, 96 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) diff --git a/railties/guides/source/caching_with_rails.textile b/railties/guides/source/caching_with_rails.textile index 1b5ec40..3e819de 100644 --- a/railties/guides/source/caching_with_rails.textile +++ b/railties/guides/source/caching_with_rails.textile @@ -304,6 +304,14 @@ The +write+ and +fetch+ methods on this cache accept two additional options that ActionController::Base.cache_store = :mem_cache_store, "cache-1.example.com", "cache-2.example.com" +h4. ActiveSupport::Cache::NullStore + +This cache store implementation is meant to be used only in development or test environments and it never stores anything. This can be very useful in development when you have code that interacts directly with +Rails.cache+, but caching may interfere with being able to see the results of code changes. With this cache store, all +fetch+ and +read+ operations will result in a miss. + + +ActionController::Base.cache_store = nil + + h4. Custom Cache Stores You can create your own custom cache store by simply extending +ActiveSupport::Cache::Store+ and implementing the appropriate methods. In this way, you can swap in any number of caching technologies into your Rails application. -- 1.7.3.4