每日0点执行删除sub

master
lld 2026-03-08 00:36:38 +08:00
parent 1f00faccd9
commit a02514748c
2 changed files with 84 additions and 1 deletions

View File

@ -0,0 +1,82 @@
package com.agri.quartz.task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Component("agriTask")
public class AgriTask {
@Resource
private RedisConnectionFactory redisConnectionFactory;
private final static Logger log = LoggerFactory.getLogger(AgriTask.class);
/**
*
* redis sub:
*/
public void clearInvalidCache() {
log.info("===== 开始执行Redis sub: 键清理任务 =====");
RedisConnection connection = null;
Cursor<byte[]> cursor = null;
int deletedCount = 0;
try {
// 获取Redis连接
connection = redisConnectionFactory.getConnection();
// 配置SCAN参数匹配sub:*分批遍历每次1000条
ScanOptions scanOptions = ScanOptions.scanOptions()
.match("sub:*")
.count(1000)
.build();
// 遍历所有匹配的键
cursor = connection.scan(scanOptions);
List<byte[]> batchKeys = new ArrayList<>(1000); // 批量删除缓冲区
while (cursor.hasNext()) {
batchKeys.add(cursor.next());
// 每攒1000个键批量删除减少网络交互
if (batchKeys.size() >= 1000) {
deletedCount += batchKeys.size();
connection.del(batchKeys.toArray(new byte[0][]));
log.info("批量删除 {} 个sub: 键", batchKeys.size());
batchKeys.clear();
}
}
// 删除剩余的键
if (!batchKeys.isEmpty()) {
deletedCount += batchKeys.size();
connection.del(batchKeys.toArray(new byte[0][]));
log.info("批量删除剩余 {} 个sub: 键", batchKeys.size());
}
log.info("===== Redis sub: 键清理完成,总计删除 {} 个键 =====", deletedCount);
} catch (Exception e) {
log.error("Redis sub: 键清理失败", e);
// 可选:添加告警逻辑(如钉钉/邮件通知)
} finally {
// 关闭游标和连接
if (cursor != null) {
try {
cursor.close();
} catch (Exception e) {
log.error("关闭游标失败", e);
}
}
if (connection != null) {
connection.close();
}
}
}
}

View File

@ -30,7 +30,7 @@ import java.util.stream.Collectors;
/**
*
*
*
* 1imei
* 2imeidtu_data
* 3 +
@ -183,6 +183,7 @@ public class RollerAutoTask {
//判断温度是否在适宜温度内
TempCommandStatus tempCommandStatus = TempJudgeUtil.judgeTempCommand(currentTemp, term.getTemp());
// todo 开关指令需要通知用户 推送主题 && 更新数据 前端重新请求消息表
if (tempCommandStatus == TempCommandStatus.OPEN) {
// 开指令
sendOpenCommand(imei,agriName, roller, isFirstRun, term.getVent(), reservedLen);