Redis 数据结构 | 字符串

88 阅读8分钟

1. SET:为字符串键设置值

        redis> SET number "10086"
        OK
  1. 对于已存在值的键值对执行SET会覆盖
        redis> SET song_title "Get Wild"
        OK

       redis> SET song_title "Running to Horizon"
        OK
  1. 从26.12开始可以使用NX选项指定SET是否覆盖
        redis> SET password "123456" NX
        OK        -- 对尚未有值的password键进行设置,成功

       redis> SET password "999999" NX
        (nil)     -- password键已经有了值,设置失败
  1. XX选项,存在则更新、不存在返回nil
        redis> SET mongodb-homepage "mongodb.com" XX
        (nil)

        redis> SET mysql-homepage "mysql.org"
        OK     -- 为键mysql-homepage设置一个值

       redis> SET mysql-homepage "mysql.com" XX
        OK     -- 对键的值进行更新

复杂度:O(1)

2. GET:获取字符串键的值

  1. 获取指定字符串键的值
        redis> GET message
        "hello world"

       redis> GET number
        "10086"

       redis> GET homepage
        "redis.io"
  1. 如果没有则返回空
        redis> GET date
        (nil)

复杂度:O(1)

3. GETSET:获取旧值并更新新值

        redis> GET number     -- number键现在的值为"10086"
        "10086"

       redis> GETSET number "12345"
        "10086"                 -- 返回旧值

       redis> GET number     -- number键的值已被更新为"12345"
        "12345"

如果键值不存在

        redis> GET counter
        (nil)     -- 键不存在

       redis> GETSET counter 50
        (nil)     -- 返回空值作为旧值

       redis> GET counter
        "50"

复杂度:O(1)

4. MSET:一次为多个字符串键设置值

  1. MSET一次可以为多个键值设置值
        redis> MSET message "hello world" number "10086" homepage "redis.io"
        OK

       redis> GET message
        "hello world"

       redis> GET number
        "10086"

       redis> GET homepage
        "redis.io"
  1. 值存在MSET命令会更新
        redis> MSET message "good morning! " number "12345"
        OK

        redis> GET message
        "good morning! "

       redis> GET number
        "12345"

MSET相比SET可以有效减少客户端与服务端的交互次数

复杂度:O(N)

5. MGET:一次获取多个字符串键的值

  1. 获取多个键的值
        redis> MGET message number homepage
        1) "hello world"     -- message键的值
        2) "10086"            -- number键的值
        3) "redis.io"        -- homepage键的值
  1. 查询不存在的值返回空
        redis> MGET not-exists-key
        1) (nil)

MGET也可以减少客户端与服务端的交互次数

复杂度:O(N)

6. MSETNX:只在键不存在的情况下,一次为多个字符串键设置值

MSETNX命令在成功执行设置操作时返回1,在放弃执行设置操作时则返回0。

        redis> MGET k1 k2 k3 k4
        1) (nil)              -- 键k1、 k2和k3都不存在
        2) (nil)
        3) (nil)
        4) "hello world"     -- 键k4已存在

       redis> MSETNX k1 "one" k2 "two" k3 "three" k4 "four"
        (integer) 0     -- 因为键k4已存在,所以MSETNX未能执行设置操作

       redis> MGET k1 k2 k3 k4     -- 各个键的值没有变化
        1) (nil)
        2) (nil)
        3) (nil)
        4) "hello world"

复杂度:O(N)

7. STRLEN:获取字符串值的字节长度

  1. 获取长度
        redis> GET number
        "10086"

       redis> STRLEN number     -- number键的值长5字节
        (integer) 5

       redis> GET message
        "hello world"

       redis> STRLEN message    -- message键的值长11字节
        (integer) 11

       redis> GET book
        "The Design and Implementation of Redis"

       redis> STRLEN book       -- book键的值长38字节
        (integer) 38
  1. 不存在的键,返回0
        redis> STRLEN not-exists-key
        (integer) 0

复杂度:O(1)。

8. GETRANGE:获取字符串值指定索引范围上的内容

GETRANGE命令接受的是闭区间索引范围

        redis> GETRANGE message 0 4      -- 获取字符串值索引0至索引4上的内容
        "hello"

       redis> GETRANGE message 6 10     -- 获取字符串值索引6至索引10上的内容
        "world"

       redis> GETRANGE message 3 7      -- 获取字符串值的中间部分
        "lo wo"

       redis> GETRANGE message -11-7  -- 使用负数索引获取指定内容
        "hello"

复杂度:O(N),其中N为被返回内容的长度。

9. SETRANGE:对字符串值的指定索引范围进行设置

可以将字符串键的值从索引index开始的部分替换为指定的新内容,被替换内容的长度取决于新内容的长度

        redis> GET message
        "hello world"

       redis> SETRANGE message 6 "Redis"
        (integer) 11     -- 字符串值当前的长度为11字节

       redis> GET message
        "hello Redis"

给定的新内容比被替换的内容更长时,SETRANGE命令就会自动扩展被修改的字符串值

        redis> GET message
        "hello Redis"

       redis> SETRANGE message 5 ", this is a message send from peter."
        (integer) 41

       redis> GET message
        "hello, this is a message send from peter."

根据用户给定的index索引扩展字符串

        redis> GET greeting
        "hello"

        redis> SETRANGE greeting 10 "world"
        (integer) 15

        redis> GET greeting
        "hello\x00\x00\x00\x00\x00world"

\x00代表空字节。

复杂度:O(N),其中N为被修改内容的长度。

10. APPEND:追加新内容到值的末尾

将给定的内容追加到字符串键已有值的末尾,并返回字符串当前的长度

        redis> GET description
        "Redis"

        redis> APPEND description " is a database"
        (integer) 19     -- 追加操作执行完毕之后,值的长度

处理不存在的键时,APPEND命令会讲键值初始化为空字符串,在进行追加,最终效果跟SET类似。

        redis> GET append_msg  -- 键不存在
        (nil)

       redis> APPEND append_msg "hello"  -- 效果相当于执行SET append_msg "hello"
        (integer) 5

       redis> GET append_msg
        "hello"

复杂度:O(N),其中N为新追加内容的长度。

11. INCRBY、DECRBY:对整数值执行加法操作和减法操作

当字符串键存储的值能够被Redis解释为整数时,用户就可以通过INCRBY命令和DECRBY命令对被存储的整数值执行加法或减法操作。

        redis> SET number 100
        OK

       redis> GET number
        "100"

       redis> INCRBY number 300      -- 将键的值加上300
        (integer) 400

       redis> INCRBY number 256      -- 将键的值加上256
        (integer) 656

       redis> INCRBY number 1000     -- 将键的值加上1000
        (integer) 1656

       redis> GET number
        "1656"

减法

        redis> SET number 10086
        OK

       redis> GET number
        "10086"

       redis> DECRBY number 300      -- 将键的值减去300
        (integer) 9786

       redis> DECRBY number 786      -- 将键的值减去786
        (integer) 9000

       redis> DECRBY number 5500     -- 将键的值减去5500
        (integer) 3500

       redis> GET number
        "3500"

类型限制

        redis> SET pi 3.14
        OK

       redis> INCRBY pi 100              -- 不能对浮点数值执行
        (error) ERR value is not an integer or out of range

       redis> SET message "hello world"
        OK

       redis> INCRBY message             -- 不能对字符串值执行
        (error) ERR wrong number of arguments for 'incrby' command

       redis> SET big-number 123456789123456789123456789
        OK

       redis> INCRBY big-number 100     -- 不能对超过64位长度的整数执行
        (error) ERR value is not an integer or out of range

不能被Redis解释为整数,将会报错

        redis> INCRBY number 3.14                -- 不能使用浮点数作为增量
        (error) ERR value is not an integer or out of range

       redis> INCRBY number "hello world"     -- 不能使用字符串值作为增量
        (error) ERR value is not an integer or out of range

当键值不存在时

        redis> GET x             -- 键x不存在
        (nil)

       redis> INCRBY x 123     -- 先将键x的值初始化为0,然后再执行加上123的操作
        (integer) 123

       redis> GET x
        "123"

        redis> GET y             -- 键y不存在
        (nil)

       redis> DECRBY y 256     -- 先将键y的值初始化为0,再执行减去256的操作
        (integer) -256

       redis> GET y
        "-256"

复杂度:O(1)。

12. INCR、DECR:对整数值执行加1操作和减1操作

        redis> SET counter 100
        OK

       redis> INCR counter     -- 对整数值执行加1操作
        (integer) 101

       redis> INCR counter
        (integer) 102

       redis> INCR counter
        (integer) 103

       redis> DECR counter     -- 对整数值执行减1操作
        (integer) 102

       redis> DECR counter
        (integer) 101

       redis> DECR counter
        (integer) 100

复杂度:O(1)。

13. INCRBYFLOAT:对数字值执行浮点数加法操作

INCRBYFLOAT命令可以把一个浮点数增量加到字符串键存储的数字值上面,并返回键在执行加法操作之后的数字值作为命令的返回值。

        redis> SET decimal 3.14              -- 一个存储着浮点数值的键
        OK

       redis> GET decimal
        "3.14"

       redis> INCRBYFLOAT decimal 2.55     -- 将键decimal的值加上2.55
        "5.69"

       redis> GET decimal
        "5.69"

当键值对不存在时,会先将键的值初始化为0,然后再执行相应的加减操作。

        redis> GET x-point     -- 不存在的键
        (nil)

       redis> INCRBYFLOAT x-point 12.7829
        "12.7829"

       redis> GET x-point
        "12.7829"

对INCRBYFLOAT命令没有提供减法命令,只能传入负数进行减法。

        redis> SET pi 3.14
        OK

        redis> GET pi
        "3.14"

       redis> INCRBYFLOAT pi -1.1     -- 值减去1.1
        "2.04"

       redis> INCRBYFLOAT pi -0.7     -- 值减去0.7
        "1.34"

       redis> INCRBYFLOAT pi -1.3     -- 值减去1.3
        "0.04"
  • INCRBYFLOAT命令既可用于浮点数值,也可以用于整数值。
  • INCRBYFLOAT命令的增量既可以是浮点数,也可以是整数。
  • 当INCRBYFLOAT命令的执行结果可以表示为整数时,命令的执行结果将以整数形式存储。
  • 使用INCRBYFLOAT命令处理浮点数的时候,命令最多只会保留计算结果小数点后的17位数字,超过这个范围的小数将被截断:

复杂度:O(1)。