Redis数据类型

一、数据类型

String: 字符串类型
Hash: 哈希类型
List: 列表类型
Set: 集合类型
Sorted set: 有序集合类型

1.String: 字符串类型

1)添加数据

#语法
127.0.0.1:6379> set key value [EX seconds] [PX milliseconds] [NX|XX]
			  设置  键  值    生存时间(秒)  生存时间(毫秒)  如果不存在|如果存在

#添加key
127.0.0.1:6379> set k4 v4
OK

#添加多个key
127.0.0.1:6379> mset k5 v5 k6 v6 k7 v7 k8 v8
OK

#先查看再添加
127.0.0.1:6379> GETSET niulei sui
"cjdsb"
127.0.0.1:6379> get niulei
"sui"

#添加key同时设置生存时间
127.0.0.1:6379> set qiudao dsb ex 10
OK

#计数器增加值
127.0.0.1:6379> set good 1
OK
127.0.0.1:6379> get good
"1"
127.0.0.1:6379> INCR good
(integer) 2
127.0.0.1:6379> INCR good
(integer) 3

#计数器减少值
127.0.0.1:6379> DECR good
(integer) 2
127.0.0.1:6379> DECR good
(integer) 1
127.0.0.1:6379> DECR good
(integer) 0

#计数器增加指定值
127.0.0.1:6379> INCRBY good 100
(integer) 100
127.0.0.1:6379> INCRBY good 100
(integer) 200
127.0.0.1:6379> INCRBY good 100
(integer) 300
127.0.0.1:6379> INCRBY good 100
(integer) 400
127.0.0.1:6379> INCRBY good 100
(integer) 500
127.0.0.1:6379> get good
"500"

#计数器减少指定值
127.0.0.1:6379> DECRBY good 100
(integer) 400
127.0.0.1:6379> DECRBY good 100
(integer) 300
127.0.0.1:6379> DECRBY good 100
(integer) 200
127.0.0.1:6379> DECRBY good 100
(integer) 100
127.0.0.1:6379> get good
"100"

#计数器增加指定小数
127.0.0.1:6379> set good 8
OK
127.0.0.1:6379> get good
"8"
127.0.0.1:6379> INCRBYFLOAT good 0.1
"8.1"
127.0.0.1:6379> INCRBYFLOAT good 0.1
"8.2"
127.0.0.1:6379> INCRBYFLOAT good 0.1
"8.3"
127.0.0.1:6379> INCRBYFLOAT good 0.1
"8.4"

#计数器减少指定小数
127.0.0.1:6379> INCRBYFLOAT good -0.1
"8.3"
127.0.0.1:6379> INCRBYFLOAT good -0.1
"8.2"
127.0.0.1:6379> INCRBYFLOAT good -0.1
"8.1"
127.0.0.1:6379> INCRBYFLOAT good -0.1
"8"

2)查询数据

#查看key
127.0.0.1:6379> get k1
"qwrwjgefdlksdfjlbasklgfns"

#查看数据长度
127.0.0.1:6379> TYPE k1
string
127.0.0.1:6379> STRLEN k1
(integer) 25

#查看指定位置的数据
127.0.0.1:6379> GETRANGE k1 0 3
"qwrw"
127.0.0.1:6379> GETRANGE k1 0 0
"q"

#查看多个key的值
127.0.0.1:6379> mget k1 k2 k3 k4
1) "qwrwjgefdlksdfjlbasklgfns"
2) "v2"
3) "v3"
4) "v4"

3)删除数据

#删除key
127.0.0.1:6379> del k1
(integer) 1

#删除多个key
127.0.0.1:6379> del k2 k3 k4
(integer) 3

4)修改数据

#修改数据
127.0.0.1:6379> get k4
"v4"
127.0.0.1:6379> set k4 v444444
OK
127.0.0.1:6379> get k4
"v444444"

#追加数据
127.0.0.1:6379> APPEND k4 append
(integer) 13
127.0.0.1:6379> get k4
"v444444append"

#修改指定位置的数据
127.0.0.1:6379> get k4
"v444444append"
127.0.0.1:6379> GETRANGE k4 4 4
"4"
127.0.0.1:6379> SETRANGE k4 4 0
(integer) 13
127.0.0.1:6379> get k4
"v444044append"

2.Hash: 哈希类型

1)添加数据

#语法
127.0.0.1:6379> hset key field value
			   设置  键   列    值

#添加一个key的一个列
127.0.0.1:6379> hset teacher name qiudao
(integer) 1

#添加多个列
127.0.0.1:6379> hmset teacher id 1 age 78 sex nv 
OK

2)查看数据

#查看key的一列值
127.0.0.1:6379> hget teacher name
"qiudao"
127.0.0.1:6379> hget teacher sex
"nv"
127.0.0.1:6379> hget teacher age
"78"

#查看所有列的值
127.0.0.1:6379> hgetall teacher
1) "name"
2) "qiudao"
3) "id"
4) "1"
5) "age"
6) "78"
7) "sex"
8) "nv"

3)修改数据

#添加列
127.0.0.1:6379> hset teacher money -100000
(integer) 1
127.0.0.1:6379> hgetall teacher
 1) "name"
 2) "qiudao"
 3) "id"
 4) "1"
 5) "age"
 6) "78"
 7) "sex"
 8) "nv"
 9) "money"
10) "-100000"

#修改指定列的值
127.0.0.1:6379> HSET teacher name congtianqi
(integer) 0
127.0.0.1:6379> hgetall teacher
 1) "name"
 2) "congtianqi"
 3) "id"
 4) "1"
 5) "age"
 6) "78"
 7) "sex"
 8) "nv"
 9) "money"
10) "-100000"

#使用incr
127.0.0.1:6379> HINCRBY teacher id 1
(integer) 2
127.0.0.1:6379> hgetall teacher
 1) "name"
 2) "congtianqi"
 3) "id"
 4) "2"
 5) "age"
 6) "78"
 7) "sex"
 8) "nv"
 9) "money"
10) "-100000"
127.0.0.1:6379> HINCRBY teacher money 100000
(integer) 0
127.0.0.1:6379> hgetall teacher
 1) "name"
 2) "congtianqi"
 3) "id"
 4) "2"
 5) "age"
 6) "78"
 7) "sex"
 8) "nv"
 9) "money"
10) "0"

4)删除数据

#删除指定key的列
127.0.0.1:6379> HDEL teacher money sex
(integer) 2
127.0.0.1:6379> hgetall teacher
1) "name"
2) "congtianqi"
3) "id"
4) "2"
5) "age"
6) "78"

#删除整个key
127.0.0.1:6379> del teacher
(integer) 1

3.List: 列表类型

队列:先进先出
堆栈:先进后出

1)添加数据

#添加一个数据(往左侧添加)
127.0.0.1:6379> LPUSH list qiudao
(integer) 1

#添加多个数据(往左侧添加)
127.0.0.1:6379> LPUSH list zengdao niulei lijianpeng
(integer) 4
127.0.0.1:6379> LPUSH list liuxinyu
(integer) 5

#添加多条数据(往右侧添加)
127.0.0.1:6379> RPUSH list congtianqi
(integer) 6
127.0.0.1:6379> RPUSH zhangxin guozongyuan geshaosong
(integer) 2
127.0.0.1:6379> RPUSH list zhangxin guozongyuan geshaosong

2)查看数据

#查看列表所有数据
127.0.0.1:6379> LRANGE list 0 -1
1) "liuxinyu"
2) "lijianpeng"
3) "niulei"
4) "zengdao"
5) "qiudao"
6) "congtianqi"
7) "zhangxin"
8) "guozongyuan"
9) "geshaosong"

#查看列表类型数据量
127.0.0.1:6379> type list
list
127.0.0.1:6379> LLEN list
(integer) 9

#查看指定位置的数据
127.0.0.1:6379> LINDEX list 0
"liuxinyu"
127.0.0.1:6379> LINDEX list 1
"lijianpeng"

#读取队列数据,消费队列数据,消费一条少一条
1.使用lpush插入数据
127.0.0.1:6379> LPUSH list liukong
(integer) 10

2.查看数据
127.0.0.1:6379> LRANGE list 0 -1
 1) "liukong"
 2) "liuxinyu"
 3) "lijianpeng"
 4) "niulei"
 5) "zengdao"
 6) "qiudao"
 7) "congtianqi"
 8) "zhangxin"
 9) "guozongyuan"
10) "geshaosong"

3.使用LPOP消费数据
127.0.0.1:6379> LPOP list
"liukong"
127.0.0.1:6379> LPOP list
"liuxinyu"

4.再次查看数据
127.0.0.1:6379> LRANGE list 0 -1
1) "lijianpeng"
2) "niulei"
3) "zengdao"
4) "qiudao"
5) "congtianqi"
6) "zhangxin"
7) "guozongyuan"
8) "geshaosong"

5.使用RPOP消费数据
127.0.0.1:6379> RPOP list
"geshaosong"
127.0.0.1:6379> RPOP list
"guozongyuan"
127.0.0.1:6379> RPOP list
"zhangxin"

6.再次查看数据
127.0.0.1:6379> LRANGE list 0 -1
1) "lijianpeng"
2) "niulei"
3) "zengdao"
4) "qiudao"
5) "congtianqi"

#总结:
1.当使用LPUSH插入数据,使用LPOP消费数据时,是堆栈
  当使用RPUSH插入数据,使用RPOP消费数据时,是堆栈
2.当使用LPUSH插入数据,使用RPOP消费数据时,是队列
  当使用RPUSH插入数据,使用LPOP消费数据时,是队列

3)修改数据

#修改指定位置的数据
127.0.0.1:6379> LSET list 2 daijiadong
OK
127.0.0.1:6379> LRANGE list 0 -1
1) "lijianpeng"
2) "niulei"
3) "daijiadong"
4) "qiudao"
5) "congtianqi"

#将数据插入到列表指定位置
127.0.0.1:6379> LINSERT list after qiudao banzhang
(integer) 6
127.0.0.1:6379> LRANGE list 0 -1
1) "lijianpeng"
2) "niulei"
3) "daijiadong"
4) "qiudao"
5) "banzhang"
6) "congtianqi"

#将列表数据取出插入另一个列表
127.0.0.1:6379> RPOPLPUSH list list
"congtianqi"
127.0.0.1:6379> LRANGE list 0 -1
1) "congtianqi"
2) "lijianpeng"
3) "niulei"
4) "daijiadong"
5) "qiudao"
6) "banzhang"
127.0.0.1:6379>

4)删除数据

#删除key
127.0.0.1:6379> DEL list

#删除指定数量的值(当数量不够时,删除仅有的值)
127.0.0.1:6379> LRANGE list 0 -1
1) "congtianqi"
2) "lijianpeng"
3) "niulei"
4) "daijiadong"
5) "qiudao"
6) "banzhang"

127.0.0.1:6379> LREM list 2 congtianqi
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "lijianpeng"
2) "niulei"
3) "daijiadong"
4) "qiudao"
5) "banzhang"

#从头开始,索引为0,1,2的3个元素,其余全部删除改(反向删除)
127.0.0.1:6379> ltrim list 0 2
OK
127.0.0.1:6379> LRANGE list 0 -1
1) "lijianpeng"
2) "niulei"
3) "daijiadong"
127.0.0.1:6379>

4.Set: 集合类型

在微博应用中,可以将一个用户所有的关注人存在一个集合中,将其所有粉丝存在一个集合。Redis还为集合提供了求交集、并集、差集等操作,可以非常方便的实现如共同关注、共同喜好、二度好友等功能,对上面的所有集合操作,你还可以使用不同的命令选择将结果返回给客户端还是存集到一个新的集合中。

1)添加数据

#添加两个集合(集合中设置一样的值没有效果)
127.0.0.1:6379> sadd set1 1 2 3 5 7
(integer) 5

127.0.0.1:6379> sadd set2 1 3 5 8 9
(integer) 5

2)查看数据

#查看集合的数据
127.0.0.1:6379> SMEMBERS set1
1) "1"
2) "2"
3) "3"
4) "5"
5) "7"
127.0.0.1:6379> SMEMBERS set2
1) "1"
2) "3"
3) "5"
4) "8"
5) "9"

#查看集合中是否有某个值
127.0.0.1:6379> SISMEMBER set1 1
(integer) 1
127.0.0.1:6379> SISMEMBER set1 0
(integer) 0

#查看集合中的数据量
127.0.0.1:6379> SCARD set1
(integer) 5

#获取集合中随机值
127.0.0.1:6379> SRANDMEMBER set1 1
1) "2"

#取集合的交集
127.0.0.1:6379> SINTER set1 set2
1) "1"
2) "3"
3) "5"

#取集合的并集
127.0.0.1:6379> SUNION set1 set 2
1) "1"
2) "2"
3) "3"
4) "5"
5) "7"

#对比集合(取出前者有而后者没有的值)
127.0.0.1:6379> SDIFF set1 set2
1) "2"
2) "7"
127.0.0.1:6379> SDIFF set2 set1
1) "8"
2) "9"

3)修改数据

#移动集合中的值到另一个集合
127.0.0.1:6379> SMOVE set1 set2 2
(integer) 1
127.0.0.1:6379> SMEMBERS set1
1) "1"
2) "3"
3) "5"
4) "7"
127.0.0.1:6379> SMEMBERS set2
1) "1"
2) "2"
3) "3"
4) "5"
5) "8"
6) "9"

4)删除数据

#尾部数据查询,查询到即删除
127.0.0.1:6379> SPOP set1
"7"
127.0.0.1:6379> SPOP set1 2
1) "5"
2) "3"
127.0.0.1:6379> SMEMBERS set1
1) "1"

#删除指定值,若值不存在则只删除有的值
127.0.0.1:6379> SREM set2 1 2 3 5
(integer) 4
127.0.0.1:6379> SMEMBERS set2
1) "8"
2) "9"

5.有序集合 sorted-set

应用场景:
排行榜应用,取TOP N操作
这个需求与上面需求的不同之处在于,前面操作以时间为权重,这个是以某个条件为权重,比如按顶的次数排序,这时候就需要我们的sorted set出马了,将你要排序的值设置成sorted set的score,将具体的数据设置成相应的value,每次只需要执行一条ZADD命令即可。

1)添加数据

#添加一个数据
127.0.0.1:6379> ZADD linux9 90 banzhang 80 shengwei 50 jiwei 20 tiwei 100 xuewei
(integer) 5

2)查看数据

#查看排序后数据
127.0.0.1:6379> ZRANGE linux9 0 -1
1) "tiwei"
2) "jiwei"
3) "shengwei"
4) "banzhang"
5) "xuewei"

#查看排序及排序依据
127.0.0.1:6379> ZRANGE linux9 0 -1 WITHSCORES
 1) "tiwei"
 2) "20"
 3) "jiwei"
 4) "50"
 5) "shengwei"
 6) "80"
 7) "banzhang"
 8) "90"
 9) "xuewei"
10) "100"

#查看排序及排序依据(倒叙)
127.0.0.1:6379> ZREVRANGE linux9 0 -1 WITHSCORES
 1) "xuewei"
 2) "100"
 3) "banzhang"
 4) "90"
 5) "shengwei"
 6) "80"
 7) "jiwei"
 8) "50"
 9) "tiwei"
10) "20"

#查看指定成员的分数
127.0.0.1:6379> ZSCORE linux9 xuewei
"100"

#查看成员的数量
127.0.0.1:6379> ZCARD linux9
(integer) 5

#查看符合条件范围的成员
127.0.0.1:6379> ZCOUNT linux9 50 100
(integer) 4
127.0.0.1:6379> ZRANGEBYSCORE linux9 50 100
1) "jiwei"
2) "shengwei"
3) "banzhang"
4) "xuewei"
127.0.0.1:6379> ZRANGEBYSCORE linux9 50 100 WITHSCORES
1) "jiwei"
2) "50"
3) "shengwei"
4) "80"
5) "banzhang"
6) "90"
7) "xuewei"
8) "100"

3)修改数据

#修改数据的值,排行榜顺序也会改变
127.0.0.1:6379> ZINCRBY linux9 100 tiwei
"120"
127.0.0.1:6379> ZREVRANGE linux9 0 -1 WITHSCORES
 1) "tiwei"
 2) "120"
 3) "xuewei"
 4) "100"
 5) "banzhang"
 6) "90"
 7) "shengwei"
 8) "80"
 9) "jiwei"
10) "50"

4)删除数据

#删除集合中指定成员
127.0.0.1:6379> zrem linux9 shengwei 
(integer) 1
127.0.0.1:6379> ZREVRANGE linux9 0 -1 WITHSCORES
1) "tiwei"
2) "120"
3) "xuewei"
4) "100"
5) "banzhang"
6) "90"
7) "jiwei"
8) "50"

#删除整个key
127.0.0.1:6379> del linux9
(integer) 1
posted @ 2020-08-04 16:11  元气少女郭德纲!!  阅读(185)  评论(0编辑  收藏  举报