本文内容为 http://redis.io/commands/incr 的翻译、注解、例子扩充及其它修改。感谢 Redis 作者 Antirez 为开源社区作出的贡献,本文保证最新、最准、最全以表示对其的敬意。欢迎留言纠错、提示更新或支持。
INCR key

使键 key 对应的值加 1

Increments the number stored at key by one. 

如果键 key 不存在,则在进行该操作前创建该键并设值为 0

如果键 key 对应的值的类型错误,或虽然是字符串,但该字符串不能用来表示一个整数,将会返回一个错误。

该命令所能操作的数值范围被限制为 64 位有符号整数(64 bit signed integers)

If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.

注意 这是一个字符串操作,因为 Redis 并不存在一个明确的整数类型。该命令执行时,键 key 所对应的字符串值(String value)被解析为 10 进制 64 位有符号整数(base-10 64 bit signed integer)

Note: this is a string operation because Redis does not have a dedicated integer type. The string stored at the key is interpreted as a base-10 64 bit signed integer to execute the operation.

事实上,Redis 内部采用整数形式(Integer representation)来存储对应的整数值,所以对该类字符串值实际上是用整数保存,也就不存在存储整数的字符串表示(String representation)所带来的额外消耗。

Redis stores integers in their integer representation, so for string values that actually hold an integer, there is no overhead for storing the string representation of the integer.