aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/lib/active_support/cache.rb13
-rw-r--r--activesupport/test/caching_test.rb19
2 files changed, 30 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
index 3c422e0252..448db538ab 100644
--- a/activesupport/lib/active_support/cache.rb
+++ b/activesupport/lib/active_support/cache.rb
@@ -99,11 +99,16 @@ module ActiveSupport
class Store
cattr_accessor :logger
+ attr_reader :silence, :logger_off
+
def silence!
@silence = true
self
end
+ alias silence? silence
+ alias logger_off? logger_off
+
# Fetches data from the cache, using the given key. If there is data in
# the cache with the given key, then that data is returned.
#
@@ -233,11 +238,15 @@ module ActiveSupport
private
def expires_in(options)
- (options && options[:expires_in]) || 0
+ expires_in = options && options[:expires_in]
+
+ raise ":expires_in must be a number" if expires_in && !expires_in.is_a?(Numeric)
+
+ expires_in || 0
end
def log(operation, key, options)
- logger.debug("Cache #{operation}: #{key}#{options ? " (#{options.inspect})" : ""}") if logger && !@silence && !@logger_off
+ logger.debug("Cache #{operation}: #{key}#{options ? " (#{options.inspect})" : ""}") if logger && !silence? && !logger_off?
end
end
end
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb
index 928af256f4..7667f11343 100644
--- a/activesupport/test/caching_test.rb
+++ b/activesupport/test/caching_test.rb
@@ -1,3 +1,4 @@
+require 'logger'
require 'abstract_unit'
require 'active_support/cache'
@@ -190,6 +191,8 @@ uses_memcached 'memcached backed store' do
@cache = ActiveSupport::Cache.lookup_store(:mem_cache_store)
@data = @cache.instance_variable_get(:@data)
@cache.clear
+ @cache.silence!
+ @cache.logger = Logger.new("/dev/null")
end
include CacheStoreBehavior
@@ -312,6 +315,22 @@ uses_memcached 'memcached backed store' do
app = @cache.middleware.new(app)
app.call({})
end
+
+ def test_expires_in
+ result = @cache.write('foo', 'bar', :expires_in => 1)
+ assert_equal 'bar', @cache.read('foo')
+ sleep 2
+ assert_equal nil, @cache.read('foo')
+ end
+
+ def test_expires_in_with_invalid_value
+ @cache.write('baz', 'bat')
+ assert_raise(RuntimeError) do
+ @cache.write('foo', 'bar', :expires_in => 'Mon Jun 29 13:10:40 -0700 2150')
+ end
+ assert_equal 'bat', @cache.read('baz')
+ assert_equal nil, @cache.read('foo')
+ end
end
class CompressedMemCacheStore < ActiveSupport::TestCase