新聞中心
??隨著數(shù)據(jù)量的增加,把大量數(shù)據(jù)存儲在Redis中已經(jīng)成為一種必然,其中最常用的就是把數(shù)據(jù)一次性批量插入到Redis中。但是,由于Redis本身是單線程的,如果一個插入操作的數(shù)據(jù)量過大,會導(dǎo)致內(nèi)存占用過多,存入數(shù)據(jù)的效率也會大大降低。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供姑蘇網(wǎng)站建設(shè)、姑蘇做網(wǎng)站、姑蘇網(wǎng)站設(shè)計、姑蘇網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、姑蘇企業(yè)網(wǎng)站模板建站服務(wù),十余年姑蘇做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
??針對這個問題,我們可以充分利用Java多線程編程的特性,來大大提升把大數(shù)據(jù)存儲到Redis的效率,如下代碼所示:
“`java
public static void mn(string[] args) {
long start = System.currentTimeMillis();
//定義要存儲的大量數(shù)據(jù)
List datalist = getBigDataList();
//定義線程數(shù)量
int threadCount = 8;
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
for (int i = 0; i
int begin = (datalist.size() / threadCount) * i;
int end = (datalist.size() / threadCount) * (i + 1);
if (i == (threadCount-1)) {
end = datalist.size();
}
List tmpList = datalist.subList(begin, end);
executorService.submit(new DataInsertRedisTask(tmpList));
}
long end = System.currentTimeMillis();
System.out.println(“耗時: “+(end-start));
executorService.shutdown();
}
//多線程任務(wù),將數(shù)據(jù)存儲到Redis
class DataInsertRedisTask implements Runnable {
List tmpList;
public DataInsertRedisTask (List tmpList) {
this.tmpList = tmpList;
}
@Override
public void run() {
// 批量把大量數(shù)據(jù)寫入Redis
for(String item : tmpList){
jedis.lpush(“bigDataList”, item);
}
}
}
??上述代碼中,通過設(shè)置線程數(shù)量,將大量數(shù)據(jù)拆分成若干個小數(shù)據(jù)分段,分別交由多個線程來進(jìn)行批量插入,從而大大提升了把大數(shù)據(jù)存入Redis的效率,實(shí)測百倍效率提升的效果。因此,在合理利用多線程,配合Redis的使用時,可以實(shí)現(xiàn)較高的效率提升。
香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機(jī)、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗(yàn)。專業(yè)提供云主機(jī)、虛擬主機(jī)、域名注冊、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
分享文章:間效率加快大數(shù)據(jù)存入Redis,提升效率百倍(大數(shù)據(jù)存入redis超時)
當(dāng)前網(wǎng)址:http://m.fisionsoft.com.cn/article/ccohgjc.html


咨詢
建站咨詢
