incrbyex

Increment a value only if it exists

Returns New value of the counter if found, otherwise nil
Keys
  1. Key used as a counter, which may or may not exist
Args
  1. Amount to increment the value by

Example

> evalsha f74dd5c086898b8a3d69655334b94fa7e006a9b1 1 activeusers 1
(nil)
> set activeusers 0
OK
> evalsha f74dd5c086898b8a3d69655334b94fa7e006a9b1 1 activeusers 1
(integer) 1
> evalsha f74dd5c086898b8a3d69655334b94fa7e006a9b1 1 activeusers 2
(integer) 3

Source

if redis.call("EXISTS",KEYS[1]) == 1 then
  return redis.call("INCRBY",KEYS[1],ARGV[1])
else
  return nil
end

SHA

f74dd5c086898b8a3d69655334b94fa7e006a9b1

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.