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 by the incr and decr commands must be decimal 32-bit unsigned integers.
If the key does not exist, NOT_FOUND is returned. If the value of the key is not a number, CLIENT_ERROR is returned. For other errors, ERROR is returned.
incr command
Syntax:
The basic syntax format of the incr command is as follows:
incr key increment_value
The parameter description is as follows:
key: The key in the key-value structure is used to find cached values.
increment_value: Increased value.
Example
In the following example, we use visitors as the key, the initial value is 10, and then add 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 description:
NOT_FOUND: key does not exist.
CLIENT_ERROR: The auto-incremented 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 parameter description is as follows:
key: The key in the key-value structure is used to find cached values.
decrement_value: Decrement value.
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, the initial value is 10, and then subtracted by 5.
Output
Output information description:
NOT_FOUND: key does not exist.
CLIENT_ERROR: The auto-incremented value is not an object.
ERROROther errors, such as syntax errors, etc.