diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2018-07-01 12:42:18 +0200 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2018-07-01 12:46:15 +0200 |
commit | 969577d960cd7e2d0a421af4a987f56894c3b229 (patch) | |
tree | ea6aa0f608aa255e0e49a6d0970baedf6dca92e5 /activesupport/lib/active_support/cache | |
parent | f666fb58a3d7b56461d9aac04411022aaa86c0c7 (diff) | |
download | rails-969577d960cd7e2d0a421af4a987f56894c3b229.tar.gz rails-969577d960cd7e2d0a421af4a987f56894c3b229.tar.bz2 rails-969577d960cd7e2d0a421af4a987f56894c3b229.zip |
Refactor #33254.
Firstly, increment and decrement shouldn't care about the particulars of
key expiry. They should only know that they have to pass that responsibility
on to somewhere else.
Secondly, it moves the key normalization back inside the instrumentation like
it was originally. I think that matches the original design intention or at
the very least it lets users catch haywire key truncation.
Thirdly, it moves the changelog entry to the top of the file, where new entries
go. I couldn't understand what the entry was saying so I tried to rewrite it.
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 |