redis set命令操作

1.添加一个或多个成员到set
SADD key member [member ...]
summary: Add one or more members to a set
since: 1.0.0
127.0.0.1:6379> sadd set1 val1 val2 val3
(integer) 3

2.获取一个set的成员数量
SCARD key
summary: Get the number of members in a set
since: 1.0.0
127.0.0.1:6379> scard set1
(integer) 3

3.查询在第一个set中但不在后续set中的成员,并返回
SDIFF key [key ...]
summary: Subtract multiple sets
since: 1.0.0
127.0.0.1:6379> SMEMBERS set1
1) "val3"
2) "val4"
3) "val5"
4) "val2"
5) "val1"
127.0.0.1:6379> SMEMBERS set2
1) "val3"
2) "val4"
3) "val2"
127.0.0.1:6379> SDIFF set2 set1
(empty list or set)
127.0.0.1:6379> SDIFF set1 set2
1) "val5"
2) "val1"

4.查询key1中的成员但不在key2等后续key中的成员,并将获得的结果放在des-key中
SDIFFSTORE des-key key1 [key2 ...]
summary: Subtract multiple sets and store the resulting set in a key
since: 1.0.0
127.0.0.1:6379> SDIFFSTORE set3 set1 set2
(integer) 2
127.0.0.1:6379> SMEMBERS set3
1) "val5"
2) "val1"

5.查询set中的重复项
SINTER key [key ...]
summary: Intersect multiple sets
since: 1.0.0
127.0.0.1:6379> sinter set1 set2
1) "val3"
2) "val4"
3) "val2"
127.0.0.1:6379> sinter set2 set1
1) "val3"
2) "val4"
3) "val2"

6.查询set中的重复项,并放在目标set中
SINTERSTORE des-key key [key ...]
summary: Intersect multiple sets and store the resulting set in a key
since: 1.0.0
127.0.0.1:6379> SINTERSTORE set4 set1 set2
(integer) 3
127.0.0.1:6379> SMEMBERS set4
1) "val3"
2) "val2"
3) "val4"

7.查询指定的成员是否在指定的key中(在返回1,不在返回0)
SISMEMBER key member
summary: Determine if a given value is a member of a set
since: 1.0.0

8.查询set中所有的成员
SMEMBERS key
summary: Get all the members in a set
since: 1.0.0
127.0.0.1:6379> SMEMBERS set4
1) "val3"
2) "val2"
3) "val4"

9.将set中的一个成员从set移到另一个set
SMOVE source destination member
summary: Move a member from one set to another
since: 1.0.0
127.0.0.1:6379> smove set4 set3 val3
(integer) 1
127.0.0.1:6379> SMEMBERS set4
1) "val2"
2) "val4"
127.0.0.1:6379> SMEMBERS set3
1) "val3"
2) "val5"
3) "val1"

10.移除并返回随机的成员
SPOP key
summary: Remove and return a random member from a set
since: 1.0.0
127.0.0.1:6379> SMEMBERS set3
1) "val3"
2) "val5"
3) "val1"
127.0.0.1:6379> spop set3
"val1"
127.0.0.1:6379> SMEMBERS set3
1) "val3"
2) "val5"

11.获取一个或多个随机成员(count指代获取的数目)
SRANDMEMBER key [count]
summary: Get one or multiple random members from a set
since: 1.0.0
127.0.0.1:6379> SRANDMEMBER set3 2
1) "val3"
2) "val5"

12.移除set中的一个或多个成员
SREM key member [member ...]
summary: Remove one or more members from a set
since: 1.0.0
127.0.0.1:6379> srem set3 val3 val4
(integer) 1  #只删除了val3,val4不存在所以返回1

13.添加多个sets,显示多个sets的结果,不重复
SUNION key [key ...]
summary: Add multiple sets
since: 1.0.0

127.0.0.1:6379> SUNION set3 set4 set1
1) "val3"
2) "val5"
3) "val4"
4) "val2"
5) "val1"

14.连接多个set结果,并将结果存在目标set中
SUNIONSTORE dest-key key [key ...]
summary: Add multiple sets and store the resulting set in a key
since: 1.0.0

127.0.0.1:6379> SUNIONstore  set6  set3 set4 set1
(integer) 5

时间: 2024-10-01 03:48:41

redis set命令操作的相关文章

redis hash命令操作

/*********redis hash命令操作 **********/ 1.删除一个或多个hash域 HDEL key field [field ...] summary: Delete one or more hash fields since: 2.0.0 127.0.0.1:6379> HDEL hash1 name (integer) 1 2.判断一个域是否存在 HEXISTS key field summary: Determine if a hash field exists si

Redis——常用命令操作

 一,启动   Centos6       进入到redis安装目录: cd redis-2.8.17     进入src目录: cd src     启动server:  ./redis-server    使用配置文件启动: ./redis-serverredis.conf      Ubuntu 启动server:redis-server 启动客户端:redis-cli    Windows   redis-cli.exe-h 127.0.0.1 -p 6379 二,使用命令对Redis基

redis sorted_set 命令操作

score 是set排序的值,越大越在前 1.添加 成员到sorted_set中,并设置score ZADD key 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 127.0.0.1:6379> zadd sset1 2 val4 (integer) 1 2. 查询sort

redis list命令操作

1.将值追加到列表 RPUSH key value [value ...] summary: Append one or multiple values to a list since: 1.0.0 127.0.0.1:6379> RPUSH mylist value1 value2 value3 (integer) 3 2.获取列表的长度 LLEN key summary: Get the length of a list since: 1.0.0 127.0.0.1:6379> llen

redis transaction和connection命令操作

/*********** redis transaction命令操作 **********/ 1. 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 sinc

redis server和scripting 命令操作

/********* redis server命令操作 *************/ BGREWRITEAOF - summary: Asynchronously rewrite the append-only file since: 1.0.0 BGSAVE - summary: Asynchronously save the dataset to disk since: 1.0.0 CLIENT GETNAME - summary: Get the current connection na

redis命令操作之generic和string.java

redis是键值对的内存数据库,属于Nosql范畴 登录redis自带的客户端 #redis-cli 127.0.0.1:6379> 1.设置redis 键值对 > set foo bar  #设置键foo的值为"bar" OK #设置成功 2.获得redis 键值对 > get foo #根据键获得值 "bar" #返回的结果 3.删除键值对  127.0.0.1:6379> del foo foo2 foo3 #删除键值对,可删除多个 (

第2讲 Redis常用命令与高级应用

目录 一.redis数据类型 5. sorted sets类型和操作 二.Redis常用命令 1.键值相关命令 2.服务器相关命令 三. redis高级应用 1. 给redis服务器设置密码 2.持久化 3.主从备份 一.redis数据类型 5. sorted sets类型和操作 sorted set是set的一个升级版本,它给集合中每个元素都定义一个分数,集合中的元素按照其分数排序. 1) zadd 键 分数1 值1 [分数2 值2-] 该命令添加指定的成员到key对应的有序集合中,每个成员都

redis-Ubuntu server 15.0.4安装Redis make命令不能执行

问题描述 Ubuntu server 15.0.4安装Redis make命令不能执行 按照官方的下载安装指导:http://redis.io/download#installation Download, extract and compile Redis with: $ wget http://download.redis.io/releases/redis-3.0.3.tar.gz $ tar xzf redis-3.0.3.tar.gz $ cd redis-3.0.3 $ make T