1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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

This commit is contained in:
Jason Lee 2018-06-30 12:52:29 +08:00
parent a969294160
commit 5394be374d
2 changed files with 22 additions and 6 deletions

View file

@ -264,7 +264,7 @@ module ActiveSupport
failsafe :increment do failsafe :increment do
redis.with do |c| redis.with do |c|
val = c.incrby key, amount 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 c.expire key, expires_in
end end
val val
@ -290,7 +290,7 @@ module ActiveSupport
failsafe :decrement do failsafe :decrement do
redis.with do |c| redis.with do |c|
val = c.decrby key, amount 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 c.expire key, expires_in
end end
val val

View file

@ -145,24 +145,40 @@ module ActiveSupport::Cache::RedisCacheStoreTests
def test_increment_expires_in def test_increment_expires_in
assert_called_with @cache.redis, :incrby, [ "#{@namespace}:foo", 1 ] do assert_called_with @cache.redis, :incrby, [ "#{@namespace}:foo", 1 ] do
assert_called_with @cache.redis, :expire, [ "#{@namespace}:foo", 60 ] 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
end end
# key and ttl exist
@cache.redis.setex "#{@namespace}:bar", 120, 1
assert_not_called @cache.redis, :expire do 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
end end
def test_decrement_expires_in def test_decrement_expires_in
assert_called_with @cache.redis, :decrby, [ "#{@namespace}:foo", 1 ] do assert_called_with @cache.redis, :decrby, [ "#{@namespace}:foo", 1 ] do
assert_called_with @cache.redis, :expire, [ "#{@namespace}:foo", 60 ] 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
end end
# key and ttl exist
@cache.redis.setex "#{@namespace}:bar", 120, 1
assert_not_called @cache.redis, :expire do 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 end
end end