Redis 命令 和 数据类型 您知道多少

文章目录

如果您对Redis的了解不深入请关注本栏目,本栏目包括Redis安装Redis配置文件说明

一、概述

  • Redis(Remote Dictionary Server)是一种高性能的开源内存数据库,它具有多种用途和功能,可以充当缓存、消息队列、数据库、实时分析和数据处理平台等多种角色。
  • Redis命令十分丰富,包括的命令组有 Generic、Cluster、Connection、Geo、Hashes、HyperLogLog、Keys、Lists、Pub/Sub、Scripting、Server、Sets、Sorted Sets、Strings、Transactions一共15个redis命令组两百多个redis命令。命令不区分大小写。
  • Redis数据是二进制安全的,主要类型有:
    • 字符串类型(string):是最基本的数据类型,可以存储任何类型的字符串数据,同时还支持位图操作
    • 链表类型(list):链表类型是一个双向链表,可以在链表的两端进行插入和删除操作。
    • 哈希类型(hash):哈希类型存储了字段(field)和值(value)之间的映射关系。适用于存储和操作具有结构化数据的对象,如用户信息或对象属性。
    • 无序集合(set):无序集合是一组唯一的元素的集合,其中元素之间没有特定的顺序。他使用哈希表实现,可以高效地添加、删除和查找元素。适用于需要存储和操作唯一元素集合的场景。
    • 有序集合(sorted_set):有序集合是一组唯一的元素的集合,每个元素都关联一个分数(score)。根据元素的分数进行排序,并且可以通过分数范围或排名来进行元素的查询和获取。适用于需要按照某个顺序存储和检索元素的场景,如排行榜、优先级队列等。

在这里插入图片描述

二、Redis 命令行客户端连接 Redis 服务器

  • 使用 redis-cli 登录 Redis 服务器,如下,登录本地Redis服务默认6379端口
redis-cli
  • redis-cli 登录 Redis 服务器还有很多参数可以指定,如下
# 指定主机
redis-cli -h 127.0.0.1
# 指定端口
redis-cli -p 7379
# 指定库(在Redis中有默认有16个库,对应库序号为0~15,不指定默认是0)
redis-cli -n 8
# 使 redis-cli 支持编码级格式化
redis-cli --raw
  • 使用帮助文档查看 redis-cli 各参数的作用
redis-cli -h

[root@yiqifu-redis ~]# redis-cli -h
redis-cli 6.0.6

Usage: redis-cli [OPTIONS] [cmd [arg [arg …]]]
-h Server hostname (default: 127.0.0.1).
-p Server port (default: 6379).
-s Server socket (overrides hostname and port).
-a Password to use when connecting to the server.
You can also use the REDISCLI_AUTH environment
variable to pass this password more safely
(if both are used, this argument takes predecence).
–user Used to send ACL style ‘AUTH username pass’. Needs -a.
–pass Alias of -a for consistency with the new --user option.
–askpass Force user to input password with mask from STDIN.
If this argument is used, ‘-a’ and REDISCLI_AUTH
environment variable will be ignored.
-u Server URI.
-r Execute specified command N times.
-i When -r is used, waits seconds per command.
It is possible to specify sub-second times like -i 0.1.
-n Database number.
-3 Start session in RESP3 protocol mode.
-x Read last argument from STDIN.
-d Multi-bulk delimiter in for raw formatting (default: \n) .
-c Enable cluster mode (follow -ASK and -MOVED redirections ).
–raw Use raw formatting for replies (default when STDOUT is

三、在 Redis 帮助命令的说明

  • 使用 redis-cli 登录 Redis服务器后,输入 help 可以看命令帮助介绍。

[root@yiqifu-redis ~]# redis-cli
127.0.0.1:6379> help
redis-cli 6.0.6
To get help about Redis commands type:
“help @” to get a list of commands in
“help ” for help on
“help ” to get a list of possible help topics
“quit” to exit

To set redis-cli preferences:
“:set hints” enable online hints
“:set nohints” disable online hints
Set your preferences in ~/.redisclirc

  • 从上面可以看出Redis帮助分为两种,分别是

    • 命令组帮助 help @
    • 命令帮助 help
  • 命令组有15种:包括了主要命令和数据类型等命令分组。

    @generic,@string,@list,@set,@sorted_set,@hash,@pubsub, @transactions,@connection,@server,@scripting,@hyperloglog,@cluster,@geo, @stream

  • 有关Redis命令帮助也可以看这里:http://redis.cn/commands.html

四、Redis 通用命令 @generic

4.1 通用命令说明

  • 通用命令有很多,常用的如 keys、type、object、expire、expireat、ttl。

4.1 keys 命令,列举出当前库的所有键

  • 如下列出所有Key

127.0.0.1:6379> keys *

  1. “aaa”
  2. “bbb”

4.2 type 命令,可以查看键对应值的类型

  • 如下查看aaa的值类型

    127.0.0.1:6379> type aaa
    string

4.3 object encoding 命令,查看key对应值的编码类型

  • 如下查看 ccc 的编码类型

    127.0.0.1:6379> object encoding ccc
    “raw”

    127.0.0.1:6379> set xxx 100
    OK
    127.0.0.1:6379> object encoding xxx
    “int”

4.4 expire 命令,设置过期时间

  • 如下设置bbb 的过期时间为20秒。

    127.0.0.1:6379> EXPIRE bbb 20
    (integer) 1

4.5 expireat 命令,设置过期时间戳

  • 如下,设置bbb 的过期时间为指定时间戳1697014508。

    127.0.0.1:6379> EXPIREAT bbb 1697014508
    (integer) 1

4.6 ttl 命令,查看键还有多久过期

  • 过期时间说明

    • -1 是没有指定过期时间
    • -2 表示已过期
    • 正数表示还剩多少秒过期
  • 如下查看 aaa 的过期时间

    127.0.0.1:6379> ttl aaa
    (integer) -1

4.7 通用命令帮助

  • 通用命令在 @generic 组中,使用帮助命令查看 @generic 组命令使用方法

127.0.0.1:6379> help @generic

DEL key [key …]
summary: Delete a key
since: 1.0.0

DUMP key
summary: Return a serialized version of the value stored at the specified k ey.
since: 2.6.0

EXISTS key [key …]
summary: Determine if a key exists
since: 1.0.0

EXPIRE key seconds
summary: Set a key’s time to live in seconds
since: 1.0.0

EXPIREAT key timestamp
summary: Set the expiration for a key as a UNIX timestamp
since: 1.2.0

KEYS pattern
summary: Find all keys matching the given pattern
since: 1.0.0

五、Redis 数据类型一:字符串数据类型 @string

5.1 类型说明

  • Redis 字符串数据类型用于存放字符串数据。字符串类的功能十强大,具体表现为:
    • 实现锁机制。
    • 可以存放文本内容,并可对内容进行截取、统计等操作。
    • 也可以存放数字,并参与加减等计算。如应用到秒杀、抢购方面十分方便。
    • 还可以按位操作,并参与安位计算和统计功能。

5.2 set 命令,添加一个字符串值到Redis数据库中

  • 如下添加key为aaa,值111的数据

127.0.0.1:6379> set aaa 111
OK

  • set 命令设置过期时间参数 ex,如下,设置bbb过期时间为20秒。

    127.0.0.1:6379> set bbb 222 ex 20
    OK
    127.0.0.1:6379> ttl bbb
    (integer) 15

  • set 命令限制只能新建参数 nx,如下,第二次添加nx参数则添加失败。这种机制可实现锁。

    127.0.0.1:6379> set aaa 111
    OK
    127.0.0.1:6379> set aaa 111 nx
    (nil)

  • set 命令限制只能更新参数 xx,如下,在没有key为ccc的情况下使用 xx 参数添加失败

    127.0.0.1:6379> set ccc 333 xx
    (nil)

5.3 get 命令,从Redis数据库中获取键对应的值

  • 如下获取key为aaa的值

127.0.0.1:6379> get aaa
“111”

5.4 mset 命令,一次添加多个键值

  • 如下一次添加两个键值aaa=111, bbb=222

    127.0.0.1:6379> mset aaa 111, bbb 222
    OK
    127.0.0.1:6379> keys *
    1) “aaa”
    2) “bbb”

5.5 mget 命令,一次读取多个键值

  • 如下一次读取两个键值 aaa, bbb

    127.0.0.1:6379> mget aaa bbb
    1) “111,”
    2) “222”

5.6 msetnx 命令,一次添加多个键值2

  • 与mset命令相同,只是只允许新建,这种方式可以实现锁机制

127.0.0.1:6379> msetnx mmm 111, nnn 222
(integer) 1
127.0.0.1:6379> msetnx mmm 333, nnn 444
(integer) 0
127.0.0.1:6379> mget mmm nnn
1) “111,”
2) “222”

5.7 append 命令,追加内容

  • 如下向原键ccc添加新内容 world

    127.0.0.1:6379> set ccc hello
    OK
    127.0.0.1:6379> append ccc world
    (integer) 10
    127.0.0.1:6379> get ccc
    “helloworld”

5.8 setrange 命令,从指定偏移位置开始覆盖字符串

  • 如下把键ccc对应的值从位置5开始覆盖为内容yiqifu

    127.0.0.1:6379> setrange ccc 5 yiqifu
    (integer) 11
    127.0.0.1:6379> get ccc
    “helloyiqifu”

5.9 getrange 命令,截取字符串

  • 索引位置说明

    • 索引位置可以是正数,也可以是正数
    • 正数是0开始,表示从字符串开头开始
    • 负数从-1开始,表示从字符串结尾开始
  • 如下从键ccc的值中开始截取内容。

    127.0.0.1:6379> getrange ccc 5 9
    “world”
    127.0.0.1:6379> getrange ccc 5 -1
    “world”

5.10 strlen 命令,查询字符串值长度

  • 如下查询键为ccc的字符串长度

    127.0.0.1:6379> strlen ccc
    (integer) 11

5.11 incr/decr 命令,对于数据是数字类型的自增、自减命令

  • 如下自增、自增操作

    127.0.0.1:6379> set ddd 10
    OK
    127.0.0.1:6379> incr ddd
    (integer) 11
    127.0.0.1:6379> get ddd
    “11”
    127.0.0.1:6379> decr ddd
    (integer) 10
    127.0.0.1:6379> get ddd
    “10”

5.12 incrby/decrby/incrbyfloat 命令,对于数据是数字类型的加、减操作

  • 如下自增、自减操作

    127.0.0.1:6379> set ddd 10
    OK
    127.0.0.1:6379> incrby ddd 5
    (integer) 15
    127.0.0.1:6379> get ddd
    “15”
    127.0.0.1:6379> decrby ddd 5
    (integer) 10
    127.0.0.1:6379> get ddd
    “10”
    127.0.0.1:6379> incrbyfloat ddd 0.1
    “10.1”
    127.0.0.1:6379> get ddd
    “10.1”

5.13 getset 命令,给指定键设置一个新值,并返回旧值

  • 如下对键eee设置222,会返回旧值111

    127.0.0.1:6379> set eee 111
    OK
    127.0.0.1:6379> get eee
    “111”
    127.0.0.1:6379> getset eee 222
    “111”
    127.0.0.1:6379> get eee
    “222”

5.14 setbit 命令,按位设置值,位的下标从0开始

  • 如下将第一个字节的第6位设置为1,二进制表示为:00000100

    127.0.0.1:6379> setbit aaa 5 1
    (integer) 0
    127.0.0.1:6379> get aaa
    “\x04”

5.15 bitpos 命令,按位查找值。

  • 如下从第0个字节开始到第二个字节结束,按位查找第一次出现1的位置(注意是出现1这个bit的位置)

    127.0.0.1:6379> bitpos aaa 1 0 2
    (integer) 5

5.16 bitcount 命令,按位统计

  • 如下从第0个字节开始到第二个字节结束,按位统计出现1的次数。

    127.0.0.1:6379> bitcount aaa 0 2
    (integer) 1

    • 结束位置可以使用负数,如-1表示最后一个字节,-2表示倒数第二个字节,以此类推。

5.17 bitop 命令,按位操作

  • 如下将key为aaa和bbb的值。按位与操作,并存放在ccc中;按位或操作,并存放在ddd中。

    127.0.0.1:6379> setbit aaa 6 1
    (integer) 0
    127.0.0.1:6379> setbit bbb 7 1
    (integer) 0
    127.0.0.1:6379> bitop and ccc aaa bbb
    (integer) 1
    127.0.0.1:6379> get ccc
    “\x00”

    127.0.0.1:6379> bitop or ddd aaa bbb
    (integer) 1
    127.0.0.1:6379> get ddd
    “\x03”

5.18 命令帮助

  • 有关更多字符串类型数据的操作命令,请查看帮助组 @string

127.0.0.1:6379> help @string

APPEND key value
summary: Append a value to a key
since: 2.0.0

BITCOUNT key [start end]
summary: Count set bits in a string
since: 2.6.0

BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
summary: Perform arbitrary bitfield integer operations on strings
since: 3.2.0

BITOP operation destkey key [key …]
summary: Perform bitwise operations between strings
since: 2.6.0

BITPOS key bit [start] [end]
summary: Find first bit set or clear in a string
since: 2.8.7

DECR key
summary: Decrement the integer value of a key by one
since: 1.0.0

DECRBY key decrement
summary: Decrement the integer value of a key by the given number
since: 1.0.0

GET key
summary: Get the value of a key
since: 1.0.0

六、Redis 数据类型二:链表数据类型 @list

6.1 类型说明

  • Redis 链表数据类型用于存放列表型数据
    • 可以从链表的左侧添加数据
    • 可以从链表右侧添加数据。
    • 链表可以存储重复且有序的数据
    • 可以当作数据来使用
    • 可以当作栈来使用
    • 可以当作队列来使用
    • 支持阻塞队列/单播队列

6.2 lpush 命令,从链表左侧添加列表元素

  • 如添加一个key为aaa,值为列表的111,222,333,444,555的元素

127.0.0.1:6379> lpush aaa 111 222 333 444 555
(integer) 5

  • 注意:测试时如果操作过程提示值类型错误,可以先把数据库清空,如下:

    127.0.0.1:6379> lpush aaa 111 222 333 444 555
    (error) WRONGTYPE Operation against a key holding the wrong kind of value
    127.0.0.1:6379> flushdb
    OK

6.3 rpush 命令,从链表右侧添加列表元素

  • 如添加一个key为bbb,值为列表的111,222,333,444,555的元素

127.0.0.1:6379> rpush bbb 111 222 333 444 555
(integer) 5

6.4 lpop 命令,从链表左侧弹出一个元素

  • 如弹出链表aaa的元素3次,和之前lpush对比,会发现这类似一种栈的结构。

127.0.0.1:6379> lpop aaa
“555”
127.0.0.1:6379> lpop aaa
“444”
127.0.0.1:6379> lpop aaa
“333”

6.5 rpop 命令,从链表右侧弹出一个元素

  • 如弹出链表bbb的元素3次,和之前rpush对比,会发现这类似一种栈的结构。

127.0.0.1:6379> rpop bbb
“555”
127.0.0.1:6379> rpop bbb
“444”
127.0.0.1:6379> rpop bbb
“333”

6.6 lrange 命令,从链表取出指定元素

  • 如取出链表aaa的所有元素。
    • Redis支持正向索引和逆向索引
    • 正向索引从0开始表示第一个,并依次向后增加
    • 负向索引从-1表始表示最后一个,并依次向前减少

127.0.0.1:6379> lpush aaa 111 222 333 444 555
(integer) 7
127.0.0.1:6379>
127.0.0.1:6379> lrange aaa 0 -1
1) “555”
2) “444”
3) “333”
4) “222”
5) “111”
6) “222”
7) “111”
127.0.0.1:6379> lrange bbb 0 -1
1) “111”
2) “222”

6.7 lindex 命令,从链表索引位置取一个元素

  • 如在取出链表第3个元素的值。

127.0.0.1:6379> lindex aaa 3
“222”

6.8 lset 命令,修改链表索引位置的元素值

  • 如修改链表第3个元素的值为xxx。

127.0.0.1:6379> lset aaa 3 xxxx
OK
127.0.0.1:6379> lindex aaa 3
“xxxx”

6.9 lrem 命令,删除链表中指定个数的元素

  • 如删除链表第中从左往右1个111元素。
    • 正数是从前往后
    • 负数是从后往前

127.0.0.1:6379> lrem aaa 1 111
(integer) 1
127.0.0.1:6379> lrange aaa 0 -1
1) “555”
2) “444”
3) “333”
4) “xxxx”
5) “222”
6) “111”

6.10 linsert 命令,向链表中插入一个元素

  • 如在链表的555元素前添加一个777,并在777元素后添加一个666。

127.0.0.1:6379> linsert aaa before 555 777
(integer) 7
127.0.0.1:6379> linsert aaa after 777 666
(integer) 8
127.0.0.1:6379> lrange aaa 0 4
1) “777”
2) “666”
3) “555”
4) “444”
5) “333”

6.11 llen 命令,统计链表元素个数

  • 如统计链表aaa的元素个数。

127.0.0.1:6379> llen aaa
(integer) 8

6.12 blpop 命令,以阻塞的方式从链表左侧弹出一个元素

  • 如以阻塞的方式从链表左侧ccc取一个元素,阻塞时间为5秒
    • 如果时间填0则表示一直等待,只到客户端向链表ccc添加一个元素。

127.0.0.1:6379> blpop ccc 5
(nil)
(5.04s)

6.13 brpop 命令,以阻塞的方式从链表右侧弹出一个元素

  • 如以阻塞的方式从链表右侧ccc取一个元素,阻塞时间为5秒
    • 如果时间填0则表示一直等待,只到客户端向链表ccc添加一个元素。

127.0.0.1:6379> brpop ccc 5
(nil)
(5.09s)

6.14 ltrim 命令,从链表两端删除指定元素

  • 如从链表左边删除2个元素,从链表右边删除3个元素。

127.0.0.1:6379> lrange aaa 0 -1
1) “777”
2) “666”
3) “555”
4) “444”
5) “333”
6) “xxxx”
7) “222”
8) “111”
127.0.0.1:6379> ltrim aaa 2 -3
OK
127.0.0.1:6379> lrange aaa 0 -1
1) “555”
2) “444”
3) “333”
4) “xxxx”

6.15 帮助命令

  • 有关更多链表类型数据的操作命令,请查看帮助组 @list

    127.0.0.1:6379> help @list

    BLPOP key [key …] timeout
    summary: Remove and get the first element in a list, or block until one is available
    since: 2.0.0

    BRPOP key [key …] timeout
    summary: Remove and get the last element in a list, or block until one is a vailable
    since: 2.0.0

    BRPOPLPUSH source destination timeout
    summary: Pop an element from a list, push it to another list and return it; or block until one is available
    since: 2.2.0

    LINDEX key index
    summary: Get an element from a list by its index
    since: 1.0.0

七、Redis 数据类型三:哈希数据类型 @hash

7.1 类型说明

  • Redis 哈希数据类型用于存放字典(对象)型数据
    • 哈希类型存储了字段(field)和值(value)之间的映射关系。
    • 适用于存储和操作具有结构化数据的对象,如用户信息或对象属性。

7.2 hset 命令,在哈希中添加元素

  • 如添加一个哈希person1
    • 属性名name,值lisi
    • 属性名age,值21

127.0.0.1:6379> hset person1 name zhangsan
(integer) 1
127.0.0.1:6379> hset person1 age 20
(integer) 1

  • 假如没有哈希,我们只能通过字符串来保存,如下

    127.0.0.1:6379> set person1::name zhangsan
    OK
    127.0.0.1:6379> set person1::age 20
    OK

    127.0.0.1:6379> keys person1*
    1) “person1::age”
    2) “person1::name”

7.3 hmset 命令,在哈希中一次添加多个元素

  • 如添加一个哈希person2
    • 属性名name,值lisi
    • 属性名age,值21

127.0.0.1:6379> hmset person2 name lisi age 21
OK
127.0.0.1:6379> keys person2
1) “person2”

7.4 hget 命令,从哈希中取出一个属性值

  • 如取出哈希person1中属性值。

127.0.0.1:6379> hget person1 name
“zhangsan”
127.0.0.1:6379> hget person1 age
“20”

7.5 hmget 命令,从哈希中取出多个属性值

  • 如取出哈希person1中属性值。

127.0.0.1:6379> hmget person1 name age
1) “zhangsan”
2) “20”

7.6 hkeys 命令,取出哈希中所有键

  • 如取出哈希person1中所有key。

127.0.0.1:6379> hkeys person1

1) “name”
2) “age”

7.7 hvals 命令,取出哈希中所有值

  • 如取出哈希person1中所有值。

127.0.0.1:6379> hvals person1
1) “zhangsan”
2) “20”

7.8 hgetall 命令,取出哈希中所有键值

  • 如取出哈希person1中所有键值。

127.0.0.1:6379> hgetall person1

  1. “name”
  2. “zhangsan”
  3. “age”
  4. “20”

7.9 hincrby 命令,对哈希元素进行整数加减运算

  • 如对哈希person1中的age增减1。

127.0.0.1:6379> hincrby person1 age -1
“19”
127.0.0.1:6379> hget person1 age
“19”

7.10 hincrbyfloat 命令,对哈希元素进行浮点数加减运算

  • 如对哈希person1中的age增加0.5。

127.0.0.1:6379> hincrbyfloat person1 age 0.5
“20.5”
127.0.0.1:6379> hget person1 age
“20.5”

7.11 帮助命令

  • 有关更多哈希类型数据的操作命令,请查看帮助组 @hash

    127.0.0.1:6379> help @hash

    HDEL key field [field …]
    summary: Delete one or more hash fields
    since: 2.0.0

    HEXISTS key field
    summary: Determine if a hash field exists
    since: 2.0.0

    HGET key field
    summary: Get the value of a hash field
    since: 2.0.0

    HGETALL key
    summary: Get all the fields and values in a hash
    since: 2.0.0

    HINCRBY key field increment
    summary: Increment the integer value of a hash field by the given number
    since: 2.0.0

八、Redis 数据类型四:集合数据类型 @set

8.1 类型说明

  • Redis 集合数据类型用于存放列表型数据
    • 集合存放无序且唯一的数据(与链表对比)
    • 元素之间没有特定的顺序。
    • 使用哈希表实现,可以高效地添加、删除和查找元素。
    • 适用于需要存储和操作唯一元素集合的场景。

8.2 sadd 命令,在集合中添加元素

  • 如添加一个集合aaa,值为 11,22,33,44,55,11,22
    • 注意:他会对结果去重

127.0.0.1:6379> sadd aaa 11 22 33 44 55 11 22
(integer) 5
127.0.0.1:6379> smembers aaa
1) “11”
2) “22”
3) “33”
4) “44”
5) “55”

8.3 smembers 命令,在查看集合元素

  • 如查看集合aaa的元素

127.0.0.1:6379> smembers aaa
1) “11”
2) “22”
3) “33”
4) “44”
5) “55”

8.4 srem 命令,从集合中删除元素

  • 如删除集合aaa元素 33、44

127.0.0.1:6379> srem aaa 33 44
(integer) 2
127.0.0.1:6379> smembers aaa
5) “11”
6) “22”
7) “55”

8.5 sinter 命令,对两个集合进行交集运算

  • 如对集合aaa和集合bbb进行交集运算。

127.0.0.1:6379> sadd bbb 11 22 66 77
(integer) 4
127.0.0.1:6379> sinter aaa bbb
1) “11”
2) “22”

8.6 sinterstore 命令,对两个集合进行交集运算2

  • 对两个集合进行交集运算,并将结果存储在一个另一个集合中。

  • 如对集合aaa和集合bbb进行交集运算,并将结果存储在ccc中。

127.0.0.1:6379> SINTERSTORE ccc aaa bbb
(integer) 2
127.0.0.1:6379> SMEMBERS ccc
1) “11”
2) “22”

8.7 sunion 命令,对两个集合进行并集运算

  • 如对集合aaa和集合bbb进行并集运算。

127.0.0.1:6379> sunion aaa bbb
1) “11”
2) “22”
3) “55”
4) “66”
5) “77”

8.8 sunionstore 命令,对两个集合进行并集运算2

  • 对两个集合进行并集运算,并将结果存储在一个另一个集合中。
  • 如对集合aaa和集合bbb进行并集运算,并将结果存储在ddd中。

127.0.0.1:6379> sunionstore ddd aaa bbb
(integer) 5
127.0.0.1:6379> smembers ddd
1) “11”
2) “22”
3) “55”
4) “66”
5) “77”

8.9 sdiff 命令,对两个集合进行差集运算

  • 注意差集是有方向的
  • 如对集合aaa和集合bbb进行差集运算。

127.0.0.1:6379> sdiff aaa bbb
1) “55”
127.0.0.1:6379> sdiff bbb aaa
1) “66”
2) “77”

8.10 sdiffstore 命令,对两个集合进行差集运算2

  • 对两个集合进行差集运算,并将结果存储在一个另一个集合中。

    • 注意差集是有方向的
  • 如对集合aaa和集合bbb进行差集运算,并将结果存储在eee中。

127.0.0.1:6379> sdiffstore eee bbb aaa
(integer) 2
127.0.0.1:6379> smembers eee
1) “66”
2) “77”

8.11 srandmembers 命令,随机取出集合中指定数量的元素

  • 作用:抽奖系统
  • 元素个数可以是正数,也可以是负数
    • 正数:返回结果集不会超过集合长度(不会重复)
    • 负数:返回结果集就是您指定的元素个数(会有重复的)

127.0.0.1:6379> srandmember aaa 2
1) “22”
2) “11”
127.0.0.1:6379> srandmember aaa 10
1) “11”
2) “22”
3) “55”
127.0.0.1:6379> srandmember aaa -4
1) “55”
2) “55”
3) “22”
4) “55”

8.12 spop 命令,随机取出集合中一个元素

  • 作用:抽奖系统
  • 随机取出集合中一个元素,直到集合中没有元素为止

127.0.0.1:6379> spop aaa
“22”
127.0.0.1:6379> spop aaa
“11”
127.0.0.1:6379> spop aaa
“55”
127.0.0.1:6379> spop aaa
(nil)

8.13 帮助命令

  • 有关更多集合类型数据的操作命令,请查看帮助组 @set

    127.0.0.1:6379> help @set

    SADD key member [member …]
    summary: Add one or more members to a set
    since: 1.0.0

    SCARD key
    summary: Get the number of members in a set
    since: 1.0.0

    SDIFF key [key …]
    summary: Subtract multiple sets
    since: 1.0.0

    SDIFFSTORE destination key [key …]
    summary: Subtract multiple sets and store the resulting set in a key
    since: 1.0.0

    SINTER key [key …]
    summary: Intersect multiple sets
    since: 1.0.0

    SINTERSTORE destination key [key …]
    summary: Intersect multiple sets and store the resulting set in a key
    since: 1.0.0

九、Redis 数据类型五:有序集合数据类型 @sorted_set

9.1 类型说明

  • Redis 有序集合数据类型用于存放有序列表型数据
    • 存放有序且唯一的数据(与集合对比)
  • 主要用途如下:
    • 排行榜:可以使用有序集合存储用户的分数或排名信息,并根据分数进行排序,以便实现排行榜功能。
    • 带权重的优先级队列:可以使用有序集合存储带有权重的任务,根据权重进行排序,以便实现优先级队列。
    • 范围查询:有序集合提供了按照分数范围进行元素查询的功能,可以方便地获取特定范围内的元素。
    • 去重和统计:有序集合的元素是唯一的,可以用于去重操作,并且可以根据分数进行统计和计数。

9.2 zadd 命令,在有序集合中添加元素

  • 如添加一个集合aaa,值为 zhangsan, lisi, wangwu ,排序分值为 3,1,2
    • 注意:他会按排序分值进行排序存储

127.0.0.1:6379> zadd aaa 3 zhangsan 1 lisi 2 wangwu
(integer) 3
127.0.0.1:6379> zrange aaa 0 -1
1) “lisi”
2) “wangwu”
3) “zhangsan”

9.3 zrane 命令,取出有序集合元素

  • 查看集合元素的同时要查看分值请加 withscores 参数

  • 如查看集合aaa的元素

127.0.0.1:6379> zrange aaa 0 -1
1) “lisi”
2) “wangwu”
3) “zhangsan”
127.0.0.1:6379> zrange aaa 0 -1 withscores
1) “lisi”
2) “1”
3) “wangwu”
4) “2”
5) “zhangsan”
6) “3”

9.4 zrevrange 命令,出取有序集合元素,由高到低

  • zrange 是正序取集合元素。zrevrange 是倒序取集事元素。
  • 如查看集合aaa的元素

127.0.0.1:6379> zrevrange aaa 0 -1
1) “zhangsan”
2) “wangwu”
3) “lisi”

9.5 zrangebyscore 命令,根据分值查看有序集合元素

  • 根据分值查看有序集合元素时,要指定分值范围
  • 如查看集合aaa的元素

127.0.0.1:6379> zrangebyscore aaa 2 3
1) “wangwu”
2) "zhangsan

9.6 zscore 命令,根据元素从有序集合中取出分值

  • 如查看 lisi 的分值

127.0.0.1:6379> zscore aaa lisi
“1”

9.7 zrank 命令,根据元素从有序集合中取出排名

  • 如查看 lisi 的排名

127.0.0.1:6379> zrank aaa lisi
(integer) 0

9.8 zincrby 命令,对有序集合的分值做加减运算

  • 应用:如歌曲排行榜

  • 如对 wangwu 的分值加2

127.0.0.1:6379> zrange aaa 0 -1 withscores
1) “lisi”
2) “1”
3) “wangwu”
4) “2”
5) “zhangsan”
6) “3”
127.0.0.1:6379> zincrby aaa 2 wangwu
“4”
127.0.0.1:6379> zrange aaa 0 -1 withscores
1) “lisi”
2) “1”
3) “zhangsan”
4) “3”
5) “wangwu”
6) “4”

9.9 zinterstore 命令,对两个有序集合进行交集运算

  • 对两个有序集合进行交集运算,并将结果存储在一个另一个集合中。
  • 如对集合aaa和集合bbb进行并集运算,并将结果存储在ccc中。

127.0.0.1:6379> zadd bbb 10 xiaoming 20 xiaozhang 30 lisi
(integer) 1
127.0.0.1:6379> zinterstore ccc 2 aaa bbb
(integer) 1
127.0.0.1:6379> zrange ccc 0 -1 withscores
1) “lisi”
2) “31”

127.0.0.1:6379> zinterstore ccc 2 aaa bbb weights 1 0.2
(integer) 1
127.0.0.1:6379> zrange ccc 0 -1
1) “lisi”
127.0.0.1:6379> zrange ccc 0 -1 withscores
1) “lisi”
2) “7”

127.0.0.1:6379> zinterstore ccc 2 aaa bbb weights 1 0.2 aggregate max
(integer) 1
127.0.0.1:6379> zrange ccc 0 -1 withscores
1) “lisi”
2) “6”

  • zinterstore ccc 2 aaa bbb weights 1 0.2 aggregate max 命令解析
    • zinterstore 求集合交集命令
    • ccc 存放交集结果的名称
    • 2 求2个集合的交集
    • aaa 求交集的第一个集合
    • bbb 求交集的第二个集合
    • weights 1 0.2 求交集时,对于分数的权重指定,这里对aaa集合分数的权重为1,对bbb集合分数的权重为0.2
    • aggregate max 分数聚合方式法,这里是取最大值,可以使用 min, max, sum

9.10 zunionstore 命令,对两个有序集合进行并集运算

  • 对两个有序集合进行并集运算,并将结果存储在一个另一个集合中。
  • 如对集合aaa和集合bbb进行并集运算,并将结果存储在ddd中。

127.0.0.1:6379> zunionstore ddd 2 aaa bbb
(integer) 6
127.0.0.1:6379> zrange ddd 0 -1
1) “zhangsan”
2) “wangwu”
3) “xiaoming”
4) “xiaozhang”
5) “xiaoliu”
6) “lisi”

9.11 帮助命令

  • 有关更多链表类型数据的操作命令,请查看帮助组 @sorted_set

    127.0.0.1:6379> help @sorted_set

    BZPOPMAX key [key …] timeout
    summary: Remove and return the member with the highest score from one or more sorted sets, or block until one is available
    since: 5.0.0

    BZPOPMIN key [key …] timeout
    summary: Remove and return the member with the lowest score from one or more sorted sets, or block until one is available
    since: 5.0.0

    ZADD key [NX|XX] [CH] [INCR] score member [score member …]
    summary: Add one or more members to a sorted set, or update its score if it already exists
    since: 1.2.0

    ZCARD key
    summary: Get the number of members in a sorted set
    since: 1.2.0

    ZCOUNT key min max
    summary: Count the members in a sorted set with scores within the given values
    since: 2.0.0

十、Redis 连接命令 @connection

10.1 select命令,切换 Redis 库

  • Redis 默认库是0,总共有16个,select 命令参数的取值范围为0~15
  • 如下切换到库2

127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]>

10.2 帮助命令

  • 连接命令在 @connection组中,使用帮助命令查看 @connection组命令使用方法

127.0.0.1:6379> help @connection

AUTH password
summary: Authenticate to the server
since: 1.0.0

CLIENT CACHING YES|NO
summary: Instruct the server about tracking or not keys in the next reques t
since: 6.0.0

CLIENT GETNAME -
summary: Get the current connection name
since: 2.6.9

CLIENT GETREDIR -
summary: Get tracking notifications redirection client ID if any
since: 6.0.0

CLIENT ID -
summary: Returns the client ID for the current connection
since: 5.0.0

CLIENT KILL [ip:port] [ID client-id] [TYPE normal|master|slave|pubsub] [US ER username] [ADDR ip:port] [SKIPME yes/no]
summary: Kill the connection of a client
since: 2.4.0

CLIENT LIST [TYPE normal|master|replica|pubsub]
summary: Get the list of client connections
since: 2.4.0

CLIENT PAUSE timeout
summary: Stop processing commands from clients for some time
since: 2.9.50

CLIENT REPLY ON|OFF|SKIP
summary: Instruct the server whether to reply to commands
since: 3.2.0

十一、Redis 管理命令 @server

11.1 config get 命令,配置管理

  • 相当于修改redis.conf配置文件

  • 如下配置protected-mode为no

    127.0.0.1:6379> CONFIG GET *
    1) “rdbchecksum”
    2) “yes”
    3) “daemonize”
    4) “no”
    5) “io-threads-do-reads”
    6) “no”
    7) “lua-replicate-commands”
    8) “yes”
    9) “always-show-logo”
    10) “yes”
    11) “protected-mode”
    12) “yes”

    127.0.0.1:6379> CONFIG SET “protected-mode” no
    OK

11.2 flushdb 命令,从当前库中删除所有键

  • Redis默认16个库,默认库是0
  • 如下这里删除当前库所有数据,但是其他库数据依然存在

127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> keys *
(empty array)

11.3 flushall 命令,从所有库中删除所有键

  • 如下删除所有库的所有数据,其他库中的数据也没有了

    127.0.0.1:6379> flushall
    OK
    127.0.0.1:6379> keys *
    (empty array)

11.4 svae 命令,以前台阻塞的方式保存快照

  • 如下以同步方式保存快照,进行持久化

127.0.0.1:6379> save
OK

11.5 bgsave 命令,以后台非阻塞的方式保存快照

  • 如下以民步方式保存快照,进行持久化

127.0.0.1:6379> bgsave
Background saving started

11.6 BGREWRITEAOF 命令,以后台非阻塞的方式重写AOF

  • 他的作用是执行一些压缩,让aof文件变小

  • 如下执行重写AOF

    127.0.0.1:6379> BGREWRITEAOF
    Background append only file rewriting started

11.7 REPLICAOF 命令,将当前主机作为从机加入Redis主机(Master)

  • 如下将当前Redis实例作为从机(Slave)加入主机(Master 127.0.0.1:6379)

    127.0.0.1:6381> REPLICAOF 127.0.0.1 6379
    OK

11.8 帮助命令

  • 管理命令在 @server 组中,使用帮助命令查看 @server 组命令使用方法

127.0.0.1:6379> help @server

ACL CAT [categoryname]
summary: List the ACL categories or the commands inside a category
since: 6.0.0

ACL DELUSER username [username …]
summary: Remove the specified ACL users and the associated rules
since: 6.0.0

ACL GENPASS [bits]
summary: Generate a pseudorandom secure password to use for ACL users
since: 6.0.0

ACL GETUSER username
summary: Get the rules for a specific ACL user
since: 6.0.0

ACL HELP -
summary: Show helpful text about the different subcommands
since: 6.0.0

ACL LIST -
summary: List the current ACL rules in ACL config file format
since: 6.0.0

ACL LOAD -
summary: Reload the ACLs from the configured ACL file
since: 6.0.0

ACL LOG [count or RESET]
summary: List latest events denied because of ACLs in place
since: 6.0.0

十二、Redis 事务 @transactions

12.1 命令作用

  • 将一组命令放在同一个事务中进行处理。

12.2 命令介绍

  • multi 开始事务
  • exec 提交事务
  • watch 为 Redis 事务提供 check-and-set (CAS)行为(被 WATCH的键会被监视,并会发觉这些键是否被改动过了。 如果有至少一个被监视的键在 EXEC 执行之前被修改了, 那么整个事务都会被取消, EXEC 返回nil-reply来表示事务已经失败。)

12.3 事务命令使用示例

  • 如下,在事务中添加 aaa, bbb ,保证两个操作在一直事务中执行的示例

127.0.0.1:6379> multi
OK
127.0.0.1:6379> set aaa 111
QUEUED
127.0.0.1:6379> set bbb 222
QUEUED
127.0.0.1:6379> exec

  1. OK
  2. OK

13.4 监控命令使用示例

  • 如果在上面的示例中添加了watch,然后另一个客户端修改了 aaa 的值,则本次事务修改 aaa 后提交会失败,如下:

127.0.0.1:6379> watch aaa
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set aaa 333
QUEUED
127.0.0.1:6379> exec
(nil)

  • 这里模拟另一个客户端修改其值

  • 127.0.0.1:6379> set aaa 444
    OK

13.5 命令帮助

  • 事务命令在 @transactions组中,使用帮助命令查看 @transactions 组命令使用方法

127.0.0.1:6379> help @transactions

DISCARD -
summary: Discard all commands issued after MULTI
since: 2.0.0

EXEC -
summary: Execute all commands issued after MULTI
since: 1.2.0

MULTI -
summary: Mark the start of a transaction block
since: 1.2.0

UNWATCH -
summary: Forget about all watched keys
since: 2.2.0

WATCH key [key …]
summary: Watch the given keys to determine execution of the MULTI/EXEC block
since: 2.2.0

十三、Redis 发布/订阅 @pubsub

13.1 订阅

  • 如下通过 subscribe 订阅xxx,当发布后收到了消息123456

127.0.0.1:6379> subscribe xxx
Reading messages… (press Ctrl-C to quit)

  1. “subscribe”
  2. “xxx”
  3. (integer) 1
  4. “message”
  5. “xxx”
  6. “123456”

13.2 发布

  • 如下通过 publish 向 xxx 发布了内容 123456

127.0.0.1:6379> publish xxx 123456
(integer) 1

13.3 帮助命令

  • 发布/订阅命令在 @pubsub组中,使用帮助命令查看 @pubsub 组命令使用方法

127.0.0.1:6379> help @pubsub

PSUBSCRIBE pattern [pattern …]
summary: Listen for messages published to channels matching the given patt erns
since: 2.0.0

PUBLISH channel message
summary: Post a message to a channel
since: 2.0.0

PUBSUB subcommand [argument [argument …]]
summary: Inspect the state of the Pub/Sub subsystem
since: 2.8.0

PUNSUBSCRIBE [pattern [pattern …]]
summary: Stop listening for messages posted to channels matching the given patterns
since: 2.0.0

SUBSCRIBE channel [channel …]
summary: Listen for messages published to the given channels
since: 2.0.0

UNSUBSCRIBE [channel [channel …]]
summary: Stop listening for messages posted to the given channels
since: 2.0.0

十四、Redis 管道(Pipelining)

  • Redis是一种基于客户端-服务端模型以及请求/响应协议的TCP服务。

  • 在Redis中,一次请求/响应服务器能实现处理新的请求即使旧的请求还未被响应。这样就可以将多个命令发送到服务器,而不用等待回复,最后在一个步骤中读取该答复。

    这就是管道(pipelining),是一种几十年来广泛使用的技术。例如许多POP3协议已经实现支持这个功能,大大加快了从服务器下载新邮件的过程。

    Redis很早就支持管道(pipelining)技术,因此无论你运行的是什么版本,你都可以使用管道(pipelining)操作Redis。

  • 下面是一个使用的例子,首先创建一个xxx=10,然后加1,最后再取值的过程。这个三个命令放在一起,使用了Redis管道。

[root@yiqifu-redis ~]# echo -e “set xxx 10\n incr xxx\n get ccc” | nc localhost 6379
+OK
:11
$1

  • 执以上命令前,如果没有安装 nc 请先安装 nc,如下
yum install nc  -y
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QIFU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值