目录

Redis 常用命令操作

Redis 和其他很多 key-value 数据库的不同之处在于,Redis 不仅支持简单的字符串键值对,它还提供了一系列数据结构类型值,比如列表、哈希、集合和有序集,并在这些数据结构类型上定义了一套强大的 API。通过对不同类型的值进行操作,Redis 可以很轻易地完成其他只支持字符串键值对的 key-value 数据库很难(或者无法)完成的任务。在 Redis 的内部,数据结构类型值由高效的数据结构和算法进行支持,并且在 Redis 自身的构建当中,也大量用到了这些数据结构。

数据类型

  • 字符串:String
  • 哈希或散列:Hash
  • 列表:List
  • 集合:Set
  • 有序集合:Sorted Set

操作实例

字符串

Redis 数据库是以无序的方式存放数据库键的,一个新加入的键可能会出现在数据库的任何位置上。因此我们在使用 Redis 的过程中不应该对键在数据库中的摆放位置做任何假设,以免造成错误。

  • 设置字符串键值:
1
2
127.0.0.1:6379> SET phone 10086
OK
  • 获取字符串键值:
1
2
127.0.0.1:6379> GET phone
"10086"
  • 覆盖字符串键值:
1
2
3
4
5
6
7
8
127.0.0.1:6379> SET redis "Redis is an in-memory database that persists on disk"
OK
127.0.0.1:6379> GET redis
"Redis is an in-memory database that persists on disk"
127.0.0.1:6379> SET redis "Redis is often referred as a data structures server"
OK
127.0.0.1:6379> GET redis
"Redis is often referred as a data structures server"
  • 仅在键没有值时进行设置:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
127.0.0.1:6379> GET password
(nil)
127.0.0.1:6379> SET password "password" NX
OK
127.0.0.1:6379> GET password
"password"
127.0.0.1:6379> SET password "123456" NX
(nil)
127.0.0.1:6379> GET password
"password"
  • 仅在键已有值时进行设置:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
127.0.0.1:6379> GET redis:password
(nil)
127.0.0.1:6379> SET redis:password "redis.io" XX
(nil)
127.0.0.1:6379> GET redis:password
(nil)
127.0.0.1:6379> SET redis:password "redis.io"
OK
127.0.0.1:6379> SET redis:password "Redis.IO" XX
OK
  • 获取键目前的值,然后为键设置新值,最后把之前获取到的旧值返回:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
127.0.0.1:6379> GET phone
(nil)
127.0.0.1:6379> GETSET phone 10086
(nil)
127.0.0.1:6379> SET phone 10086
OK
127.0.0.1:6379> GETSET phone 10001
"10086"
127.0.0.1:6379> GET phone
"10001"
  • 一次为多个字符串键设置值:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
127.0.0.1:6379> MSET a hello b world c 10086
OK
127.0.0.1:6379> MGET a
1) "hello"
127.0.0.1:6379> MGET b
1) "world"
127.0.0.1:6379> MGET c
1) "10086"
127.0.0.1:6379> MSET a redis b memory c 10001
OK
127.0.0.1:6379> MGET a
1) "redis"
127.0.0.1:6379> MGET b
1) "memory"
127.0.0.1:6379> MGET c
1) "10001"
  • 一次获取多个字符串键的值:
1
2
3
4
5
127.0.0.1:6379> MSET a "Hello World" b 10086
OK
127.0.0.1:6379> MGET a b
1) "Hello World"
2) "10086"
  • 仅在键不存在时,一次为多个字符串键设置值:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
127.0.0.1:6379> MSET a hello b world c 10086
OK
127.0.0.1:6379> MSETNX a 1 b 2 c 3 d 4
(integer) 0
127.0.0.1:6379> MGET a b c d
1) "hello"
2) "world"
3) "10086"
4) (nil)
127.0.0.1:6379> MSETNX d 4
(integer) 1
127.0.0.1:6379> MGET a b c d
1) "hello"
2) "world"
3) "10086"
4) "4"
  • 获取字符串值的字节长度:
1
2
3
4
5
6
7
8
127.0.0.1:6379> SET org "中国人民解放军"
OK
127.0.0.1:6379> STRLEN org
(integer) 21
127.0.0.1:6379> SET str "Hello World!"
OK
127.0.0.1:6379> STRLEN str
(integer) 12
  • 获取字符串值指定索引范围上的内容:
1
2
3
4
127.0.0.1:6379> SET org Microsoft
OK
127.0.0.1:6379> GETRANGE org 0 4
"Micro"
  • 对字符串的指定索引范围进行设置:
1
2
3
4
5
6
127.0.0.1:6379> SET message "Hello World"
OK
127.0.0.1:6379> SETRANGE message 6 Redis
(integer) 11
127.0.0.1:6379> GET message
"Hello Redis"
  • 自动扩展被修改的字符串:
1
2
3
4
5
6
7
8
127.0.0.1:6379> SET message "Hello World"
OK
127.0.0.1:6379> GET message
"Hello World"
127.0.0.1:6379> SETRANGE message 6 "Redis, Hello World"
(integer) 24
127.0.0.1:6379> GET message
"Hello Redis, Hello World"
  • 在值里面填充空字节:

\x00 符号代表一个空字符。

1
2
3
4
5
6
7
8
127.0.0.1:6379> SET message Hello
OK
127.0.0.1:6379> get message
"Hello"
127.0.0.1:6379> SETRANGE message 10 Redis
(integer) 15
127.0.0.1:6379> GET message
"Hello\x00\x00\x00\x00\x00Redis"
  • 追加新内容到值的末尾:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
127.0.0.1:6379> SET message Hello
OK
127.0.0.1:6379> APPEND message " Redis"
(integer) 11
127.0.0.1:6379> GET message
"Hello Redis"
127.0.0.1:6379> APPEND message ", Hello World"
(integer) 24
127.0.0.1:6379> GET message
"Hello Redis, Hello World"
  • 不存在的键追加内容:

效果等同于 SET 命令。

1
2
3
4
5
6
7
8
127.0.0.1:6379> GET message
(nil)
127.0.0.1:6379> APPEND message "Hello Redis"
(integer) 11
127.0.0.1:6379> APPEND message ", Hello World"
(integer) 24
127.0.0.1:6379> GET message
"Hello Redis, Hello World"
  • 整数值的加法、减法操作:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
127.0.0.1:6379> SET number 1000
OK
127.0.0.1:6379> GET number
"1000"
127.0.0.1:6379> INCRBY number 100
(integer) 1100
127.0.0.1:6379> INCRBY number 300
(integer) 1400
127.0.0.1:6379> GET number
"1400"
127.0.0.1:6379> DECRBY number 300
(integer) 1100
127.0.0.1:6379> DECRBY number 100
(integer) 1000
127.0.0.1:6379> get number
"1000"

错误的实例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
127.0.0.1:6379> SET number 1.105
OK
127.0.0.1:6379> INCRBY number 1
(error) ERR value is not an integer or out of range
127.0.0.1:6379> SET message Hello
OK
127.0.0.1:6379> DECRBY message 2
(error) ERR value is not an integer or out of range
127.0.0.1:6379> SET bignumber 123456789123456789123456789
OK
127.0.0.1:6379> INCRBY bignumber 3
(error) ERR value is not an integer or out of range
  • 不存在键的加法、减法运算:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
127.0.0.1:6379> GET a
(nil)
127.0.0.1:6379> INCRBY a 10
(integer) 10
127.0.0.1:6379> DECRBY b 10
(integer) -10
127.0.0.1:6379> GET a
"10"
127.0.0.1:6379> GET b
"-10"
  • 整数的自增、自减操作:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
127.0.0.1:6379> GET x
(nil)
127.0.0.1:6379> INCR x
(integer) 1
127.0.0.1:6379> INCR y
(integer) 1
127.0.0.1:6379> DECR x
(integer) 0
127.0.0.1:6379> DECR y
(integer) 0
127.0.0.1:6379> GET x
"0"
127.0.0.1:6379> GET y
"0"
  • 浮点数的加法、减法及不存在键的操作:

INCRBYFLOAT 只会保留计算结果小数点后 17 位数字。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
127.0.0.1:6379> SET decimal 3.1415
OK
127.0.0.1:6379> GET decimal
"3.1415"
127.0.0.1:6379> INCRBYFLOAT decimal 2.55
"5.6915"
127.0.0.1:6379> GET decimal
"5.6915"
127.0.0.1:6379> INCRBYFLOAT decimal -2.55
"3.1415"
127.0.0.1:6379> GET decimal
"3.1415"
127.0.0.1:6379> GET point
(nil)
127.0.0.1:6379> SET point 1.12345
OK
127.0.0.1:6379> GET point
"1.12345"
127.0.0.1:6379> GET x
(nil)
127.0.0.1:6379> INCRBYFLOAT x 0.123456789123456789123456789
"0.12345678912345679"
127.0.0.1:6379> GET x
"0.12345678912345679"

哈希或散列

  • 为字段设置值:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
127.0.0.1:6379> HSET user:1001 name imajinyun age 18
(integer) 2
127.0.0.1:6379> HSET user:1001 gender male
(integer) 1
127.0.0.1:6379> HSET user:1001 created_at 1573036176
(integer) 1
127.0.0.1:6379> HGETALL user:1001
1) "name"
2) "imajinyun"
3) "age"
4) "18"
5) "gender"
6) "male"
7) "created_at"
8) "1573036176"
  • 使用新值覆盖旧值:
1
2
3
4
5
6
127.0.0.1:6379> HSET user:1001 name iphone
(integer) 1
127.0.0.1:6379> HSET user:1001 name imajinyun
(integer) 0
127.0.0.1:6379> HGET user:1001 name
"imajinyun"
  • 仅在字段不存在的情况下为它设置值:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
127.0.0.1:6379> HSETNX user:1001 name peter
(integer) 1
127.0.0.1:6379> HSETNX user:1001 name imajinyun
(integer) 0
127.0.0.1:6379> HGET user:1001 name
"peter"
127.0.0.1:6379> HSETNX user:1001 age 18
(integer) 1
127.0.0.1:6379> HGETALL user:1001
1) "name"
2) "peter"
3) "age"
4) "18"
  • 获取单个、多个字段的值:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
127.0.0.1:6379> HSET user:1001 name imajinyun age 18
(integer) 2
127.0.0.1:6379> HGET user:1001 name
"imajinyun"
127.0.0.1:6379> HGET user:1001 age
"18"
127.0.0.1:6379> HGETALL user:1001
1) "name"
2) "imajinyun"
3) "age"
4) "18"
  • 获取不存在的字段或者不存在的哈希:
1
2
3
4
5
6
7
8
127.0.0.1:6379> HSET user:1001 name imajinyun
(integer) 1
127.0.0.1:6379> HGET user:1001 age
(nil)
127.0.0.1:6379> HGET user:1002 age
(nil)
127.0.0.1:6379> HGETALL user:1002
(empty list or set)
  • 对字段存储的整数值进行加法或减法操作:
1
127.0.0.1:6379>

列表

集合

有序集合

其它