Method: ActiveSupport::Cache::RedisCacheStore#decrement

Defined in:
activesupport/lib/active_support/cache/redis_cache_store.rb

#decrement(name, amount = 1, options = nil) ⇒ Object

Decrement a cached integer value using the Redis decrby atomic operator. Returns the updated value.

If the key is unset or has expired, it will be set to -amount:

cache.decrement("foo") # => -1

To set a specific value, call #write passing raw: true:

cache.write("baz", 5, raw: true)
cache.decrement("baz") # => 4

Decrementing a non-numeric value, or a value written without raw: true, will fail and return nil.

Failsafe: Raises errors.



267
268
269
270
271
272
273
274
275
276
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 267

def decrement(name, amount = 1, options = nil)
  options = merged_options(options)
  key = normalize_key(name, options)

  instrument :decrement, key, amount: amount do
    failsafe :decrement do
      change_counter(key, -amount, options)
    end
  end
end