Commit 753efef3b109f4ce131b605b252b4da255b3300f

Authored by 648540858
1 parent a6321f08

清理录像合并临时文件时,清理redis记录

src/main/java/top/panll/assist/service/FileManagerTimer.java
... ... @@ -6,7 +6,10 @@ import org.slf4j.LoggerFactory;
6 6 import org.springframework.beans.factory.annotation.Autowired;
7 7 import org.springframework.scheduling.annotation.Scheduled;
8 8 import org.springframework.stereotype.Component;
  9 +import org.springframework.util.StringUtils;
  10 +import top.panll.assist.dto.MergeOrCutTaskInfo;
9 11 import top.panll.assist.dto.UserSettings;
  12 +import top.panll.assist.utils.RedisUtil;
10 13  
11 14 import java.io.File;
12 15 import java.text.ParseException;
... ... @@ -19,6 +22,7 @@ import java.util.List;
19 22 public class FileManagerTimer {
20 23  
21 24 private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  25 + private final SimpleDateFormat simpleDateFormatForTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
22 26  
23 27 private final static Logger logger = LoggerFactory.getLogger(FileManagerTimer.class);
24 28  
... ... @@ -28,8 +32,13 @@ public class FileManagerTimer {
28 32 @Autowired
29 33 private VideoFileService videoFileService;
30 34  
31   -// @Scheduled(fixedDelay = 2000) //测试 20秒执行一次
32   - @Scheduled(cron = "0 0 0 * * ?") //每天的0点执行
  35 + @Autowired
  36 + private RedisUtil redisUtil;
  37 +
  38 + private final String keyStr = "MERGEORCUT";
  39 +
  40 + @Scheduled(fixedDelay = 2000) //测试 20秒执行一次
  41 +// @Scheduled(cron = "0 0 0 * * ?") //每天的0点执行
33 42 public void execute(){
34 43 int recordDay = userSettings.getRecordDay();
35 44 Date lastDate=new Date();
... ... @@ -115,5 +124,20 @@ public class FileManagerTimer {
115 124 }
116 125 }
117 126 }
  127 + // 清理redis记录
  128 + String key = String.format("%S_*_*_*", keyStr);
  129 + List<Object> taskKeys = redisUtil.scan(key);
  130 + for (Object taskKeyObj : taskKeys) {
  131 + String taskKey = (String) taskKeyObj;
  132 + MergeOrCutTaskInfo mergeOrCutTaskInfo = (MergeOrCutTaskInfo)redisUtil.get(taskKey);
  133 + try {
  134 + if (StringUtils.isEmpty(mergeOrCutTaskInfo.getCreateTime())
  135 + || simpleDateFormatForTime.parse(mergeOrCutTaskInfo.getCreateTime()).before(lastTempDate)) {
  136 + redisUtil.del(taskKey);
  137 + }
  138 + } catch (ParseException e) {
  139 + e.printStackTrace();
  140 + }
  141 + }
118 142 }
119 143 }
... ...