继上一篇使用jedis连接redis-cluster进行字符串数据结构api演示
之后的第二章节。本章内容讲解使用jedis连接redis-cluster进行list列表数据结构api演示。
package com.coderman.jedis.clusterdemo;
import com.alibaba.fastjson.JSON;
import org.junit.Test;
import java.util.List;
import java.util.Random;
/**
* @Author fanchunshuai
* @Date 2019/12/30 10
* @Description:
* redis list api操作
*/
public class ListAPITest extends ClusterTest{
private static final String DEPART_KEYS = "XXJS:DEPART";
/**
* 右侧插入
*/
@Test
public void testAddRPush(){
String key = DEPART_KEYS +":ID";
//list 右侧插入5条,返回总共条数
//Long result = cluster.rpush(key,new String[]{"6","7","8","9","10"});
//循环插入10w数据
for (int i = 0; i < 1000000 ; i++) {
cluster.rpush(key,new String[]{i+""});
}
}
/**
* 左侧插入
*/
@Test
public void testAddLPush(){
String key = DEPART_KEYS +":ID";
//list 右侧插入5条,返回总共条数
Long result = cluster.lpush(key,new String[]{"11","12","13","14","15"});
System.out.println("result = "+result);
}
/**
* 按范围返回条数
*/
@Test
public void testRange(){
String key = DEPART_KEYS +":ID";
//list 右侧插入5条,返回总共条数
List<String> result = cluster.lrange(key,1L,5L);
System.out.println("result = "+result);
}
@Test
public void testGetAll(){
String key = DEPART_KEYS +":ID";
//list 右侧插入5条,返回总共条数
long startTime = System.currentTimeMillis();
List<String> result = cluster.lrange(key,1L,-1L);
System.out.println("result.size = "+result.size());
long endTime = System.currentTimeMillis();
System.out.println("result.time = "+(endTime - startTime));
//数据量越大(100w数据,每页取10w),后面分页会越慢,参考mysql limit,类似
for (int i = 0;i < 10;i ++){
long startTime2 = System.currentTimeMillis();
List<String> resultI = cluster.lrange(key,1L,(i+1) * 100000);
System.out.println("resultI.size = "+resultI.size());
long endTime2 = System.currentTimeMillis();
System.out.println("result.time = "+(endTime2 - startTime2));
}
//数据量越大(100w数据,每页取1w),后面分页会越慢,参考mysql limit,类似
for (int i = 0;i < 100;i ++){
long startTime2 = System.currentTimeMillis();
List<String> resultI = cluster.lrange(key,1L,(i+1) * 10000);
System.out.println("resultI2.size = "+resultI.size());
long endTime2 = System.currentTimeMillis();
System.out.println("result2.time = "+(endTime2 - startTime2));
}
}
/**
* 返回列表长度
*/
@Test
public void testGetLength(){
String key = DEPART_KEYS +":ID";
Long length = cluster.llen(key);
System.out.println("length = "+length);
}
/**
* 根据索引获取值
*/
@Test
public void testGetIndex(){
String key = DEPART_KEYS +":ID";
String indexOneValue = cluster.lindex(key,1L);
System.out.println("indexOneValue = "+indexOneValue);
}
/**
* 按范围删除数据
*/
@Test
public void testtrimDelete(){
String key = DEPART_KEYS +":ID";
//保留1~10000之间的数,不在此区间内的数删除,
long startTime = System.currentTimeMillis();
String indexOneValue = cluster.ltrim(key,1L,10000L);
long endTime = System.currentTimeMillis();
System.out.println("indexOneValue.time = "+(endTime - startTime));
System.out.println("indexOneValue = "+indexOneValue);
}
/**
* 阻塞获取
* @throws InterruptedException
*/
@Test
public void testBrpop() throws InterruptedException {
String key = DEPART_KEYS +":ID";
Thread pop1 = new Thread(()->{
Long length = cluster.llen(key);
while (length > 0){
List<String> value = cluster.brpop(1,key);
System.out.println(Thread.currentThread().getName()+","+"pop1.info = " + JSON.toJSONString(value));
length = cluster.llen(key);
}
},"pop-1");
Thread pop2 = new Thread(()->{
Long length = cluster.llen(key);
while (length > 0){
List<String> value = cluster.brpop(1,key);
System.out.println(Thread.currentThread().getName()+","+"pop2.info = " + JSON.toJSONString(value));
length = cluster.llen(key);
}
},"pop-2");
pop1.start();
pop2.start();
pop1.join();
pop2.join();
}
/**
* 简单实现一个消费者生产者模型
* @throws InterruptedException
*/
@Test
public void testBrpopPush() throws InterruptedException {
String key = DEPART_KEYS +":ID";
Random random = new Random();
Thread push = new Thread(()->{
while (true){
cluster.lpush(key,random.nextInt(100000)+"");
long length = cluster.llen(key);
System.out.println(Thread.currentThread().getName()+","+"length = "+length);
}
},"push");
Thread pop2 = new Thread(()->{
while (true){
List<String> value = cluster.brpop(10,key);
System.out.println(Thread.currentThread().getName()+","+"pop2.info = "+value.size());
}
},"pop-2");
Thread pop3 = new Thread(()->{
while (true){
List<String> value = cluster.brpop(10,key);
System.out.println(Thread.currentThread().getName()+","+"pop3.info = "+value.size());
}
},"pop-3");
Thread pop4 = new Thread(()->{
while (true){
List<String> value = cluster.brpop(10,key);
System.out.println(Thread.currentThread().getName()+","+"pop4.info = "+value.size());
}
},"pop-4");
push.start();
pop2.start();
pop3.start();
pop4.start();
push.join();
pop2.join();
pop3.join();
pop4.join();
}
}