Redis基本数据类型—字符串类型

Bertha 。 2022-02-01 09:49 431阅读 0赞

字符串类型是Redis中最基本的数据类型,它可以存储任何形式的字符串,包括二进制数据。其他的类型都是以字符串类型为基础,可以看作是字符串类型的不同组织形式。

1、赋值与取值

格式

  1. redis> SET key value # 单个key赋值
  2. redis> MSET key1 value1 key2 value2 [key value…] # 多个key赋值
  3. redis> GET key # 获取单个key值
  4. redis> MGET key1 key2 [key …] # 获取多个key值

举例

  1. 127.0.0.1:6379> SET foo hello
  2. OK
  3. 127.0.0.1:6379> MSET a 1 b 2
  4. OK
  5. 127.0.0.1:6379> GET foo
  6. "hello"
  7. 127.0.0.1:6379> MGET a b
  8. 1) "1"
  9. 2) "2"

2、递增递减数字

格式

  1. redis> INCR key # key自增1,如果不存在,则默认是0
  2. redis> INCRBY key increment # key自增 increment
  3. redis> DECR key # key自减1
  4. redis> DECRBY key increment # key自减 increment
  5. redis> INCRBYFLOAT key increment # key增加一个双精度浮点数 increment

举例

  1. 127.0.0.1:6379> INCR num
  2. (integer) 1
  3. 127.0.0.1:6379> INCR num
  4. (integer) 2
  5. 127.0.0.1:6379> DECR num
  6. (integer) 1
  7. 127.0.0.1:6379> DECR num
  8. (integer) 0
  9. 127.0.0.1:6379> INCRBY num 5
  10. (integer) 5
  11. 127.0.0.1:6379> DECRBY num 2
  12. (integer) 3
  13. 127.0.0.1:6379> INCRBYFLOAT num 5.9
  14. "8.9"
  15. 127.0.0.1:6379> INCRBYFLOAT num 5E+4

3、向尾部追加值

格式

  1. redis> APPEND key value # 在key的值后面追加值value

举例

  1. 127.0.0.1:6379> GET foo
  2. "hello"
  3. 127.0.0.1:6379> APPEND foo " world!" # 因为有空格,需要用双引号以示区分
  4. (integer) 12
  5. 127.0.0.1:6379> GET foo
  6. "hello world!"

4、获取字符串长度

格式

  1. redis> STRLEN key # 获取字符串key的值的长度

举例

  1. 127.0.0.1:6379> SET foo hello
  2. OK
  3. 127.0.0.1:6379> STRLEN foo
  4. (integer) 5
  5. 127.0.0.1:6379> SET foo 你好 # “你”和“好”的中文UTF-8编码长度为3
  6. OK
  7. 127.0.0.1:6379> STRLEN foo
  8. (integer) 6

5、位操作

格式

  1. redis> GETBIT key offset # 获取key的二进制表示中第offset的位置的值
  2. redis> SETBIT key offset value # 设置key的二进制表示中第offset的位置的值为value
  3. redis> BITCOUNT key [start] [end] # 统计start与end字节之间1的个数
  4. redis> BITOP operation destroy key [key …] # 对多个key进行位操作,结果存到destroy中
  5. redis> BITPOS key value [start] [end] # 获取start与value字节之间键值为value的偏移量,从0开始

举例

  1. 127.0.0.1:6379> SET foo bar
  2. OK
  3. 127.0.0.1:6379> GETBIT foo 0
  4. (integer) 0
  5. 127.0.0.1:6379> GETBIT foo 6
  6. (integer) 1
  7. 127.0.0.1:6379> GETBIT foo 10000 # 当索引超过实际长度时默认为0
  8. (integer) 0
  9. 127.0.0.1:6379> SETBIT foo 6 0
  10. (integer) 1
  11. 127.0.0.1:6379> SETBIT foo 7 1
  12. (integer) 0
  13. 127.0.0.1:6379> GET foo
  14. "aar"
  15. 127.0.0.1:6379> BITCOUNT foo # 统计值为1的二进制位个数
  16. (integer) 10
  17. 127.0.0.1:6379> BITCOUNT foo 0 1 # 统计前两个字节包含的二进制值为1的个数
  18. (integer) 6

位操作:AND、OR、XOR、NOT

  1. 127.0.0.1:6379> SET fool1 bar
  2. OK
  3. 127.0.0.1:6379> GET fool1
  4. "bar"
  5. 127.0.0.1:6379> SET fool2 aar
  6. OK
  7. 127.0.0.1:6379> GET fool2
  8. "aar"
  9. 127.0.0.1:6379> BITOP OR res fool1 fool2 # OR运算
  10. (integer) 3
  11. 127.0.0.1:6379> GET res
  12. "car"
  13. 127.0.0.1:6379>

BITPOS

  1. 127.0.0.1:6379> GET foo
  2. "bar"
  3. 127.0.0.1:6379> BITPOS foo 1
  4. (integer) 1
  5. 127.0.0.1:6379> BITPOS foo 0
  6. (integer) 0
  7. 127.0.0.1:6379> BITPOS foo 1 1 2 # 查找第1和2个字节之间二进制值为1的偏移量
  8. (integer) 9

注:偏移量总是从头算起,与字节无关,如果二进制值全是1,则0的偏移量为键值长度的下一个字节的偏移量,这是因为redis会默认为键值长度之后的二进制位都是0.

发表评论

表情:
评论列表 (有 0 条评论,431人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Redis基本数据类型字符串类型

    字符串类型是Redis中最基本的数据类型,它可以存储任何形式的字符串,包括二进制数据。其他的类型都是以字符串类型为基础,可以看作是字符串类型的不同组织形式。 1、赋值与取值

    相关 Redis基本数据类型——列表类型

    列表类型可以存储一个有序的字符串列表,常用的操作是向列表两端添加元素、或者获取列表某一个片段。 Redis的列表类型是使用双向链表实现的。向两端添加、删除元素的时间复杂度为O