Memcached incr and decr Commands
The Memcached incr and decr commands are used to increment or decrement the numeric value of an already existing key.
The data operated on by the incr and decr commands must be a decimal 32-bit unsigned integer.
If the key does not exist, it returns NOT_FOUND, and if the value of the key is not a number, it returns CLIENT_ERROR, and other errors return ERROR.
incr command
Syntax:
The basic syntax format of the incr command is as follows:
incr key increment_value
The parameters are described as follows:
- **key:** the key in the key-value structure, used to look up the cached value.
- increment_value: the value to increment by.
Example
In the following example, we use visitors as the key with an initial value of 10, then perform an add 5 operation.
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
Description of the output information:
- NOT_FOUND: the key does not exist.
- CLIENT_ERROR: the increment value is not an object.
- ERROROther errors, such as syntax errors, etc.
decr command
The basic syntax format of the decr command is as follows:
decr key decrement_value
The parameters are described as follows:
- **key:** the key in the key-value structure, used to look up the cached value.
- decrement_value: the value to decrement by.
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, then perform a subtract 5 operation.
Output
Description of the output information:
- NOT_FOUND: the key does not exist.
- CLIENT_ERROR: the increment value is not an object.
- ERROROther errors, such as syntax errors, etc.