redis


String(字符串)

追加字符串

1
2
3
4
5
6
hominsu-redis-docker:0>get name
"hominsu"
hominsu-redis-docker:0>APPEND name " very cool"
"17" // 17 是追加完字符串之后的长度
hominsu-redis-docker:0>get name
"hominsu very cool"

如果追加不存在的 key,会创建这个 key(相当于 set key)

1
2
3
4
hominsu-redis-docker:0>APPEND hello "Hello World"
"11"
hominsu-redis-docker:0>get hello
"Hello World"

字符串长度

1
2
3
4
5
6
7
8
hominsu-redis-docker:0>STRLEN name
"17"
hominsu-redis-docker:0>APPEND name ", hahahah!!!"
"29"
hominsu-redis-docker:0>STRLEN name
"29"
hominsu-redis-docker:0>get name
"hominsu very cool, hahahah!!!"

自增与自减

自增:incr

1
2
3
4
5
6
7
8
9
10
hominsu-redis-docker:0>set views 0
"OK"
hominsu-redis-docker:0>get views
"0"
hominsu-redis-docker:0>incr views
"1"
hominsu-redis-docker:0>incr views
"2"
hominsu-redis-docker:0>get views
"2"

自减:decr

1
2
3
4
5
6
7
8
hominsu-redis-docker:0>decr views
"1"
hominsu-redis-docker:0>decr views
"0"
hominsu-redis-docker:0>decr views
"-1"
hominsu-redis-docker:0>get views
"-1"

设置步长

1
2
3
4
hominsu-redis-docker:0>INCRBY views 10
"9"
hominsu-redis-docker:0>DECRBY views 10
"-1"

如果字符串中的值不是一个整形就不能自增自减

1
2
3
4
5
6
7
8
9
10
11
12
13
hominsu-redis-docker:0>keys *
1) "age"
2) "views"
3) "hello"
4) "name"
hominsu-redis-docker:0>type views
"string"
hominsu-redis-docker:0>type hello
"string"
hominsu-redis-docker:0>get hello
"Hello World"
hominsu-redis-docker:0>INCR hello
"ERR value is not an integer or out of range"

截取字符串

通过 GETRANGE 可以获取指定范围内的字符串子串,要注意⚠️:填写的范围是闭区间0 - 6 即获取 7 个字符

1
2
3
4
hominsu-redis-docker:0>get name
"hominsu very cool, hahahah!!!"
hominsu-redis-docker:0>GETRANGE name 0 6
"hominsu"

这里 [0, -1] 获取的是整个字符串

1
2
hominsu-redis-docker:0>GETRANGE name 0 -1
"hominsu very cool, hahahah!!!"

替换字符串

通过 SETRANGE 可以替换指定开始位置的字符串

1
2
3
4
5
6
hominsu-redis-docker:0>get name
"hominsu very cool, hahahah!!!"
hominsu-redis-docker:0>SETRANGE name 0 "hauhau"
"29"
hominsu-redis-docker:0>get name
"hauhauu very cool, hahahah!!!"

同时设置键和过期时间

setex(set with expire)

1
2
3
4
5
6
7
8
9
10
hominsu-redis-docker:0>setex key1 10 "key1"
"OK"
hominsu-redis-docker:0>ttl key1
"6"
hominsu-redis-docker:0>get key1
"key1"
hominsu-redis-docker:0>ttl key1
"-2"
hominsu-redis-docker:0>get key1
null

不存在再设置

setnx(set if not exist)分布式锁中很很常用

1
2
3
4
5
6
7
8
hominsu-redis-docker:0>get age
"19"
hominsu-redis-docker:0>set age 20
"OK"
hominsu-redis-docker:0>setnx age 18
"0"
hominsu-redis-docker:0>setnx key 18
"1"

批量设置和获取键

mset:批量设置

1
2
3
4
5
6
7
8
9
10
11
hominsu-redis-docker:0>mset k1 v1 k2 v2 k3 v3
"OK"
hominsu-redis-docker:0>keys *
1) "age"
2) "k2"
3) "key"
4) "views"
5) "name"
6) "k1"
7) "k3"
8) "hello"

mget:批量获取

1
2
3
4
hominsu-redis-docker:0>mget k1 k2 k3
1) "v1"
2) "v2"
3) "v3"

同样的,也有 msetnx,但是没有 msetex,并且 msetnx 是一个原子操作,要么全部成功要么全部失败

存储对象

设置一个 user:1 对象,值用 json 格式保存:set user:1 {name:hominsu,age:20}(注意不能有空格)

1
2
3
4
5
6
7
hominsu-redis-docker:0>set user:1 {name:hominsu,age:20}
"OK"
hominsu-redis-docker:0>keys *
1) "user:1"
2) "name"
hominsu-redis-docker:0>get user:1
"{name:hominsu,age:20}"

通过 mset 来设置:

1
2
3
4
5
6
7
8
9
hominsu-redis-docker:0>mset user:1:name hominsu user:1:age 20
"OK"
hominsu-redis-docker:0>keys *
1) "user:1:age"
2) "user:1:name"
3) "name"
hominsu-redis-docker:0>mget user:1:name user:1:age
1) "hominsu"
2) "20"

虽然通过这种方式可以存放对象,但是不是最好的方法,对象最好还是通过 Hash 进行存储

GETSET

顾名思义:先获取,然后设置

1
2
3
4
5
6
7
8
hominsu-redis-docker:0>GETSET db redis
null
hominsu-redis-docker:0>get db
"redis"
hominsu-redis-docker:0>GETSET db mysql
"redis"
hominsu-redis-docker:0>get db
"mysql"

List(列表)

头插

LPUSH 头插,先进后出

1
2
3
4
5
6
7
8
9
10
hominsu-redis-docker:0>LPUSH list one
"1"
hominsu-redis-docker:0>LPUSH list two
"2"
hominsu-redis-docker:0>LPUSH list three
"3"
hominsu-redis-docker:0>LRANGE list 0 -1
1) "three"
2) "two"
3) "one"

尾插

RPUSH 尾插,先进先出

1
2
3
4
5
6
7
8
9
10
11
12
13
hominsu-redis-docker:0>RPUSH list four
"4"
hominsu-redis-docker:0>RPUSH list five
"5"
hominsu-redis-docker:0>RPUSH list six
"6"
hominsu-redis-docker:0>LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "four"
5) "five"
6) "six"

从头部弹出

LPOP

1
2
3
4
5
6
7
8
hominsu-redis-docker:0>LPOP list
"three"
hominsu-redis-docker:0>LRANGE list 0 -1
1) "two"
2) "one"
3) "four"
4) "five"
5) "six"

从尾部弹出

RPOP

1
2
3
4
5
6
7
hominsu-redis-docker:0>RPOP list
"six"
hominsu-redis-docker:0>LRANGE list 0 -1
1) "two"
2) "one"
3) "four"
4) "five"

索引

LINDEX key index

1
2
3
4
5
6
7
8
9
hominsu-redis-docker:0>LRANGE list 0 -1
1) "two"
2) "one"
3) "four"
4) "five"
hominsu-redis-docker:0>LINDEX list 0
"two"
hominsu-redis-docker:0>LINDEX list 1
"one"

修改元素

LSET key index value:修改指定索引位置的值

1
2
3
4
5
6
7
8
9
10
11
12
hominsu-redis-docker:0>LRANGE list 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
hominsu-redis-docker:0>LSET list 0 "0"
"OK"
hominsu-redis-docker:0>LRANGE list 0 -1
1) "0"
2) "two"
3) "three"
4) "four"

插入元素

LINSERT key BEFORE|AFTER pivot value:向某个元素的前面或者后面插入元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
hominsu-redis-docker:0>LRANGE list 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
hominsu-redis-docker:0>LINSERT list before one zero
"5"
hominsu-redis-docker:0>LINSERT list after four five
"6"
hominsu-redis-docker:0>LRANGE list 0 -1
1) "zero"
2) "one"
3) "two"
4) "three"
5) "four"
6) "five"

长度

LLEN key:通过下标截取

1
2
3
4
5
6
7
hominsu-redis-docker:0>LRANGE list 0 -1
1) "two"
2) "one"
3) "four"
4) "five"
hominsu-redis-docker:0>LLEN list
"4"

移除指定值

LREM key count value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
hominsu-redis-docker:0>LRANGE list 0 -1
1) "two"
2) "two"
3) "one"
4) "four"
5) "five"
6) "two"
hominsu-redis-docker:0>LREM list 1 two
"1"
hominsu-redis-docker:0>LRANGE list 0 -1
1) "two"
2) "one"
3) "four"
4) "five"
5) "two"
hominsu-redis-docker:0>LREM list 2 two
"2"
hominsu-redis-docker:0>LRANGE list 0 -1
1) "one"
2) "four"
3) "five"

TRIM 修剪

LTRIM key start end

1
2
3
4
5
6
7
8
9
10
hominsu-redis-docker:0>LRANGE list 0 -1
1) "hello1"
2) "hello2"
3) "hello3"
4) "hello4"
hominsu-redis-docker:0>LTRIM list 1 2
"OK"
hominsu-redis-docker:0>LRANGE list 0 -1
1) "hello2"
2) "hello3"

POP 同时 PUSH

RPOPLPUSH source distinction:将一个列表中的数据从尾部弹出并插入另一个列表的头部

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
hominsu-redis-docker:0>LRANGE list 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
hominsu-redis-docker:0>RPOPLPUSH list list-dst
"four"
hominsu-redis-docker:0>RPOPLPUSH list list-dst
"three"
hominsu-redis-docker:0>LRANGE list 0 -1
1) "one"
2) "two"
hominsu-redis-docker:0>LRANGE list-dst 0 -1
1) "three"
2) "four"

扩展:RPOPLPUSH source distinction timeout:同上,只是多了个超时

Set(集合)

Set 是无序不重复的!!!

基础操作

SADD key member:向一个集合中添加成员

1
2
3
4
5
6
hominsu-redis-docker:0>SADD set one
"1"
hominsu-redis-docker:0>SADD set two
"1"
hominsu-redis-docker:0>SADD set three
"1"

通过:SMEMBERS key,可以查看集合中的全部成员

1
2
3
4
hominsu-redis-docker:0>SMEMBERS set
1) "one"
2) "three"
3) "two"

SISMEMBER key member:判断一个 member 是否在集合中

1
2
hominsu-redis-docker:0>SISMEMBER set one
"1"

SCARD key:获取集合中的成员个数

1
2
hominsu-redis-docker:0>SCARD set
"3"

SREM key member ...:移除集合中的成员

1
2
3
4
5
hominsu-redis-docker:0>SREM set one
"1"
hominsu-redis-docker:0>SMEMBERS set
1) "three"
2) "two"

随机获取成员

SRANDMEMBER key count:从集合中随机获取指定数目的成员

1
2
3
4
5
6
7
8
9
10
hominsu-redis-docker:0>SMEMBERS set
1) "one"
2) "three"
3) "two"
hominsu-redis-docker:0>SRANDMEMBER set 2
1) "one"
2) "three"
hominsu-redis-docker:0>SRANDMEMBER set 2
1) "three"
2) "two"

随机删除成员

SPOP key:从一个集合中弹出一个成员,因为 Set 是无序的,因此 POP 可以看作是一个随机的删除

1
2
3
4
5
6
7
8
9
10
hominsu-redis-docker:0>SMEMBERS set
1) "one"
2) "three"
3) "two"
hominsu-redis-docker:0>SPOP set
"three"
hominsu-redis-docker:0>SPOP set
"one"
hominsu-redis-docker:0>SPOP set
"two"

移动

SMOVE source distinction member:将一个成员从一个集合移动到另一个集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
hominsu-redis-docker:0>SMEMBERS set
1) "one"
2) "three"
3) "two"
hominsu-redis-docker:0>SMEMBERS set2
1) "four"
hominsu-redis-docker:0>SMOVE set set2 three
"1"
hominsu-redis-docker:0>SMEMBERS set
1) "one"
2) "two"
hominsu-redis-docker:0>SMEMBERS set2
1) "three"
2) "four"

差集

SDIFF key [key ...]:差集,以第一个为参照

1
2
3
4
5
6
7
8
hominsu-redis-docker:0>SMEMBERS set1
1) "b"
2) "c"
3) "a"
hominsu-redis-docker:0>SMEMBERS set2
1) "e"
2) "d"
3) "c"

set1 与 set2 的差集

1
2
3
hominsu-redis-docker:0>SDIFF set1 set2
1) "b"
2) "a"

set2 与 set1 的差集

1
2
3
hominsu-redis-docker:0>SDIFF set2 set1
1) "e"
2) "d"

交集

SINTER [key ...]:查看集合的交集

1
2
hominsu-redis-docker:0>SINTER set1 set2
1) "c"

并集

SUNION [key ...]:查看集合的并集

1
2
3
4
5
6
hominsu-redis-docker:0>SUNION set1 set2
1) "e"
2) "a"
3) "c"
4) "b"
5) "d"

Hash(哈希)

类似于 map,键值对

基础操作

HSET key field value:设置一个键值对

HGET key field:取出一个键的值

1
2
3
4
hominsu-redis-docker:0>HSET hash hominsu 123456
"1"
hominsu-redis-docker:0>HGET hash hominsu
"123456"

HMSET key field value [field value ...]:设置多个键值对,如果键已存在,会将这个键的值覆盖掉

HMGET key field [field ...]:获取多个键的值

HGETALL key:获取全部的键值对,以 key - value 的格式输出

1
2
3
4
5
6
7
8
9
10
11
12
13
hominsu-redis-docker:0>HMSET hash tom 123 alice 456
"OK"
hominsu-redis-docker:0>HMGET hash hominsu tom alice
1) "123456"
2) "123"
3) "456"
hominsu-redis-docker:0>HGETALL hash
1) "hominsu"
2) "123456"
3) "tom"
4) "123"
5) "alice"
6) "456"

HDEL key field:删除指定的键值对

1
2
3
4
5
6
7
8
9
10
11
12
13
14
hominsu-redis-docker:0>HGETALL hash 
1) "hominsu"
2) "123456"
3) "tom"
4) "123"
5) "alice"
6) "456"
hominsu-redis-docker:0>HDEL hash tom
"1"
hominsu-redis-docker:0>HGETALL hash
1) "hominsu"
2) "123456"
3) "alice"
4) "456"

HELN key:获取 Hash 表中字段的数目

1
2
hominsu-redis-docker:0>HLEN hash
"2"

HEXIST key field:判断 Hash 表中的某个字段是否存在

1
2
3
4
hominsu-redis-docker:0>HEXISTS hash hominsu
"1"
hominsu-redis-docker:0>HEXISTS hash hauhau
"0"

分别获取键和值

HKEYS key:获取全部键

1
2
3
hominsu-redis-docker:0>HKEYS hash
1) "hominsu"
2) "alice"

HVALS key:获取全部值

1
2
3
hominsu-redis-docker:0>HVALS hash
1) "123456"
2) "456"

自增自减

HINCRBY key field increment:对 Hash 中的某个键值对进行自增

1
2
3
4
5
6
7
8
9
10
11
12
hominsu-redis-docker:0>HKEYS hash
1) "hominsu"
2) "alice"
3) "num"
hominsu-redis-docker:0>HVALS hash
1) "123456"
2) "456"
3) "0"
hominsu-redis-docker:0>HINCRBY hash num 2
"2"
hominsu-redis-docker:0>HINCRBY hash num 2
"4"

Hash 没有自减操作,但是可以将 HINCRBY key field increment 中的 INCREMENT设为负数实现

1
2
3
4
5
6
7
8
9
10
11
12
hominsu-redis-docker:0>HKEYS hash
1) "hominsu"
2) "alice"
3) "num"
hominsu-redis-docker:0>HVALS hash
1) "123456"
2) "456"
3) "0"
hominsu-redis-docker:0>HINCRBY hash num 2
"2"
hominsu-redis-docker:0>HINCRBY hash num 2
"4"

不存在再设置

HSETNX key field value:Hash 中不存在才能设置这个键值对

1
2
3
4
5
6
7
8
hominsu-redis-docker:0>HKEYS hash
1) "hominsu"
2) "alice"
3) "num"
hominsu-redis-docker:0>HSETNX hash hello world
"1"
hominsu-redis-docker:0>HSETNX hash hello world
"0"

存储对象

通过下面的方式可以存储用户信息这一类数据,Hash 更加适合对象的存储,String 适合字符串的存储

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
hominsu-redis-docker:0>HSET user:1 name hominsu
"1"
hominsu-redis-docker:0>HSET user:1 passwd 123456
"1"
hominsu-redis-docker:0>HGETALL user:1
1) "name"
2) "hominsu"
3) "passwd"
4) "123456"

hominsu-redis-docker:0>HSET user:2 name hauhau
"1"
hominsu-redis-docker:0>HSET user:2 passwd 654321
"1"
hominsu-redis-docker:0>HGETALL user:2
1) "name"
2) "hauhau"
3) "passwd"
4) "654321"

在 RDM 中看到的效果

Sorted Set(有序集合)

有序集合

基础操作

ZADD key [NX|XX] [CH] [INCR] score member [score member ...]:添加元素到有序集合中,score 类似于优先级,可以通过这个值实现排行榜的功能

ZRANGE key start stop [withscores]:获取有序集合中指定范围内的值,通过 withscores 关键字可以输出 score

1
2
3
4
5
6
7
8
hominsu-redis-docker:0>ZADD set 1 one
"1"
hominsu-redis-docker:0>ZADD set 2 two 3 three
"2"
hominsu-redis-docker:0>ZRANGE set 0 -1
1) "one"
2) "two"
3) "three"

ZCARD key:获取有序集合中的元素个数

1
2
hominsu-redis-docker:0>ZCARD salary
"2"

ZCOUNT key min max:获取 score 区间内的元素个数

1
2
hominsu-redis-docker:0>ZCOUNT salary 500 100000
"2"

ZREM key member [member ...]:移除一个或多个元素

1
2
3
4
5
6
7
hominsu-redis-docker:0>ZREM salary alice
"1"
hominsu-redis-docker:0>ZRANGE salary 0 -1 withscores
1) "tom"
2) "5000"
3) "hominsu"
4) "100000"

升序输出

ZRANGEBYSCORE key min max [withscores] [limit offset count]:根据 score 进行排序(升序),-inf:表示负无穷,+inf:表示正无穷;withscores 关键字可以将 score 也一并输出;(1:表示开区间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
hominsu-redis-docker:0>ZADD salary 100000 hominsu
"1"
hominsu-redis-docker:0>ZADD salary 5000 tom
"1"
hominsu-redis-docker:0>ZADD salary 10000 alice
"1"
hominsu-redis-docker:0>ZRANGE salary 0 -1
1) "tom"
2) "alice"
3) "hominsu"
hominsu-redis-docker:0>ZRANGEBYSCORE salary -inf +inf
1) "tom"
2) "alice"
3) "hominsu"
hominsu-redis-docker:0>ZRANGEBYSCORE salary -inf +inf withscores
1) "tom"
2) "5000"
3) "alice"
4) "10000"
5) "hominsu"
6) "100000"

降序输出

ZREVRANGEBYSCORE key max min [withscores] [limit offset count]:根据 score 进行排序(降序),-inf:表示负无穷,+inf:表示正无穷;withscores 关键字可以将 score 也一并输出;(1:表示开区间

1
2
3
4
5
hominsu-redis-docker:0>ZREVRANGEBYSCORE salary +inf -inf withscores
1) "hominsu"
2) "100000"
3) "tom"
4) "5000"