diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2018-06-29 10:59:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-29 10:59:37 +0200 |
commit | 4883f73978355a572856a6c26b7a6dcbc828f2fd (patch) | |
tree | b6ee2cad58a0fda333dd8d63f9971e9b3ed37ae3 /activesupport/lib/active_support/cache | |
parent | 11505cc2b3b24f7b1f0aa1585fbe4830885593ca (diff) | |
parent | 9d5b02ec5062a23665ec596ef7d3efe4f5abcc27 (diff) | |
download | rails-4883f73978355a572856a6c26b7a6dcbc828f2fd.tar.gz rails-4883f73978355a572856a6c26b7a6dcbc828f2fd.tar.bz2 rails-4883f73978355a572856a6c26b7a6dcbc828f2fd.zip |
Merge pull request #33254 from huacnlee/add-expires-in-option-support-for-redis-cache-store-increment-method
Add :expires_in option support for RedisCacheStore increment/decrement method
Diffstat (limited to 'activesupport/lib/active_support/cache')
-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 |