package com.usky.rule.cache; import com.usky.common.redis.core.RedisHelper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; /** * 设备采集触发规则成功执行动作后的冷却窗口,避免短时间内频繁上报反复执行同一规则。 */ @Component public class DeviceAcqTriggerCooldownCache { private static final String KEY_PREFIX = "ruleEngine:deviceAcqCooldown:"; @Autowired private RedisHelper redisHelper; public boolean isInCooldown(Long ruleEngineId, String deviceId) { return redisHelper.hasKey(buildKey(ruleEngineId, deviceId)); } /** * 执行成功后调用,在 {@code cooldownSeconds} 秒内同规则同设备不再触发动作。 */ public void startCooldown(Long ruleEngineId, String deviceId, long cooldownSeconds) { if (cooldownSeconds <= 0) { return; } redisHelper.set(buildKey(ruleEngineId, deviceId), "1", cooldownSeconds, TimeUnit.SECONDS); } private static String buildKey(Long ruleEngineId, String deviceId) { return KEY_PREFIX + ruleEngineId + ":" + deviceId; } }