Redis --- 数据类型之列表 数据类型之hash

发布时间 2023-04-17 19:36:15作者: ChAnAn

一、数据类型之列表

列表简介

Redis的list是一个字符队列,先进后出,一个key可以有多个值

列表操作

lpush key values [value ...]
将一个或多个值value插入到列表key的表头,Key不存在,则创建key

127.0.0.1:6379> FLUSHALL
OK

# lpush命令,创建变量student,类型为列表类型,值为bob,tom,lucy,lili
127.0.0.1:6379> LPUSH student bob tom lucy lili
(integer) 4
127.0.0.1:6379> type student
list

# 将A和B存储到列表student的表头
127.0.0.1:6379> LPUSH student A B
(integer) 6

# lrange   key  start   stop		从列表中取值
# 从开始位置读取key的值到stop结束
# 取出列表中的所有值,0 指第一个值,-1 指最后一个值
127.0.0.1:6379> LRANGE student 0 -1
1) "B"
2) "A"
3) "lili"
4) "lucy"
5) "tom"
6) "bob"

# 取出列表中的值,0 指第一个值,1 指第二个值
127.0.0.1:6379> LRANGE student 0 1
1) "B"
2) "A"

# 从后往前取出列表中的值,-2 指倒数第二个值,-1 指倒数第一个值
127.0.0.1:6379> LRANGE student -2 -1
1) "tom"
2) "bob"

# 从后往前取出列表中的值,2 指第三个值,4 指第五个值
127.0.0.1:6379> LRANGE student 2 4
1) "lili"
2) "lucy"
3) "tom"


# lpop  key移除并返回列表头元素数据,key不存在则返回nil
# 取出列表中的所有值,0 指第一个值,-1 指最后一个值
127.0.0.1:6379> LRANGE student 0 -1
1) "B"
2) "A"
3) "lili"
4) "lucy"
5) "tom"
6) "bob"

# 删除列表第一个值,返回被删除的值
127.0.0.1:6379> LPOP student
"B"

127.0.0.1:6379> LRANGE student 0 -1
1) "A"
2) "lili"
3) "lucy"
4) "tom"
5) "bob"

127.0.0.1:6379> LPOP student
"A"

127.0.0.1:6379> LRANGE student 0 -1
1) "lili"
2) "lucy"
3) "tom"
4) "bob"

# llen   key	返回列表key的长度
# 统计列表中值的数量
127.0.0.1:6379> TYPE student
list
127.0.0.1:6379> LLEN student
(integer) 4

# 取出列表中的所有值,0 第一个,-1 最后一个
127.0.0.1:6379> LRANGE student 0 -1
1) "lili"
2) "lucy"
3) "tom"
4) "bob"


# lindex   key   index	返回列表中第 index 个值
# lindex 输出列表中的某一个值
# lindex命令,输出列表student中的第一个值
127.0.0.1:6379> TYPE student
list
127.0.0.1:6379> LRANGE student 0 -1
1) "lili"
2) "lucy"
3) "tom"
4) "bob"
127.0.0.1:6379> LINDEX student 0
"lili"
# lindex命令,输出列表student中的倒数第一个值
127.0.0.1:6379> LINDEX student -1
"bob"

# lindex命令,输出列表student中的倒数第二个值
127.0.0.1:6379> LINDEX student -2
"tom"

# lset   key  index  value	将key中index位置的值修改为value
# lset 修改列表中具体的某一个值
# 修改列表student的第一个值为 AAA
127.0.0.1:6379> LRANGE student 0 -1
1) "lili"
2) "lucy"
3) "tom"
4) "bob"
127.0.0.1:6379> LSET student 0 AAA
OK
127.0.0.1:6379> LRANGE student 0 -1
1) "AAA"
2) "lucy"
3) "tom"
4) "bob"


# 修改列表student的最后一个值为 CCC
127.0.0.1:6379> lset student -1  CCC
OK
127.0.0.1:6379> LRANGE student 0 -1
1) "AAA"
2) "lucy"
3) "tom"
4) "CCC"

# rpush 在列表的最后插入值
127.0.0.1:6379> LRANGE student 0 -1
1) "AAA"
2) "lucy"
3) "tom"
4) "CCC"

# 在列表student的最后插入值
127.0.0.1:6379> RPUSH student FFF ZZZ
(integer) 6
127.0.0.1:6379> LRANGE student 0 -1
1) "AAA"
2) "lucy"
3) "tom"
4) "CCC"
5) "FFF"
6) "ZZZ"


# lpush命令,在列表student的开头插入值
127.0.0.1:6379> LPUSH student iii kkk
(integer) 8
127.0.0.1:6379> LRANGE student 0 -1
1) "kkk"
2) "iii"
3) "AAA"
4) "lucy"
5) "tom"
6) "CCC"
7) "FFF"
8) "ZZZ"

# rpop 每次都删除列表最后的值
127.0.0.1:6379> LRANGE student 0 -1
1) "kkk"
2) "iii"
3) "AAA"
4) "lucy"
5) "tom"
6) "CCC"
7) "FFF"
8) "ZZZ"
127.0.0.1:6379> RPOP student
"ZZZ"
127.0.0.1:6379> LRANGE student 0 -1
1) "kkk"
2) "iii"
3) "AAA"
4) "lucy"
5) "tom"
6) "CCC"
7) "FFF"


# 删除列表student的开头的值,并返回被删除的值
127.0.0.1:6379> LPOP student
"kkk"
127.0.0.1:6379> LRANGE student 0 -1
1) "iii"
2) "AAA"
3) "lucy"
4) "tom"
5) "CCC"
6) "FFF"

二、数据类型之hash

hash简介

Redis hash
是一个string类型的 field(列) 和 value(值) 的映射表,一个key可对应多个field,一个field对应一个value,将一个对象存储为 hash 类型,较于每个字段都存储成 string 类型更能节省内存

举例说明,hash类型存储数据比字符更节省空间

需要同时定义多个变量,存储不同的信息 ,字符类型存储书本的信息:
书名 xxxxxx
作者 xxxxxx
价格 xxxxxx
版本 xxxxxx
出版社 xxxxxx

只需定义一个变量,这个变量中可以存储多个字段,不同字段存储不同的值
hash类型存储书本的信息:
书名 红楼梦
作者 xxxxxx
价格 xxxxxx
版本 xxxxxx
出版社 xxxxxx

hash操作

# hset   key   field  value	将hash表中field 值设置为 value
# 创建变量site,第一个字段为baidu, 字段值为 www.baidu.com
127.0.0.1:6379> HSET site baidu www.baidu.com
(integer) 1
127.0.0.1:6379> TYPE site
hash


# hget   key  filed	获取 hash 表中 field 的值
# hget命令,查看变量site中,第一个字段baidu的值
127.0.0.1:6379> HGET site baidu
"www.baidu.com"

# hmset同时定义多个字段
# 在变量site中,再添加一个字段jd,字段值为www.jd.com
127.0.0.1:6379> HMSET site jd www.jd.com
OK
# 在变量site中,同时添加多个字段tmall, sina, jm
127.0.0.1:6379> hmset site tmall www.tmall.com sina www.sina.com  jm  www.jm.com
OK

# hmget 同时获取多个hash类型的变量的值
127.0.0.1:6379> HMGET site jd baidu jm tmall sina
1) "www.jd.com"
2) "www.baidu.com"
3) "www.jm.com"
4) "www.tmall.com"
5) "www.sina.com"


# hkeys 获取hash类型变量的所有字段
127.0.0.1:6379> HKEYS site
1) "baidu"
2) "jd"
3) "tmall"
4) "sina"
5) "jm"

hvals 获取hash类型变量的所有字段对应的值
127.0.0.1:6379> HVALS site
1) "www.baidu.com"
2) "www.jd.com"
3) "www.tmall.com"
4) "www.sina.com"
5) "www.jm.com"


# hgetall 获取hash类型变量的所有字段和值
127.0.0.1:6379> HGETALL site
 1) "baidu"
 2) "www.baidu.com"
 3) "jd"
 4) "www.jd.com"
 5) "tmall"
 6) "www.tmall.com"
 7) "sina"
 8) "www.sina.com"
 9) "jm"
10) "www.jm.com"



# hdel 删除hash类型变量的多个字段
127.0.0.1:6379> HDEL site jd
(integer) 1
127.0.0.1:6379> HDEL site jm sina
(integer) 2

# hkeys命令,获取hash类型变量的所有字段, jm和sina字段被删除
127.0.0.1:6379> HKEYS site
1) "baidu"
2) "tmall"