Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Memcached incr with decr command


May 17, 2021 Memcached


Table of contents


The Memcached incr and decr commands are used to self-increase or subtract the numeric values of existing keys.

The data operated by the incr and decr commands must be a decal 32-bit unsigned integer.

If key does not NOT_FOUND, if the value of the key is not a number, CLIENT_ERROR returns and other errors return ERROR.


The incr command

Grammar:

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

incr key increment_value

The parameters are described below:

  • Key: Key in the key-value structure to find cached values.
  • increment_value: The increased value.

Instance

In the following example, we use visitors as key, with an initial value of 10, followed by plus 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: Self-value added is not an object.
  • ERROR other errors, such as syntax errors.

Decr command

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

decr key decrement_value

The parameters are described below:

  • Key: Key in the key-value structure to find cached values.
  • decrement_value: The decreased value.

Instance

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 key, with an initial value of 10, followed by minus 5.

Output

Output information description:

  • NOT_FOUND:key does not exist.
  • CLIENT_ERROR: Self-value added is not an object.
  • ERROR other errors, such as syntax errors.