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 decimal 32-bit unsigned integer.
If the key does not exist, it returns NOT_FOUND. If the key's value is not numeric, it returns CLIENT_ERROR. Other errors return ERROR.
incr Command
Syntax:
The basic syntax format for 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, and then perform an increment by 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
The output information is explained as follows:
- 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
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
In the following example, we use visitors as the key, with an initial value of 10, and then perform a decrement by 5 operation.
set visitors 0 900 2
10
STORED
get visitors
VALUE visitors 0 2
10
END
decr visitors 5
5
get visitors
VALUE visitors 0 2
5
END
Output
The output information is explained as follows:
- NOT_FOUND: The key does not exist.
- CLIENT_ERROR: The decrement value is not an object.
- ERROR: Other errors, such as syntax errors, etc.
YouTip