首页 > 数据库 >007-Redis的 Set 命令

007-Redis的 Set 命令

时间:2022-10-06 17:45:11浏览次数:79  
标签:返回 set Redis 127.0 member 6379 Set 007 key

1. Set

1.1 sadd

1.1.1 基本信息

SADD key member [member ...]

summary: Add one or more members to a set

since: 1.0.0

Add the specified members to the set stored at key. Specified members that are already a member of this set are ignored. If key does not exist, a new set is created before adding the specified members.

将指定的成员添加到按键存储的集合中。已经是此集成员的指定成员将被忽略。如果键不存在,则在添加指定成员之前创建一个新集。

An error is returned when the value stored at key is not a set.

如果键上存储的值不是集合,则返回错误。

1.1.2 返回

  • 添加到集合中的元素数,不包括集合中已经存在的所有元素

1.1.3 练习

127.0.0.1:6379[1]> sadd myset a b c d e f g h i j k l m n
(integer) 14

1.2 scard

1.2.1 基本信息

SCARD key

summary: Get the number of members in a set

since: 1.0.0

Returns the set cardinality (number of elements) of the set stored at key.

返回存储在 key 处的集的元素数。

1.2.2 返回

  • 集合的基数(元素数量) ,如果键不存在,则为0

1.2.3 练习

127.0.0.1:6379[1]> scard myset
(integer) 14

1.3 sdiff

1.3.1 基本信息

SDIFF key [key ...]

summary: Subtract multiple sets

since: 1.0.0

Returns the members of the set resulting from the difference between the first set and all the successive sets.

返回由第一个集和所有后续集之间的差集。

Keys that do not exist are considered to be empty sets.

不存在的键被认为是空集。

1.3.2 返回

  • 包含结果集成员的列表

1.3.3 练习

127.0.0.1:6379[1]> sadd set2 a b d
(integer) 3
127.0.0.1:6379[1]> sadd set3 e f g
(integer) 3
127.0.0.1:6379[1]> sdiff myset set2 set3
1) "h"
2) "i"
3) "c"
4) "k"
5) "j"
6) "n"
7) "l"
8) "m"

1.4 sdiffstore

1.4.1 基本信息

SDIFFSTORE destination key [key ...]

summary: Subtract multiple sets and store the resulting set in a key

since: 1.0.0

This command is equal to SDIFF, but instead of returning the resulting set, it is stored in destination.

这个命令等于 SDIFF,但是不返回结果集,而是存储在目标中。

If destination already exists, it is overwritten.

如果目的地已经存在,则覆盖它。

1.4.2 返回

  • 结果集中的元素数

1.4.3 练习

127.0.0.1:6379[1]> sdiffstore newset myset set2 set3
(integer) 8

1.5 sinter

1.5.1 基本信息

SINTER key [key ...]

summary: Intersect multiple sets

since: 1.0.0

Returns the members of the set resulting from the intersection of all the given sets.

返回由所有给定集的交集。

Keys that do not exist are considered to be empty sets. With one of the keys being an empty set, the resulting set is also empty (since set intersection with an empty set always results in an empty set).

不存在的键被认为是空集。如果其中一个键为空集,则结果集也为空(因为集与空集交集总是导致空集)。

1.5.2 返回

  • 包含结果集成员的列表

1.5.3 练习

127.0.0.1:6379[1]> sinter myset set2 set3
(empty array)

1.6 sinterstore

1.6.1 基本信息

SINTERSTORE destination key [key ...]

summary: Intersect multiple sets and store the resulting set in a key

since: 1.0.0

This command is equal to SINTER, but instead of returning the resulting set, it is stored in destination.

该命令等于 SINTER,但是不返回结果集,而是存储在目标中。

If destination already exists, it is overwritten.

如果目的地已经存在,则覆盖它。

1.6.2 返回

  • 结果集中的元素数

1.7 sismember

1.7.1 基本信息

SISMEMBER key member

summary: Determine if a given value is a member of a set

since: 1.0.0

Returns if member is a member of the set stored at key.

如果成员是按键存储的集的成员,则返回。

1.7.2 返回

  • 如果元素是集合的成员,则为1。
  • 如果元素不是集合的成员,或者键不存在,则返回0。

1.7.3 练习

127.0.0.1:6379[1]> sismember myset 1
(integer) 0
127.0.0.1:6379[1]> sismember myset a
(integer) 1

1.8 smembers

1.8.1 基本信息

SMEMBERS key

summary: Get all the members in a set

since: 1.0.0

Returns all the members of the set value stored at key.

返回按键存储的集值的所有成员。

1.8.2 返回

  • 集合中的所有元素

1.8.3 练习

127.0.0.1:6379[1]> smembers set2
1) "d"
2) "b"
3) "a"

1.9 smismember

1.9.1 基本信息

SMISMEMBER key member [member ...]

summary: Returns the membership associated with the given elements for a set

since: 6.2.0

Returns whether each member is a member of the set stored at key.

返回每个成员是否是按键存储的集的成员。

For every member, 1 is returned if the value is a member of the set, or 0 if the element is not a member of the set or if key does not exist.

对于每个成员,如果值是集合的成员,则返回1; 如果元素不是集合的成员或键不存在,则返回0。

1.9.2 返回

  • 表示给定元素的成员资格的列表,顺序与请求的顺序相同

1.9.3 练习

127.0.0.1:6379[1]> smismember set2 a c e f
1) (integer) 1
2) (integer) 0
3) (integer) 0
4) (integer) 0

1.10 smove

1.10.1 基本信息

SMOVE source destination member

summary: Move a member from one set to another

since: 1.0.0

Move member from the set at source to the set at destination. This operation is atomic. In every given moment the element will appear to be a member of source or destination for other clients.

将成员从源集移动到目标集。这次行动是原子级的。在每一个给定的时刻,元素看起来都是其他客户端的源或目标的成员。

If the source set does not exist or does not contain the specified element, no operation is performed and 0 is returned. Otherwise, the element is removed from the source set and added to the destination set. When the specified element already exists in the destination set, it is only removed from the source set.

如果源集不存在或不包含指定的元素,则不执行任何操作,并返回0。否则,元素将从源集中移除并添加到目标集中。当目标集中已经存在指定的元素时,只从源集中移除该元素。

An error is returned if source or destination does not hold a set value.

如果源或目标不包含设置值,则返回错误。

1.10.2 返回

  • 如果元素被移动,则为1
  • 如果元素不是源的成员并且未执行任何操作,则为0

1.10.3 练习

127.0.0.1:6379[1]> smove set2 set4 a
(integer) 1

1.11 spop

1.11.1 基本信息

SPOP key [count]

summary: Remove and return one or multiple random members from a set

since: 1.0.0

Removes and returns one or more random members from the set value store at key.

从键的设置值存储区中移除并返回一个或多个随机成员。

This operation is similar to SRANDMEMBER, that returns one or more random elements from a set but does not remove it.

这个操作类似于 SRANDMEMBER,它从一个集合中返回一个或多个随机元素,但不删除它。

By default, the command pops a single member from the set. When provided with the optional count argument, the reply will consist of up to count members, depending on the set's cardinality.

默认情况下,该命令从集合中弹出一个成员。当提供了可选的 count 参数时,根据集合的基数,应答将包含最多的 count 成员。

1.11.2 返回

  • 当不使用 count 参数调用时,返回已删除的成员,或键不存在时为 nil
  • 当使用 count 参数调用时,返回已删除的成员,或键不存在时为空数组

1.11.3 练习

127.0.0.1:6379[1]> spop set2
"b"
127.0.0.1:6379[1]> smembers set2
1) "d"

1.12 srandmember

1.12.1 基本信息

SRANDMEMBER key [count]

summary: Get one or multiple random members from a set

since: 1.0.0

When called with just the key argument, return a random element from the set value stored at key.

当仅使用 key 参数调用时,从存储在 key 上的 set 值返回一个随机元素。

If the provided count argument is positive, return an array of distinct elements. The array's length is either count or the set's cardinality (SCARD), whichever is lower.

如果提供的 count 参数为正,则返回一个包含不同元素的数组。数组的长度是 count 或集合的基数(SCARD) ,以较低者为准。

If called with a negative count, the behavior changes and the command is allowed to return the same element multiple times. In this case, the number of returned elements is the absolute value of the specified count.

如果调用时计数为负,则行为将发生更改,并允许命令多次返回相同的元素。在这种情况下,返回元素的数量是指定计数的绝对值。

1.12.2 返回

  • 如果没有附加 count 参数,该命令将返回一个随机选择的元素,如果键不存在,则返回 nil
  • 当传递额外的 count 参数时,命令返回一个元素数组,或者当键不存在时返回一个空数组

1.12.3 练习

127.0.0.1:6379[1]> srandmember set3
"f"

1.13 srem

1.13.1 基本信息

SREM key member [member ...]

summary: Remove one or more members from a set

since: 1.0.0

Remove the specified members from the set stored at key. Specified members that are not a member of this set are ignored. If key does not exist, it is treated as an empty set and this command returns 0.

从按键存储的集合中删除指定的成员。忽略不是此集成员的指定成员。如果 key 不存在,则将其视为空集,并且此命令返回0。

An error is returned when the value stored at key is not a set.

如果键上存储的值不是集合,则返回错误。

1.13.2 返回

  • 从集中删除的成员数,不包括不存在的成员

1.13.3 练习

127.0.0.1:6379[1]> srem set3 f
(integer) 1
127.0.0.1:6379[1]> smembers set3
1) "e"
2) "g"

1.14 sscan

1.14.1 基本信息

SSCAN key cursor [MATCH pattern] [COUNT count]

summary: Incrementally iterate Set elements

since: 2.8.0

1.15 sunion

1.15.1 基本信息

SUNION key [key ...]

summary: Add multiple sets

since: 1.0.0

Returns the members of the set resulting from the union of all the given sets.

返回所有给定集合的并集

Keys that do not exist are considered to be empty sets.

不存在的键被认为是空集。

1.15.2 返回

  • 包含结果集成员的列表

1.15.3 练习

127.0.0.1:6379[1]> sunion myset set2 set3
 1) "h"
 2) "i"
 3) "c"
 4) "f"
 5) "k"
 6) "j"
 7) "e"
 8) "n"
 9) "g"
10) "b"
11) "d"
12) "a"
13) "l"
14) "m"

1.16 sunionstore

1.16.1 基本信息

SUNIONSTORE destination key [key ...]

summary: Add multiple sets and store the resulting set in a key

since: 1.0.0

This command is equal to SUNION, but instead of returning the resulting set, it is stored in destination.

这个命令等于 SUNION,但是不返回结果集,而是存储在目标中。

If destination already exists, it is overwritten.

如果目的地已经存在,则覆盖它。

1.16.2 返回

  • 结果集中的元素数

1.16.3 练习

127.0.0.1:6379[1]> sunionstore newset set2 set3
(integer) 3
127.0.0.1:6379[1]> smembers newset
1) "e"
2) "d"
3) "g"

标签:返回,set,Redis,127.0,member,6379,Set,007,key
From: https://www.cnblogs.com/autumncat/p/16758080.html

相关文章

  • Winform 打包之Setup Wizard
    Winform打包网上找了很久,也尝试了别人各种方式,坑还是比较多的。以下流程自己已经尝试过,是可用的。另外:恶心CSDN某些人很久了,都把自己文章设置收益和VIP可见,本身就没技术含......
  • redis的两种持久化方式
    redis的两种持久化方式redis是一个内存数据库,一旦断电或服务器进程退出,内存数据库中的数据将全部丢失,所以需要redis持久化redis持久化就是把数据保存在磁盘上,利用永久性......
  • Redis 3.2 集群搭建
     Redis3.0版本之后支持Cluster.这里安装3.2版本1、下载安装包cd/usr/local/wgethttp://download.redis.io/releases/redis-3.2.1.tar.gztar-zxvf/redis-3.2.1.tar.gz2......
  • Redis之持久化存储
    Redis持久化解决方案RDBRDB存储的重点在于数据本身,将数据持久化存入后缀为.rdb的文件中,即快照,每隔一段时间记录新的数据,像快速拍照一样,每次拍完放在一边,用的时候快速......
  • Redis的五种基本结构
    Stirng类型操作成功返回大于0的数或Integer1操作失败Integer0nil为空基本指令1.添加一组键值对setnameJoshua2.根据key获取value>getname"Joshua"3.key删......
  • CF895C Square Subsets
    CF895CSquareSubsets注意到平方数要求每个质因数的幂次均为偶数。由于\(70\)以内仅有\(19\)个质因数,考虑将每个\(a_i\)按照每个质因数的奇偶性分解为\(01\)串......
  • 004-Redis 的 Generic 命令组
    1.Generic1.1copy1.1.1基本信息COPYsourcedestination[DBdestination-db][REPLACE]summary:Copyakeysince:6.2.0Thiscommandcopiesthevaluestored......
  • 005-Redis的 Hash 命令组
    1.Hash1.1hdel1.1.1基本信息HDELkeyfield[field...]summary:Deleteoneormorehashfieldssince:2.0.0Removesthespecifiedfieldsfromthehashstor......
  • Subsets of Array
    寻找一组数组中不重复元素的子集packagecom.example.mathematicaldemo.demo;importlombok.extern.slf4j.Slf4j;/***资料:*https://easylearn.baidu.com/edu-......
  • shell 脚本中 set -e选项的作用
     set-e选项保证程序的每一步都执行正确,如果前面一部程序执行错误,则直接退出程序001、不加set-e的情况(base)[root@PC1test2]#lstest.sh(base)[root@PC1tes......