Redis 数据类型

Redis 数据类型简介

Runoob 数据类型相关

# String 类型    
127.0.0.1:6379> SET key value [EX seconds] [PX milliseconds] [NX|XX]
127.0.0.1:6379> MSET key value [key value ...]
...
 

# Hash 类型	 
127.0.0.1:6379> HSET key field value
127.0.0.1:6379> HMSET key field value [field value ...]
...


# List 类型		 
127.0.0.1:6379> LPUSH key value [value ...]
127.0.0.1:6379> RPUSH key value [value ...]
127.0.0.1:6379> RPOPLPUSH source destination
127.0.0.1:6379> LPOP key
127.0.0.1:6379> RPOP key
...


# Set 类型		 
127.0.0.1:6379> SADD key member [member ...]
... 


# Sorted-Set 类型		  
127.0.0.1:6379> ZADD key [NX|XX] [CH] [INCR] score member [score member ...]



# [NX|XX] 的含义
NX – Only set the key if it does not already exist.
XX – Only set the key if it already exist.

String: 字符串类型

添加数据

# 添加 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 zhangxueyou sui
"cjdsb"
127.0.0.1:6379> get zhangxueyou
"sui"

# 添加 key,同时设置生存时间
127.0.0.1:6379> set laowang 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"

查询数据

# 查看 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"

删除数据

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

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

修改数据

# 修改数据
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"

Hash: 哈希类型

添加数据

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

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

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

查看数据

# 查看key的一列值
127.0.0.1:6379> hget teacher name
"laowang"
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) "laowang"
3) "id"
4) "1"
5) "age"
6) "78"
7) "sex"
8) "nv"

修改数据

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

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

删除数据

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

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

List: 列表类型

利用 Redis 中提供的 List 类型,可以实现两种数据结构:

①.队列:先进先出

②.堆栈:先进后出

添加数据

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

# 添加多个数据(往左侧添加)
127.0.0.1:6379> LPUSH list laoguo zhangxueyou jackma
(integer) 4
127.0.0.1:6379> LPUSH list liudehua
(integer) 5

# 添加多条数据(往右侧添加)
127.0.0.1:6379> RPUSH list ponyma
(integer) 6
127.0.0.1:6379> RPUSH gutianle fanbingbing chuanpu
(integer) 2
127.0.0.1:6379> RPUSH list gutianle fanbingbing chuanpu

查看数据

# 查看列表所有数据
127.0.0.1:6379> LRANGE list 0 -1
1) "liudehua"
2) "jackma"
3) "zhangxueyou"
4) "laoguo"
5) "laowang"
6) "ponyma"
7) "gutianle"
8) "fanbingbing"
9) "chuanpu"

# 查看列表类型数据量
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
"liudehua"
127.0.0.1:6379> LINDEX list 1
"jackma"

# 读取队列数据,消费队列数据,消费一条少一条
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) "liudehua"
 3) "jackma"
 4) "zhangxueyou"
 5) "laoguo"
 6) "laowang"
 7) "ponyma"
 8) "gutianle"
 9) "fanbingbing"
10) "chuanpu"

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

4.再次查看数据
127.0.0.1:6379> LRANGE list 0 -1
1) "jackma"
2) "zhangxueyou"
3) "laoguo"
4) "laowang"
5) "ponyma"
6) "gutianle"
7) "fanbingbing"
8) "chuanpu"

5.使用RPOP消费数据
127.0.0.1:6379> RPOP list
"chuanpu"
127.0.0.1:6379> RPOP list
"fanbingbing"
127.0.0.1:6379> RPOP list
"gutianle"

6.再次查看数据
127.0.0.1:6379> LRANGE list 0 -1
1) "jackma"
2) "zhangxueyou"
3) "laoguo"
4) "laowang"
5) "ponyma"

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

修改数据

# 修改指定位置的数据
127.0.0.1:6379> LSET list 2 nezha
OK
127.0.0.1:6379> LRANGE list 0 -1
1) "jackma"
2) "zhangxueyou"
3) "nezha"
4) "laowang"
5) "ponyma"

# 将数据插入到列表指定位置
127.0.0.1:6379> LINSERT list after laowang banzhang
(integer) 6
127.0.0.1:6379> LRANGE list 0 -1
1) "jackma"
2) "zhangxueyou"
3) "nezha"
4) "laowang"
5) "banzhang"
6) "ponyma"

# 将列表数据取出插入另一个列表
127.0.0.1:6379> RPOPLPUSH list list
"ponyma"
127.0.0.1:6379> LRANGE list 0 -1
1) "ponyma"
2) "jackma"
3) "zhangxueyou"
4) "nezha"
5) "laowang"
6) "banzhang"
127.0.0.1:6379>

删除数据

# 删除key
127.0.0.1:6379> DEL list

# 删除指定数量的值(当数量不够时,删除仅有的值)
127.0.0.1:6379> LRANGE list 0 -1
1) "ponyma"
2) "jackma"
3) "zhangxueyou"
4) "nezha"
5) "laowang"
6) "banzhang"

127.0.0.1:6379> LREM list 2 ponyma
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "jackma"
2) "zhangxueyou"
3) "nezha"
4) "laowang"
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) "jackma"
2) "zhangxueyou"
3) "nezha"
127.0.0.1:6379>

Set: 集合类型

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

添加数据

# 添加两个集合(集合中设置一样的值没有效果)
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

查看数据

# 查看集合的数据
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"

# 对比集合的数据时,拿前者与后者对比,取出前者有而后者没有的,不管后者由而前者没有的
127.0.0.1:6379> SMEMBERS set3
1) "0"
2) "1"
3) "5"
4) "8"
5) "9"
127.0.0.1:6379> SMEMBERS set2
1) "1"
2) "3"
3) "4"
4) "6"
5) "8"
127.0.0.1:6379> SMEMBERS set1
1) "1"
2) "2"
3) "3"
4) "5"
5) "7"

# 对比
127.0.0.1:6379> SDIFF set1 set2
1) "2"
2) "5"
3) "7"
# 三者对比
127.0.0.1:6379> SDIFF set1 set2 set3
1) "2"
2) "7"

修改数据

# 移动集合中的值到另一个集合
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"

删除数据

# 尾部数据查询,查询到即删除
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"

Sorted-Set: 有序集合类型

应用场景:排行榜应用,取 TOP-N 操作

这个需求与上面需求的不同之处在于,前面操作以时间为权重,这个是以某个条件为权重,比如按顶的次数排序,这时候就需要我们的 Sorted-Set 出马了,将你要排序的值设置成 Sorted-Set 的 Score,将具体的数据设置成相应的 Value,每次只需要执行一条 ZADD 命令即可。

添加数据

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

查看数据

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

# 查看排序及排序依据
127.0.0.1:6379> ZRANGE exam 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 exam 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 exam xuewei
"100"

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

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

修改数据

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

删除数据

# 删除集合中指定成员
127.0.0.1:6379> zrem exam shengwei 
(integer) 1
127.0.0.1:6379> ZREVRANGE exam 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 exam
(integer) 1
posted @ 2020-08-05 16:34  拨云见日z  阅读(182)  评论(0编辑  收藏  举报