diff options
Diffstat (limited to 'activesupport/lib/active_support/cache')
-rw-r--r-- | activesupport/lib/active_support/cache/redis_cache_store.rb | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/activesupport/lib/active_support/cache/redis_cache_store.rb b/activesupport/lib/active_support/cache/redis_cache_store.rb index a05718b663..5737450b4a 100644 --- a/activesupport/lib/active_support/cache/redis_cache_store.rb +++ b/activesupport/lib/active_support/cache/redis_cache_store.rb @@ -256,18 +256,15 @@ 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 + options = merged_options(options) + key = normalize_key(name, options) + redis.with do |c| - val = c.incrby key, amount - if expires_in > 0 && c.ttl(key) < 0 - c.expire key, expires_in + c.incrby(key, amount).tap do + write_key_expiry(c, key, options) end - val end end end @@ -282,18 +279,15 @@ 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 + options = merged_options(options) + key = normalize_key(name, options) + redis.with do |c| - val = c.decrby key, amount - if expires_in > 0 && c.ttl(key) < 0 - c.expire key, expires_in + c.decrby(key, amount).tap do + write_key_expiry(c, key, options) end - val end end end @@ -405,6 +399,12 @@ module ActiveSupport end end + def write_key_expiry(client, key, options) + if options[:expires_in] && client.ttl(key).negative? + client.expire key, options[:expires_in].to_i + end + end + # Delete an entry from the cache. def delete_entry(key, options) failsafe :delete_entry, returning: false do |