hincrbyex

Increment a value in a hash only if it exists

Returns New counter value if found, otherwise nil
Keys
  1. A Hash containing a field to be used as a counter
Args
  1. The field within the Hash to use as a counter
  2. The amount to increment the counter by

Example

> evalsha 3cb0d65c399fda8a1d4eb93b2fe85311671bc969 1 guests sally 1
(nil)
> hset guests sally 8
(integer) 1
> evalsha 3cb0d65c399fda8a1d4eb93b2fe85311671bc969 1 guests sally 1
(integer) 9
> evalsha 3cb0d65c399fda8a1d4eb93b2fe85311671bc969 1 guests sally 1
(integer) 10

Source

if redis.call("HEXISTS", KEYS[1], ARGV[1]) == 1 then
  return redis.call("HINCRBY", KEYS[1], ARGV[1], ARGV[2])
else
  return nil
end

SHA

3cb0d65c399fda8a1d4eb93b2fe85311671bc969

Last updated 31 Jul 2014.

← More scripts from the Memetria Library

License

All scripts on this site are in the public domain, unless otherwise noted, and are provided without warranty express or implied. Memetria make no claim for the effectiveness, safety, security or quality of the scripts contained within.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.