提交 5394be37 编写于 作者: J Jason Lee

Fix Cache :redis_store increment/decrement ttl check and add more tests.

上级 a9692941
......@@ -264,7 +264,7 @@ def increment(name, amount = 1, options = nil)
failsafe :increment do
redis.with do |c|
val = c.incrby key, amount
if expires_in > 0 && c.ttl(key) == -2
if expires_in > 0 && c.ttl(key) < 0
c.expire key, expires_in
end
val
......@@ -290,7 +290,7 @@ def decrement(name, amount = 1, options = nil)
failsafe :decrement do
redis.with do |c|
val = c.decrby key, amount
if expires_in > 0 && c.ttl(key) == -2
if expires_in > 0 && c.ttl(key) < 0
c.expire key, expires_in
end
val
......
......@@ -145,24 +145,40 @@ def test_fetch_multi_uses_redis_mget
def test_increment_expires_in
assert_called_with @cache.redis, :incrby, [ "#{@namespace}:foo", 1 ] do
assert_called_with @cache.redis, :expire, [ "#{@namespace}:foo", 60 ] do
@cache.increment("foo", 1, expires_in: 60)
@cache.increment "foo", 1, expires_in: 60
end
end
# key and ttl exist
@cache.redis.setex "#{@namespace}:bar", 120, 1
assert_not_called @cache.redis, :expire do
@cache.decrement("foo", 1, expires_in: 60)
@cache.increment "bar", 1, expires_in: 120
end
# key exist but not have expire
@cache.redis.set "#{@namespace}:dar", 10
assert_called_with @cache.redis, :expire, [ "#{@namespace}:dar", 60 ] do
@cache.increment "dar", 1, expires_in: 60
end
end
def test_decrement_expires_in
assert_called_with @cache.redis, :decrby, [ "#{@namespace}:foo", 1 ] do
assert_called_with @cache.redis, :expire, [ "#{@namespace}:foo", 60 ] do
@cache.decrement("foo", 1, expires_in: 60)
@cache.decrement "foo", 1, expires_in: 60
end
end
# key and ttl exist
@cache.redis.setex "#{@namespace}:bar", 120, 1
assert_not_called @cache.redis, :expire do
@cache.decrement("foo", 1, expires_in: 60)
@cache.decrement "bar", 1, expires_in: 120
end
# key exist but not have expire
@cache.redis.set "#{@namespace}:dar", 10
assert_called_with @cache.redis, :expire, [ "#{@namespace}:dar", 60 ] do
@cache.decrement "dar", 1, expires_in: 60
end
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册