Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Memcached incr and decr Commands

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:

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:


decr command

The basic syntax format of the decr command is as follows:



decr key decrement_value

The parameters are described as follows:

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: