|
@@ -1,5 +1,6 @@
|
|
package com.diagbot.util;
|
|
package com.diagbot.util;
|
|
|
|
|
|
|
|
+import com.diagbot.dto.RuleDTO;
|
|
import com.diagbot.enums.RedisEnum;
|
|
import com.diagbot.enums.RedisEnum;
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
import org.apache.commons.collections4.MapUtils;
|
|
import org.apache.commons.collections4.MapUtils;
|
|
@@ -9,13 +10,10 @@ import org.springframework.dao.DataAccessException;
|
|
import org.springframework.data.redis.core.RedisOperations;
|
|
import org.springframework.data.redis.core.RedisOperations;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.SessionCallback;
|
|
import org.springframework.data.redis.core.SessionCallback;
|
|
|
|
+import org.springframework.data.redis.core.ValueOperations;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.Collection;
|
|
|
|
-import java.util.Date;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
-import java.util.Set;
|
|
|
|
|
|
+import java.util.*;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -31,6 +29,29 @@ public class RedisUtil {
|
|
@Qualifier("redisTemplateForSimilar")
|
|
@Qualifier("redisTemplateForSimilar")
|
|
RedisTemplate redisTemplate;
|
|
RedisTemplate redisTemplate;
|
|
|
|
|
|
|
|
+ private int batchSize = 1000; // 每批插入的键值对数量
|
|
|
|
+
|
|
|
|
+ public void batchInsert(Map<String, RuleDTO> largeMap) {
|
|
|
|
+ ValueOperations<String, RuleDTO> valueOps = redisTemplate.opsForValue();
|
|
|
|
+ Map<String, RuleDTO> batchMap = new HashMap<>(batchSize);
|
|
|
|
+
|
|
|
|
+ Iterator<Map.Entry<String, RuleDTO>> iterator = largeMap.entrySet().iterator();
|
|
|
|
+ int count = 0;
|
|
|
|
+
|
|
|
|
+ while (iterator.hasNext()) {
|
|
|
|
+ Map.Entry<String, RuleDTO> entry = iterator.next();
|
|
|
|
+ batchMap.put(entry.getKey(), entry.getValue());
|
|
|
|
+ count++;
|
|
|
|
+
|
|
|
|
+ if (count == batchSize || !iterator.hasNext()) {
|
|
|
|
+ valueOps.multiSet(batchMap);
|
|
|
|
+ batchMap.clear(); // 清空batchMap为下一批做准备
|
|
|
|
+ count = 0;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 更新指定的数据
|
|
* 更新指定的数据
|
|
*
|
|
*
|