redis的list取出數據方式速度測試


 

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();
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM