|
@@ -5,7 +5,6 @@ import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
-import java.util.UUID;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
@@ -23,11 +22,11 @@ public class PushFrequencyUtil {
|
|
|
*
|
|
|
* @param pushFrequency 推送频率字符串
|
|
|
* @param ruleId 规则ID
|
|
|
- * @param pushContent 推送内容
|
|
|
+ * @param clientId 客户端id
|
|
|
* @param redisTemplate Redis模板
|
|
|
* @return 是否应该推送
|
|
|
*/
|
|
|
- public static boolean checkPushFrequency(String pushFrequency, Long ruleId, String pushContent, RedisTemplate<String, Object> redisTemplate) {
|
|
|
+ public static boolean checkPushFrequency(String pushFrequency, Long ruleId, String clientId, RedisTemplate<String, Object> redisTemplate) {
|
|
|
if (pushFrequency == null || pushFrequency.trim().isEmpty()) {
|
|
|
log.debug("规则{}的推送频率为空,默认推送", ruleId);
|
|
|
return true;
|
|
@@ -52,7 +51,7 @@ public class PushFrequencyUtil {
|
|
|
log.debug("规则{}推送频率{}大于1,向上取整为周期{}", ruleId, frequency, cycle);
|
|
|
|
|
|
// 使用Redis管理周期计数
|
|
|
- return checkPushCycleWithRedis(ruleId, pushContent, cycle, redisTemplate);
|
|
|
+ return checkPushCycleWithRedis(ruleId, clientId, cycle, redisTemplate);
|
|
|
}
|
|
|
} catch (NumberFormatException e) {
|
|
|
log.warn("规则{}的推送频率格式错误:{},默认推送", ruleId, pushFrequency);
|
|
@@ -64,26 +63,25 @@ public class PushFrequencyUtil {
|
|
|
* 使用Redis检查推送周期
|
|
|
*
|
|
|
* @param ruleId 规则ID
|
|
|
- * @param pushContent 推送内容
|
|
|
+ * @param clientId 客户端id
|
|
|
* @param cycle 周期
|
|
|
* @param redisTemplate Redis模板
|
|
|
* @return 是否应该推送
|
|
|
*/
|
|
|
- private static boolean checkPushCycleWithRedis(Long ruleId, String pushContent, int cycle, RedisTemplate<String, Object> redisTemplate) {
|
|
|
+ private static boolean checkPushCycleWithRedis(Long ruleId, String clientId, int cycle, RedisTemplate<String, Object> redisTemplate) {
|
|
|
if (cycle < 1) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- String uuid = UUID.randomUUID().toString();
|
|
|
-
|
|
|
+
|
|
|
// 构建Redis key,使用规则ID和推送内容作为唯一标识
|
|
|
- String countKey = String.format("mkt:push:count:%d:%s", ruleId, uuid);
|
|
|
+ String countKey = String.format("mkt:push:count:%d:%s", ruleId, clientId);
|
|
|
|
|
|
try {
|
|
|
// Redis原子自增计数
|
|
|
Long currentCount = redisTemplate.opsForValue().increment(countKey);
|
|
|
if (currentCount == null) {
|
|
|
- log.warn("Redis自增失败,规则ID:{},推送内容:{}", ruleId, pushContent);
|
|
|
+ log.warn("Redis自增失败,规则ID:{},推送内容:{}", ruleId, clientId);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -103,7 +101,7 @@ public class PushFrequencyUtil {
|
|
|
return false;
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
- log.error("Redis操作异常,规则ID:{},推送内容:{}", ruleId, pushContent, e);
|
|
|
+ log.error("Redis操作异常,规则ID:{},推送内容:{}", ruleId, clientId, e);
|
|
|
// Redis异常时默认推送,避免影响业务
|
|
|
return true;
|
|
|
}
|