Counters

Redis can be used to store counters that can be incremented and decremented as described with the following commands:

INCR

INCR key

Increments the number stored at 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 integer. This operation is limited to 64-bit signed integers.

INCR Example

Increment a counter

redis> incr visitorCount
(integer) 1

Read more about the INCR command in the Redis documentation.

DECR

DECR key

Decrements the number stored at 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 integer. This operation is limited to 64-bit signed integers.

DECR Example

Decrement a counter

redis> decr visitorCount
(integer) 0

Read more about the DECR command in the Redis documentation.

Additional Notes

These commands can also be used for more advanced applications beyond counters such as rate limiters are described in the Redis documentation.

results matching ""

    No results matching ""