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, returnNOT_FOUND, if the key's value is not a number, returnCLIENT_ERROR, other errors returnERROR。


incr command

Syntax:

The basic syntax of the incr command is as follows:

incr key increment_value

Parameter description is as follows:

  • key:The key in the key-value structure, used to find the cached value.
  • increment_value: The increment value.

Example

In the following example, we use visitors as the key, with an initial value of 10, and then perform an increment 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 description:

  • NOT_FOUND: The key does not exist.
  • CLIENT_ERROR: The auto-increment value is not an object.
  • ERROROther errors, such as syntax errors, etc.

decr command

The basic syntax of the decr command is as follows:

decr key decrement_value

Parameter description is as follows:

  • key:The key in the key-value structure, used to find the cached value.
  • decrement_value: The 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, with an initial value of 10, and then perform a decrement of 5.

Output

Output information description:

  • NOT_FOUND: The key does not exist.
  • CLIENT_ERROR: The auto-increment value is not an object.
  • ERROROther errors, such as syntax errors, etc.
Other extensions