w3resource

Redis String: INCR

Redis INCR Command

Redis INCR command is used to increment the integer value of a key by one. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as an integer. This operation is limited to 64-bit signed integers.

Here are some common uses and scenarios for the "INCR" command:

  • Counters: Implement counters for various applications (e.g., page views, likes).
  • Rate Limiting: Track and enforce rate limits by incrementing counters.
  • Atomic Operations: Ensure atomicity when incrementing counters in a multi-client environment.
  • Session Management: Use for generating unique session IDs or sequence numbers.
  • Batch Processing: Increment multiple counters using pipelining for performance.

Syntax:

INCR KEY_NAME 

Available since

1.0.0.

Return Value

Integer reply, the value of key after the increment

Return Value Type

Integer

Example: Redis INCR

redis 127.0.0.1:6379> SET visitors 1000
OK
redis 127.0.0.1:6379> INCR visitors
(integer) 1001
redis 127.0.0.1:6379> GET visitors
(integer) 1001

Previous: PSETEX
Next: INCRBY



Follow us on Facebook and Twitter for latest update.