继上一篇使用jedis连接redis-cluster进行zset数据结构api演示
之后的第五章节。本章内容讲解使用jedis连接redis-cluster进行set数据结构api演示。
package com.coderman.jedis.clusterdemo;
import com.alibaba.fastjson.JSON;
import org.junit.Test;
import java.util.Set;
/**
* @Author fanchunshuai
* @Date 2020/1/3 18
* @Description:
* set api测试
*/
public class SetAPITest extends ClusterTest{
/**
* 重复运行两遍
* 添加集合元素
*/
@Test
public void testAddSet(){
//用户1认识的朋友
String key = "user:1:friends";
String [] members = new String[10];
members[0]= "zhangsna";
members[1]= "lisi";
for (int i = 2;i < 10;i++){
members[ i ]= "wangwu"+i;
}
Long res = cluster.sadd(key,members);
//第一次运行返回10,第二次运行没有插入元素返回0
System.out.println("res = "+res);
}
/**
* 获取所有集合
*/
@Test
public void testSearhSet(){
String key = "user:1:friends";
Set<String> set = cluster.smembers(key);
System.out.println(JSON.toJSONString(set));
}
/**
* 获取集合元素大小
*/
@Test
public void testGetSsize(){
String key = "user:1:friends";
Long size = cluster.scard(key);
System.out.println("size = "+size);
}
/**
* 判断某元素是否在集合中
*/
@Test
public void testIsInSet(){
String key = "user:1:friends";
boolean b = cluster.sismember(key,"zhangsanfeng");
System.out.println("b = " + b);
}
/**
* redis差集运算
* No way to dispatch this command to Redis Cluster because keys have different slots
*/
@Test
public void testSetDiff(){
String key1 = "user:1:friends";
//用户1认识的朋友
String key2 = "user:2:friends";
String [] members = new String[2];
members[0]= "zhangsna";
members[1]= "lisi";
cluster.sadd(key2,members);
Set<String> dif1Set = cluster.sdiff(new String[]{key1,key2});
System.out.println("dif1Set = "+JSON.toJSONString(dif1Set));
Set<String> dif2Set = cluster.sdiff(new String[]{key2,key1});
System.out.println("dif2Set = "+JSON.toJSONString(dif2Set));
}
}
程序男
发布了152 篇原创文章 · 获赞 65 · 访问量 34万+
私信
关注