redis测试:
package business; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import redis.JedisClientSingle;
import redis.clients.jedis.JedisPool; /**
* @Package redis
* @ClassName BusinessTest.java
* @author libin
* @date 2019年4月12日 下午2:16:43
* @version V1.0
*/
public class BusinessTest { public static void main(String[] args) throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-jedis.xml");
// JedisPool pool = (JedisPool)
// applicationContext.getBean("redisClient");
JedisPool pool = (JedisPool) applicationContext.getBean("jedisPool");
JedisClientSingle j = new JedisClientSingle(pool); // m1(j);
m5(j,"l2");
// m3(j,"l5");
// m4(j,"l5"); }
//
// redis中的list操作命令中删除指定key中的所有记录命令:
//
// ltrim key 1 0 //结论:这种取数据的方式还可以接受,不如直接取快lrange
private static void m5(JedisClientSingle j,String name) throws Exception {
long t1 = System.currentTimeMillis();
int i = 0;
while(true) {
i++;
String lpop = j.lpop(name);
System.out.println(lpop);
if (lpop==null) {
System.out.println("取完了");
break;
}
} // null
// 取完了
// ------个数:+220001---耗时-------:75990
System.out.println("------个数:+" + i + "---耗时-------:" + (System.currentTimeMillis() - t1));// 11000条5569
// ~5550毫秒
} private static void m4(JedisClientSingle j,String name) throws Exception {
long t1 = System.currentTimeMillis();
for (int i = 0; i < 20; i++) {
// 每次插入11000条
m1(j,name);
}
// 11000条5569
// ------个数:+220000---耗时-------:77590
// ------个数:+220000---耗时-------:78986
// ------个数:+220000---耗时-------:76039
System.out.println("------个数:+" + j.llen(name) + "---耗时-------:" + (System.currentTimeMillis() - t1));// 11000条5569
// ~5550毫秒
} private static void m3(JedisClientSingle j,String name) throws Exception {
long t1 = System.currentTimeMillis();
Long len = j.llen(name);
for (int k = 0; k < len; k++) {
// 根据角标取
String s = j.lindex(name, k);
System.out.println(s);
}
// ------个数:+11000---耗时-------:5550 如果20万用这种方式取,要10多个小时
System.out.println("------个数:+" + len + "---耗时-------:" + (System.currentTimeMillis() - t1));// 11000条5569
// ~5550毫秒
} //经过测试得出结论:取出数据可以用lrange方法 20万数据都没问题
private static void m2(JedisClientSingle j,String name) throws Exception {
long t1 = System.currentTimeMillis();
// 按照范围取
List<String> lrange = j.lrange(name, 0, -1);
for (String string : lrange) {
System.out.println(string);
}
// ------个数:+11000---耗时-------:579
// ------个数:+220000---耗时-------:25499
// ------个数:+220000---耗时-------:9950
System.out.println("------个数:+" + lrange.size() + "---耗时-------:" + (System.currentTimeMillis() - t1));// 11000条529
// ~700毫秒
} private static void m1(JedisClientSingle j,String name) throws Exception {
// 处理文件
long t1 = System.currentTimeMillis(); // String localFilePath = localTempPath+"/"+fileName;
String localFilePath = "D:\\a\\c\\haha.txt"; // 开启固定线程池
// ExecutorService exec = Executors.newFixedThreadPool(50);
// 逐行读取本地文件
List<String> dataList = new ArrayList<String>(); // File f = new File("D:\\a\\b\\in.txt");
File f = new File(localFilePath);
InputStreamReader reader = new InputStreamReader(new FileInputStream(f), "GBK");
BufferedReader br = new BufferedReader(reader);
String str = null;
// 定义计数器
int i = 0;
while ((str = br.readLine()) != null) {
// i的值是从1开始
i++;
// 逐条右插入
// Long len = j.rpush("l1", "l1-"+str);
Long len = j.rpush(name, name+"-" + str);
System.out.println(len);
}
reader.close();
br.close();
} }