Module: Moneta::IncrementSupport Private

Included in:
Adapters::File, Adapters::LRUHash, Adapters::Memory, Adapters::Sqlite
Defined in:
lib/moneta/mixins.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#increment(key, amount = 1, options = {}) ⇒ Object

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically increment integer value with key

This method also accepts negative amounts.

Parameters:

  • key (Object)
  • amount (Integer) (defaults to: 1)
  • options (Hash) (defaults to: {})

Returns:

  • (Object)

    value from store



148
149
150
151
152
153
154
155
# File 'lib/moneta/mixins.rb', line 148

def increment(key, amount = 1, options = {})
  value = load(key, options)
  intvalue = value.to_i
  raise 'Tried to increment non integer value' unless value == nil || intvalue.to_s == value.to_s
  intvalue += amount
  store(key, intvalue.to_s, options)
  intvalue
end