diff options
author | Jason Lee <huacnlee@gmail.com> | 2018-06-29 15:52:12 +0800 |
---|---|---|
committer | Jason Lee <huacnlee@gmail.com> | 2018-06-29 15:52:12 +0800 |
commit | 9d5b02ec5062a23665ec596ef7d3efe4f5abcc27 (patch) | |
tree | 857dc1c83fbec3ba065ab96763a73475159003ea /activesupport/lib | |
parent | 79ef9fbe4639954e7f71463a1184489ffec1e5e6 (diff) | |
download | rails-9d5b02ec5062a23665ec596ef7d3efe4f5abcc27.tar.gz rails-9d5b02ec5062a23665ec596ef7d3efe4f5abcc27.tar.bz2 rails-9d5b02ec5062a23665ec596ef7d3efe4f5abcc27.zip |
Add :expires_in option support for RedisCacheStore increment/decrement method.
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/cache/redis_cache_store.rb | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/cache/redis_cache_store.rb b/activesupport/lib/active_support/cache/redis_cache_store.rb index 11c574258f..95f8f639e8 100644 --- a/activesupport/lib/active_support/cache/redis_cache_store.rb +++ b/activesupport/lib/active_support/cache/redis_cache_store.rb @@ -256,9 +256,19 @@ module ActiveSupport # # 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 @@ module ActiveSupport # # 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 |