redis常用操作类型

字符串用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#设置键值 ex表示妙 nx:键不存在时创建
set a 1 ex 100 nx
#px 代表毫秒 xx:存在时创建
set a 1 px 1000 xx
#获取建
get a
#删除
del a
#获取所有键
keys *
#获取10条与a有关的从0位置开始
scan 0 match '*a*' 10
#自增
incr
#递减
decr
#指定键加3
incrby a 3
#指定键减3
decrby a 3
#字符串抓加456
append a 456
#批量赋值
mset a 1 b 2 c 3
#清空当前数据库
flushdb
#清空所有数据库
flushall

hash 用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
127.0.0.1:6379[2]> hset people age 18
(integer) 1
127.0.0.1:6379[2]> hset people name l
(integer) 1
127.0.0.1:6379[2]> hget people age
"18"
127.0.0.1:6379[2]> hmset people sex '男' phoen apple
OK
127.0.0.1:6379[2]> hget people sex
"\xe7\x94\xb7"
127.0.0.1:6379[2]> hget people phoen
"apple"
127.0.0.1:6379[2]> hmget people phoen sex name age
1) "apple"
2) "\xe7\x94\xb7"
3) "l"
4) "18"
127.0.0.1:6379[2]> hgetall people
1) "age"
2) "18"
3) "name"
4) "l"
5) "sex"
6) "\xe7\x94\xb7"
7) "phoen"
8) "apple"
127.0.0.1:6379[2]> HEXISTS people age
(integer) 1
127.0.0.1:6379[2]> HEXISTS people agea
(integer) 0
127.0.0.1:6379[2]> HINCRBY people age 1
(integer) 19
127.0.0.1:6379[2]> hget people age
"19"
127.0.0.1:6379[2]> hkeys people
1) "age"
2) "name"
3) "sex"
4) "phoen"
127.0.0.1:6379[2]> HVALS people
1) "19"
2) "l"
3) "\xe7\x94\xb7"
4) "apple"
127.0.0.1:6379[2]> hlen people
(integer) 4
127.0.0.1:6379[2]> hsetnx people color green
(integer) 1
127.0.0.1:6379[2]> hlen people
(integer) 5
127.0.0.1:6379[2]> hgetall people
1) "age"
2) "19"
3) "name"
4) "l"
5) "sex"
6) "\xe7\x94\xb7"
7) "phoen"
8) "apple"
9) "color"
10) "green"
127.0.0.1:6379[2]> hkeys people
1) "age"
2) "name"
3) "sex"
4) "phoen"
5) "color"

列表用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
127.0.0.1:6379[2]> lpush frined 1 2 3 4 5
(integer) 5
127.0.0.1:6379[2]> lpop frined
"5"
127.0.0.1:6379[2]> rpop frined
"1"
127.0.0.1:6379[2]> rpush drined 7
(integer) 1
127.0.0.1:6379[2]> rpop frined
"2"
127.0.0.1:6379[2]> llen frined
(integer) 2
127.0.0.1:6379[2]> lindex frined 1
"3"
127.0.0.1:6379[2]> lindex frined 2
(nil)
127.0.0.1:6379[2]> lindex frined 3
(nil)
127.0.0.1:6379[2]> lindex frined 0
"4"
127.0.0.1:6379[2]> lindex frined -1
"3"
127.0.0.1:6379[2]> lrange frined 0 2
1) "4"
2) "3"
127.0.0.1:6379[2]> lrange frined -2 2
1) "4"
2) "3"
127.0.0.1:6379[2]> lrange frined -1 2
1) "3"
127.0.0.1:6379[2]> lrange frined 0 3
1) "4"
2) "3"
127.0.0.1:6379[2]> llen frined
(integer) 2
127.0.0.1:6379[2]> lpush frined 1
(integer) 3
127.0.0.1:6379[2]> lrange frined 0 3
1) "1"
2) "4"
3) "3"
127.0.0.1:6379[2]> rpush frined 7
(integer) 4
127.0.0.1:6379[2]> lrange frined 0 4
1) "1"
2) "4"
3) "3"
4) "7"
127.0.0.1:6379[2]> rpushx frined 7
(integer) 5
127.0.0.1:6379[2]> lrange frined 0 5
1) "1"
2) "4"
3) "3"
4) "7"
5) "7"
127.0.0.1:6379[2]> lindex frined 1
"4"
127.0.0.1:6379[2]> lindex frined 0
"1"
127.0.0.1:6379[2]> blpop frined 1
1) "frined"
2) "1"
127.0.0.1:6379[2]> lpush friend 1 2 3
(integer) 3
127.0.0.1:6379[2]> linsert friend before 0 0
(integer) -1
127.0.0.1:6379[2]> llen friend
(integer) 3
127.0.0.1:6379[2]> linsert friend before 1 33
(integer) 4
127.0.0.1:6379[2]> llen friend
(integer) 4
127.0.0.1:6379[2]> lrange friend 0 4
1) "3"
2) "2"
3) "33"
4) "1"
127.0.0.1:6379[2]> lset friend 3 11
OK
127.0.0.1:6379[2]> lrange friend 0 4
1) "3"
2) "2"
3) "33"
4) "11"

集合用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#集合追加元素
127.0.0.1:6379[2]> sadd address home schoole company
(integer) 3
#返回集合数
127.0.0.1:6379[2]> scard address
(integer) 3
127.0.0.1:6379[2]> smembers address
1) "schoole"
2) "company"
3) "home"
127.0.0.1:6379[2]> srem address company
(integer) 1
127.0.0.1:6379[2]> smembers address
1) "schoole"
2) "home"
#随即取多个元素
127.0.0.1:6379[2]> srandmember address 1
1) "schoole"
127.0.0.1:6379[2]> srandmember address 2
1) "schoole"
2) "home"
127.0.0.1:6379[2]> srandmember address 1
1) "home"
127.0.0.1:6379[2]> srandmember address 1
1) "home"
127.0.0.1:6379[2]> sadd address2 hospital
(integer) 1
#移动元素到另一个集合
127.0.0.1:6379[2]> smove address address2 schoole
(integer) 1
127.0.0.1:6379[2]> smove address address2 school
(integer) 0
127.0.0.1:6379[2]> smembers address2
1) "schoole"
2) "hospital"
127.0.0.1:6379[2]> smembers address
1) "home"
#判断元素是否存在
127.0.0.1:6379[2]> sismember address home
(integer) 1
127.0.0.1:6379[2]> sismember address schoole
(integer) 0
127.0.0.1:6379[2]> sunion address address2
1) "hospital"
2) "schoole"
3) "home"
127.0.0.1:6379[2]> sadd address park
(integer) 1
127.0.0.1:6379[2]> sadd address2 park
(integer) 1
#并集
127.0.0.1:6379[2]> sunion address address2
1) "hospital"
2) "schoole"
3) "park"
4) "home"
#并集保存在addres3
127.0.0.1:6379[2]> SUNIONSTORE address3 address address2
(integer) 4
127.0.0.1:6379[2]> smembers address3
1) "hospital"
2) "schoole"
3) "park"
4) "home"
#获取交集
127.0.0.1:6379[2]> sinter address address2
1) "park"
127.0.0.1:6379[2]> sdiff address address2
1) "home"
127.0.0.1:6379[2]> smembers address
1) "park"
2) "home"
#获取集合元素
127.0.0.1:6379[2]> smembers address2
1) "park"
2) "schoole"
3) "hospital"
##获取差集
127.0.0.1:6379[2]> sdiff address2 address
1) "hospital"
2) "schoole"

有序集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#增加集合元素
127.0.0.1:6379> zadd orderedset 1 home 2 school
(integer) 2
#返回元素个数
127.0.0.1:6379> zcard orderedset
(integer) 2
#返回分数在0-30之间的元素个数
127.0.0.1:6379> zcount orderedset 0 30
(integer) 2
#对home元素累加2
127.0.0.1:6379> zincrby orderedset 2 home
"3"
#获取home的值
127.0.0.1:6379> zscore orderedset home
"3"
127.0.0.1:6379> zscore orderedset school
"2"
#通过索引(0-2)获取元素并带上分数
127.0.0.1:6379> zrange orderedset 0 2 withscores
1) "school"
2) "2"
3) "home"
4) "3"
127.0.0.1:6379> zrangebyscore orderedset 0 2 withscores limit 0 2
1) "school"
2) "2"
#获取分数在1-3之间的元素,从索引0到索引2
127.0.0.1:6379> zrangebyscore orderedset 1 3 withscores limit 0 2
1) "school"
2) "2"
3) "home"
4) "3"
127.0.0.1:6379> zrangebyscore orderedset 2 3 withscores limit 0 2
1) "school"
2) "2"
3) "home"
4) "3"
127.0.0.1:6379> zincrby orderedset 2 home
"5"
127.0.0.1:6379> zrangebyscore orderedset 2 3 withscores limit 0 2
1) "school"
2) "2"
127.0.0.1:6379> zrangebyscore orderedset 2 4 withscores limit 0 2
1) "school"
2) "2"
127.0.0.1:6379> zrangebyscore orderedset 2 5 withscores limit 0 2
1) "school"
2) "2"
3) "home"
4) "5"
#移除元素
127.0.0.1:6379> zrem orderedset home
(integer) 1
#移除分数在1-3之间的元素
127.0.0.1:6379> zremrangebyscore orderedset 1 3
(integer) 1
127.0.0.1:6379> zrank orderedset home
(nil)
127.0.0.1:6379> zadd orderedset 1 home 2 school
(integer) 2
#获取home顺序索引
127.0.0.1:6379> zrank orderedset home
(integer) 0
127.0.0.1:6379> zrank orderedset school
(integer) 1
127.0.0.1:6379> zrevrank orderedset school
(integer) 0
#获取home逆序索引
127.0.0.1:6379> zrevrank orderedset home
(integer) 1
你是本站第位访客 本站访客数人次
Fork me on GitHub