Memcached incr and decr Commands
The Memcached incr and decr commands are used to increment or decrement the numeric value of an existing key.
The data operated on by the incr and decr commands must be a 32-bit unsigned decimal integer.
If the key does not exist, it returns NOTFOUND. If the value of the key is not a number, it returns CLIENTERROR. Other errors return ERROR.
incr Command
Syntax:
The basic syntax format for the incr command is as follows:
incr key increment_value
Parameter descriptions are as follows:
- key: The key in the key-value structure used to find the cached value.
- increment_value: The value to be added.
Example
In the following example, we use "visitors" as the key with an initial value of 10, and then perform an addition of 5.
set visitors 0 900 2
10
STORED
get visitors
VALUE visitors 0 2
10
END
incr visitors 5
15
get visitors
VALUE visitors 0 2
15
END
Output
Output information descriptions:
- NOT_FOUND: The key does not exist.
- CLIENT_ERROR: The increment value is not an object.
- ERROR: Other errors, such as syntax errors, etc.
decr Command
The basic syntax format for the decr command is as follows:
decr key decrement_value
Parameter descriptions are as follows:
- key: The key in the key-value structure used to find the cached value.
- decrement_value: The value to be subtracted.
Example
set visitors 0 900 2
10
STORED
get visitors
VALUE visitors 0 2
10
END
decr visitors 5
5
get visitors
VALUE visitors 0 1
5
END
In the following example, we use "visitors" as the key with an initial value of 10, and then perform a subtraction of 5.
Output
Output information descriptions:
- NOT_FOUND: The key does not exist.
- CLIENT_ERROR: The increment value is not an object.
- ERROR: Other errors, such as syntax errors, etc.