Permalink
Browse files

Fixes bug where Memcached connections get corrupted when an invalid e…

…xpire is passed in [#2854 state:resolved]
  • Loading branch information...
Yehuda Katz + Carl Lerche
Yehuda Katz + Carl Lerche committed Jul 1, 2009
1 parent 5217c16 commit 4d8fd8d33568d8db9fc7a2c0bc2e11ca67c77987
@@ -91,11 +91,16 @@ def self.expand_cache_key(key, namespace = nil)
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.
#
@@ -220,8 +225,16 @@ def decrement(key, amount = 1)
end
private
+ def expires_in(options)
+ 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
@@ -130,10 +130,6 @@ def stats
end
private
- def expires_in(options)
- (options && options[:expires_in]) || 0
- end
-
def raw?(options)
options && options[:raw]
end
@@ -1,3 +1,4 @@
+require 'logger'
require 'abstract_unit'
class CacheKeyTest < ActiveSupport::TestCase
@@ -173,6 +174,8 @@ def setup
@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
@@ -295,6 +298,22 @@ def test_middleware
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

0 comments on commit 4d8fd8d

Please sign in to comment.