The husband Tao Gong has been
清风
through the ages, and who is there to dare to call him a man?
Article Directory
Introduction
1. Basic commands
1. Add/remove elements
SADD key member [member …] //
添加
元素
特性
:
- Used to add
一个
or多个
elements to the collection , the key does not exist to自动
create. 不能有相同
Elements in the set , adding the same element will be忽略
dropped.- The return value is
成功加入
the number of elements in the collection (忽略掉的元素不算
)
SREM key member [member …] //
删除
元素
特性
:
- Remove
一个
or多个
element from the collection - The return value is the
删除成功
number of elements this time 不存在
Elements will be忽略
dropped
Case:
SADD
127.0.0.1:6379> sadd letters a //向集合中添加一个元素
(integer) 1
127.0.0.1:6379> sadd letters a b c //元素a已经存在,返回值为 2
(integer) 2
127.0.0.1:6379> sadd letters a b c //3个元素都存在,返回值为 0
(integer) 0
SREM
127.0.0.1:6379> srem letters a //删除元素a 返回值 1
(integer) 1
127.0.0.1:6379> srem letters b d //d元素不存在,返回值为 1
(integer) 1
127.0.0.1:6379> srem letters g //g元素不存在,返回值为 0
(integer) 0
practice:Use Java to operate Redis database:SADD
、SREM
@Test
public void test() {
Jedis jedis = new Jedis("127.0.0.1", 6379);
Long a = jedis.sadd("letters", new String[]{
"a"});
Long a_b_c = jedis.sadd("letters", new String[]{
"a", "b", "c"});
System.out.print("添加元素:a" + " ");
System.out.println("返回值:" + a);
System.out.print("添加元素:a、b、c" + " ");
System.out.println("返回值:" + a_b_c);
Long rm_a = jedis.srem("letters", new String[]{
"a"});
Long rm_a_b_c = jedis.srem("letters", new String[]{
"a", "b", "c"});
Long rm_d = jedis.srem("letters", "d");
System.out.print("删除元素:a" + " ");
System.out.println("返回值:" + rm_a);
System.out.print("删除元素:a、b、c" + " ");
System.out.println("返回值:" + rm_a_b_c);
System.out.print("删除元素:d" + " ");
System.out.println("返回值:" + rm_d);
}
添加元素:a 返回值:1
添加元素:a、b、c 返回值:2
删除元素:a 返回值:1
删除元素:a、b、c 返回值:2
删除元素:d 返回值:0
2. Get all the elements in the set
SMEMBERS key
特性
:
- Get the
所有
elements in the collection key
Will return if it doesn't exist空
127.0.0.1:6379> sadd letters a b c //添加3个元素
(integer) 3
127.0.0.1:6379> smembers letters //获取所有的元素
1) "b"
2) "c"
3) "a"
127.0.0.1:6379> smembers other //获取一个不存在的集合 返回空
(empty list or set)
3. Determine whether the element is in the set
SISMEMBER key member
特性
:
- No matter how many elements there are in the set,
时间复杂度
it is:O(1)
member
Return1
if it exists, return if it doesn't exist0
key
Return if it doesn't exist0
一次
Only the一个
element can be judged , otherwise an error will be reported:(error) ERR wrong number of arguments for 'sismember' command
127.0.0.1:6379> smembers letters //获取所有元素
1) "b"
2) "c"
3) "a"
127.0.0.1:6379> sismember letters a //判断元素 a
(integer) 1
127.0.0.1:6379> sismember letters a b //错误语法判断元素 报错
(error) ERR wrong number of arguments for 'sismember' command
127.0.0.1:6379> sismember letters d //判断不存在元素
(integer) 0
4. Operations between sets
SDIFF key [key …] //
差集
Calculation
特性
:
- The SDIFF command is used to
多个集合
perform差集
calculations. - Set
A
the setB
difference set is denotedA-B
, all the representative属于A
and the不属于B
set (shown below) is composed of elements, andA-B={x|x∈A且x∈B}
. - SDIFF command supports
同时传入多个键(key)
, when there are three or more, calculated order is calculated前面两个
, and then the calculation result with后面
a calculation依此类推
.
Case:
First, we define 3
a set: 1 2 3
setA:; setB:; 2 3 4
setC:, 2 3
when A and B perform the difference operation A-B
because 存在
2 3, in B 不存在1
, so 结果为1
, then we then calculate the result of AB 1
and C集合
perform the difference operation ( A-B)-C
, which is 1
And 2 3
operation, so calculation 结果为1
.
127.0.0.1:6379> sadd setA 1 2 3
(integer) 3
127.0.0.1:6379> sadd setB 2 3 4
(integer) 3
127.0.0.1:6379> sdiff setA setB
1) "1"
127.0.0.1:6379> sadd setC 2 3
(integer) 2
127.0.0.1:6379> sdiff setA setB setC
1) "1"
SINTER key [key …] //
交集
Calculation
特性
:
- Used to
多个集合
perform交集
operations on - Set A and set B represent a set
A∩B
of all属于A
and属于B
elements, ieA∩B={x|x∈A且x∈B}
. - This command supports multiple simultaneous incoming keys
用法同交集运算
.
Case:
127.0.0.1:6379> smembers setA
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> smembers setB
1) "2"
2) "3"
3) "4"
127.0.0.1:6379> sinter setA setB
1) "2"
2) "3"
SUNION key [key …] //
并集
Calculation
特性
:
- The SUNION command is used to
多个集合
perform并集
operations on - The union of set A and set B
A∪B
represents a set consisting of all属于A
or属于B
elements, ieA∪B={x|x∈A或x∈B}
. - This command supports multiple simultaneous incoming keys
用法同交集运算
.
案例
:
127.0.0.1:6379> smembers setA
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> smembers setB
1) "2"
2) "3"
3) "4"
127.0.0.1:6379> sunion setA setB
1) "1"
2) "2"
3) "3"
4) "4"
127.0.0.1:6379> smembers setC
1) "2"
2) "3"
127.0.0.1:6379> sunion setA setB setC
1) "1"
2) "2"
3) "3"
4) "4"
Second, advanced commands
1. Get the number of elements in the collection
SCARD key
This command is used to get the number of elements in the collection 元素个数
, if 键存在
, then the number of collection elements is returned normally, 不存在
then 返回0
.
7.0.0.1:6379> sadd nums 1 2 3 4 5 6 7 8 9 0
(integer) 10
127.0.0.1:6379> smembers nums
1) "0"
2) "1"
3) "2"
4) "3"
5) "4"
6) "5"
7) "6"
8) "7"
9) "8"
10) "9"
127.0.0.1:6379> scard nums
(integer) 10
127.0.0.1:6379> scard other //other键不存在,返回 0
(integer) 0
2. Perform set operations and store key results
The following three commands and STORE
the usage of removing the suffix are 相同
only 计算结果
stored 键destination
in the following three commands .
SDIFFSTORE destination key [key …]
SINTERSTORE destination key [key …]
SUNIONSTORE destination key [key …]
The usage of the above three commands is the SDIFF
SINTER
SUNION
same as the previous usage, so I won't introduce them in detail here.
127.0.0.1:6379> sadd setA 1 2 3
(integer) 3
127.0.0.1:6379> sadd setB 2 3 4
(integer) 3
127.0.0.1:6379> sadd setC 4 5 6
(integer) 3
127.0.0.1:6379> sdiffstore AB setA setB //将集合setA与集合setB的计算结果存储到集合AB中
(integer) 1
127.0.0.1:6379> keys *
1) "nums"
2) "setB"
3) "setA"
4) "setC"
5) "AB"
127.0.0.1:6379> smembers AB
1) "1"
3. Randomly obtain elements in the set
SRANDMEMBER key [count]
特性
:
- The SRANDMEMBER command is used to randomly
随机
get一个
elements from the set (没有count参数情况下
). 有count参数
Can be randomly obtained once多个元素
- When the count is
整数
the time to get to thatcount
one不重复
element - When count is
整数
and大于集合中的元素个数
, it will return to the collection全部元素
- When the count is
负数
, the randomly obtained from the collection|count|
element, and these元素可能相同
.
Case:
127.0.0.1:6379> sadd num 1 2 3 4 5 6 7 8 9
(integer) 9
127.0.0.1:6379> smembers num
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
127.0.0.1:6379> srandmember num //随机获取一个元素
"4"
127.0.0.1:6379> srandmember num //随机获取一个元素
"9"
127.0.0.1:6379> srandmember num 2 //随机获取2个元素 带参数
1) "5"
2) "7"
127.0.0.1:6379> srandmember num 3 //随机获取3个元素 带参数
1) "2"
2) "7"
3) "4"
127.0.0.1:6379> srandmember num 999 //count参数大于集合中的元素,获得全部元素
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
127.0.0.1:6379> srandmember num -5 //count参数为负数,获取绝对值个元素,这些元素可能相同
1) "8"
2) "8"
3) "6"
4) "8"
5) "4"
127.0.0.1:6379> srandmember num -9
1) "8"
2) "2"
3) "4"
4) "8"
5) "9"
6) "3"
7) "3"
8) "9"
9) "4"
127.0.0.1:6379> srandmember num -11 // count负值大于集合中所有元素
1) "8"
2) "3"
3) "4"
4) "1"
5) "6"
6) "2"
7) "2"
8) "1"
9) "4"
10) "6"
11) "4"
4. Pop an element from the collection
SPOP key
The function of this command is to 左边弹出
remove 一个
elements from the collection , and then return the value of the element. Because 集合无序
, the element that pops up is随机
7.0.0.1:6379> sadd nums 1 2 3 4 5 6 7 8
(integer) 8
127.0.0.1:6379> smembers nums
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
127.0.0.1:6379> spop nums
"3"
127.0.0.1:6379> spop nums
"5"
127.0.0.1:6379> spop nums
"1"
注
: It can be seen from the above case result that the pop-up element is yes 随机
.
Redis Chinese official website: http://www.redis.cn
未完待续,持续更新中...