diff options
author | Santiago Pastorino <santiago@wyeworks.com> | 2015-11-08 21:35:18 -0300 |
---|---|---|
committer | Santiago Pastorino <santiago@wyeworks.com> | 2015-11-08 21:35:18 -0300 |
commit | 2c88ee604bac6499626e0f775d2bec539fb99d72 (patch) | |
tree | 722867a722e1fbed94c7b4fdf0fcf14e2e9bac1d /activesupport | |
parent | 280654ef884f4e9310db89ccff99b7775013ed85 (diff) | |
parent | 9b7244d07dfecfb5ba0b2e7af414df8c8be83bc5 (diff) | |
download | rails-2c88ee604bac6499626e0f775d2bec539fb99d72.tar.gz rails-2c88ee604bac6499626e0f775d2bec539fb99d72.tar.bz2 rails-2c88ee604bac6499626e0f775d2bec539fb99d72.zip |
Merge pull request #22206 from grosser/grosser/dry
dry up increment/decrement
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/cache/memory_store.rb | 35 |
1 files changed, 15 insertions, 20 deletions
diff --git a/activesupport/lib/active_support/cache/memory_store.rb b/activesupport/lib/active_support/cache/memory_store.rb index 90bb2c38c3..896c28ad8b 100644 --- a/activesupport/lib/active_support/cache/memory_store.rb +++ b/activesupport/lib/active_support/cache/memory_store.rb @@ -75,30 +75,12 @@ module ActiveSupport # Increment an integer value in the cache. def increment(name, amount = 1, options = nil) - synchronize do - options = merged_options(options) - if num = read(name, options) - num = num.to_i + amount - write(name, num, options) - num - else - nil - end - end + modify_value(name, amount, options) end # Decrement an integer value in the cache. def decrement(name, amount = 1, options = nil) - synchronize do - options = merged_options(options) - if num = read(name, options) - num = num.to_i - amount - write(name, num, options) - num - else - nil - end - end + modify_value(name, -amount, options) end def delete_matched(matcher, options = nil) @@ -167,6 +149,19 @@ module ActiveSupport !!entry end end + + private + + def modify_value(name, amount, options) + synchronize do + options = merged_options(options) + if num = read(name, options) + num = num.to_i + amount + write(name, num, options) + num + end + end + end end end end |