|
@@ -4,6 +4,7 @@ import com.google.common.reflect.TypeToken;
|
|
|
import com.tzld.piaoquan.recommend.feature.produce.model.DTSConfig;
|
|
import com.tzld.piaoquan.recommend.feature.produce.model.DTSConfig;
|
|
|
import com.tzld.piaoquan.recommend.feature.produce.util.CompressionUtil;
|
|
import com.tzld.piaoquan.recommend.feature.produce.util.CompressionUtil;
|
|
|
import com.tzld.piaoquan.recommend.feature.produce.util.JSONUtils;
|
|
import com.tzld.piaoquan.recommend.feature.produce.util.JSONUtils;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -20,6 +21,7 @@ import java.util.concurrent.TimeUnit;
|
|
|
*
|
|
*
|
|
|
* @author dyp
|
|
* @author dyp
|
|
|
*/
|
|
*/
|
|
|
|
|
+@Slf4j
|
|
|
public class RedisService implements Serializable {
|
|
public class RedisService implements Serializable {
|
|
|
private int port = 6379;
|
|
private int port = 6379;
|
|
|
private String password = "";
|
|
private String password = "";
|
|
@@ -35,7 +37,8 @@ public class RedisService implements Serializable {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void mSetV2(Iterator<Map<String, String>> dataIte, DTSConfig config) {
|
|
|
|
|
|
|
+ public void mSetV2(Iterator<Map<String, String>> dataIte, DTSConfig config, boolean isV3Optimize) {
|
|
|
|
|
+ log.info("mSetV2 param: {}, isV3Optimize: {}", config, isV3Optimize);
|
|
|
Map<String, String> batch = new HashMap<>();
|
|
Map<String, String> batch = new HashMap<>();
|
|
|
Jedis jedis = new Jedis(hostName, port);
|
|
Jedis jedis = new Jedis(hostName, port);
|
|
|
jedis.auth(password);
|
|
jedis.auth(password);
|
|
@@ -57,8 +60,8 @@ public class RedisService implements Serializable {
|
|
|
|
|
|
|
|
String value = JSONUtils.toJson(record);
|
|
String value = JSONUtils.toJson(record);
|
|
|
batch.put(redisKey, value);
|
|
batch.put(redisKey, value);
|
|
|
- if (batch.size() >= 500) {
|
|
|
|
|
- mSet(jedis, batch, expire, TimeUnit.SECONDS);
|
|
|
|
|
|
|
+ if (batch.size() >= 1000) {
|
|
|
|
|
+ mSet(jedis, batch, expire, TimeUnit.SECONDS, isV3Optimize);
|
|
|
batch.clear();
|
|
batch.clear();
|
|
|
try {
|
|
try {
|
|
|
TimeUnit.MILLISECONDS.sleep(10);
|
|
TimeUnit.MILLISECONDS.sleep(10);
|
|
@@ -68,7 +71,7 @@ public class RedisService implements Serializable {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
if (MapUtils.isNotEmpty(batch)) {
|
|
if (MapUtils.isNotEmpty(batch)) {
|
|
|
- mSet(jedis, batch, expire, TimeUnit.SECONDS);
|
|
|
|
|
|
|
+ mSet(jedis, batch, expire, TimeUnit.SECONDS, isV3Optimize);
|
|
|
}
|
|
}
|
|
|
jedis.close();
|
|
jedis.close();
|
|
|
}
|
|
}
|
|
@@ -81,14 +84,17 @@ public class RedisService implements Serializable {
|
|
|
jedis.setex(redisKey, expire, "");
|
|
jedis.setex(redisKey, expire, "");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void mSet(Jedis jedis, Map<String, String> batch, long expire, TimeUnit timeUnit) {
|
|
|
|
|
|
|
+ private void mSet(Jedis jedis, Map<String, String> batch, long expire, TimeUnit timeUnit, boolean isV3Optimize) {
|
|
|
long expireSeconds = timeUnit.toSeconds(expire);
|
|
long expireSeconds = timeUnit.toSeconds(expire);
|
|
|
|
|
|
|
|
Pipeline pipeline = jedis.pipelined();
|
|
Pipeline pipeline = jedis.pipelined();
|
|
|
for (Map.Entry<String, String> e : batch.entrySet()) {
|
|
for (Map.Entry<String, String> e : batch.entrySet()) {
|
|
|
try {
|
|
try {
|
|
|
- String newKey = String.format("%s:v2", e.getKey());
|
|
|
|
|
- pipeline.setex(newKey.getBytes(StandardCharsets.UTF_8), expireSeconds, CompressionUtil.snappyCompressV2(e.getValue()));
|
|
|
|
|
|
|
+ String key = e.getKey();
|
|
|
|
|
+ if (!isV3Optimize) {
|
|
|
|
|
+ key = String.format("%s:v2", e.getKey());
|
|
|
|
|
+ }
|
|
|
|
|
+ pipeline.setex(key.getBytes(StandardCharsets.UTF_8), expireSeconds, CompressionUtil.snappyCompressV2(e.getValue()));
|
|
|
} catch (Exception ignore) {
|
|
} catch (Exception ignore) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|