Redis提供了多种数值操作命令,可以在Redis中对数字进行自增、自减、计数等操作,下面是一些常用的数值操作命令及其功能:
- INCR:对指定键的值进行自增操作,增量为1。
- INCRBY:对指定键的值进行指定增量的自增操作。
- INCRBYFLOAT:对指定键的值进行指定增量的浮点型自增操作。
- DECR:对指定键的值进行自减操作,减量为1。
- DECRBY:对指定键的值进行指定减量的自减操作。
- GET:获取指定键的值。
- SET:设置指定键的值。
- SETNX:当指定键不存在时,设置指定键的值为指定值。
- MSET:同时设置多个键值对。
- MGET:同时获取多个键对应的值。
下面给出一些命令的具体用法及示例:
1.INCR
> SET counter 0
OK
> INCR counter
(integer) 1
> INCR counter
(integer) 2
2.INCRBY
> SET counter 0
OK
> INCRBY counter 10
(integer) 10
> INCRBY counter 5
(integer) 15
3.INCRBYFLOAT
> SET counter 0.5
OK
> INCRBYFLOAT counter 1.2
"1.7"
> INCRBYFLOAT counter 2.5
"4.2"
4.DECR
> SET counter 5
OK
> DECR counter
(integer) 4
> DECR counter
(integer) 3
5.DECRBY
> SET counter 15
OK
> DECRBY counter 5
(integer) 10
> DECRBY counter 3
(integer) 7
6.GET
> SET key1 "hello"
OK
> GET key1
"hello"
7.SET
> SET key2 "world"
OK
> GET key2
"world"
8.SETNX
> SETNX key3 "foo"
(integer) 1
> SETNX key3 "bar"
(integer) 0
> GET key3
"foo"
9.MSET
> MSET key4 "foo" key5 "bar" key6 "baz"
OK
> GET key4
"foo"
> GET key5
"bar"
> GET key6
"baz"
10.MGET
> SET key7 "foo"
OK
> SET key8 "bar"
OK
> MGET key7 key8
1) "foo"
2) "bar"
这些命令可以帮助我们在Redis中高效地进行数值类型数据的存储和操作。