Add :expires_in option support for RedisCacheStore increment/decrement method.
This commit is contained in:
parent
79ef9fbe46
commit
9d5b02ec50
@ -131,5 +131,16 @@
|
||||
|
||||
*Eileen M. Uchitelle*, *Aaron Patterson*
|
||||
|
||||
* RedisCacheStore: Support expiring counters.
|
||||
Pass `expires_in: [seconds]` to `#increment` and `#decrement` options
|
||||
to set the Redis EXPIRE if the counter doesn't exist.
|
||||
If the counter exists, Redis doesn't extend its expiry when it's exist.
|
||||
|
||||
```
|
||||
Rails.cache.increment("my_counter", 1, expires_in: 2.minutes)
|
||||
```
|
||||
|
||||
*Jason Lee*
|
||||
|
||||
|
||||
Please check [5-2-stable](https://github.com/rails/rails/blob/5-2-stable/activesupport/CHANGELOG.md) for previous changes.
|
||||
|
@ -256,9 +256,19 @@ def delete_matched(matcher, options = nil)
|
||||
#
|
||||
# Failsafe: Raises errors.
|
||||
def increment(name, amount = 1, options = nil)
|
||||
options = merged_options(options)
|
||||
key = normalize_key(name, options)
|
||||
expires_in = options[:expires_in].to_i
|
||||
|
||||
instrument :increment, name, amount: amount do
|
||||
failsafe :increment do
|
||||
redis.with { |c| c.incrby normalize_key(name, options), amount }
|
||||
redis.with do |c|
|
||||
val = c.incrby key, amount
|
||||
if expires_in > 0 && c.ttl(key) == -2
|
||||
c.expire key, expires_in
|
||||
end
|
||||
val
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -272,9 +282,19 @@ def increment(name, amount = 1, options = nil)
|
||||
#
|
||||
# Failsafe: Raises errors.
|
||||
def decrement(name, amount = 1, options = nil)
|
||||
options = merged_options(options)
|
||||
key = normalize_key(name, options)
|
||||
expires_in = options[:expires_in].to_i
|
||||
|
||||
instrument :decrement, name, amount: amount do
|
||||
failsafe :decrement do
|
||||
redis.with { |c| c.decrby normalize_key(name, options), amount }
|
||||
redis.with do |c|
|
||||
val = c.decrby key, amount
|
||||
if expires_in > 0 && c.ttl(key) == -2
|
||||
c.expire key, expires_in
|
||||
end
|
||||
val
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -141,6 +141,30 @@ def test_fetch_multi_uses_redis_mget
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
||||
assert_not_called @cache.redis, :expire do
|
||||
@cache.decrement("foo", 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)
|
||||
end
|
||||
end
|
||||
|
||||
assert_not_called @cache.redis, :expire do
|
||||
@cache.decrement("foo", 1, expires_in: 60)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class ConnectionPoolBehaviourTest < StoreTest
|
||||
|
Loading…
Reference in New Issue
Block a user