Commit a3d583e3b09f3cac9604d09dcd106e5491eb68b9

Authored by 648540858
1 parent 2c470f50

优化常量的使用,服务增加支持redis数据隔离

src/main/java/top/panll/assist/controller/RecordController.java
... ... @@ -14,9 +14,7 @@ import org.springframework.web.bind.annotation.*;
14 14 import top.panll.assist.controller.bean.ControllerException;
15 15 import top.panll.assist.controller.bean.ErrorCode;
16 16 import top.panll.assist.controller.bean.WVPResult;
17   -import top.panll.assist.dto.MergeOrCutTaskInfo;
18   -import top.panll.assist.dto.SignInfo;
19   -import top.panll.assist.dto.SpaceInfo;
  17 +import top.panll.assist.dto.*;
20 18 import top.panll.assist.service.VideoFileService;
21 19 import top.panll.assist.utils.PageInfo;
22 20 import top.panll.assist.utils.RedisUtil;
... ... @@ -43,6 +41,9 @@ public class RecordController {
43 41 @Autowired
44 42 private RedisUtil redisUtil;
45 43  
  44 + @Autowired
  45 + private UserSettings userSettings;
  46 +
46 47 private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
47 48  
48 49  
... ... @@ -372,7 +373,7 @@ public class RecordController {
372 373 @GetMapping(value = "/addStreamCallInfo", produces = "application/json;charset=UTF-8")
373 374 @PostMapping(value = "/addStreamCallInfo", produces = "application/json;charset=UTF-8")
374 375 public void addStreamCallInfo(String app, String stream, String callId) {
375   - String key = "Stream_Call_Info" + app + "_" + stream;
  376 + String key = AssistConstants.STREAM_CALL_INFO + userSettings.getId() + "_" + app + "_" + stream;
376 377 redisUtil.set(key, callId, -1);
377 378 }
378 379  
... ...
src/main/java/top/panll/assist/dto/AssistConstants.java 0 → 100644
  1 +package top.panll.assist.dto;
  2 +
  3 +public class AssistConstants {
  4 +
  5 + public final static String STREAM_CALL_INFO = "STREAM_CALL_INFO_";
  6 +
  7 + public final static String MERGEORCUT = "MERGEORCUT_";
  8 +}
... ...
src/main/java/top/panll/assist/dto/UserSettings.java
... ... @@ -3,9 +3,15 @@ package top.panll.assist.dto;
3 3 import org.springframework.beans.factory.annotation.Value;
4 4 import org.springframework.stereotype.Component;
5 5  
  6 +/**
  7 + * @author lin
  8 + */
6 9 @Component
7 10 public class UserSettings {
8 11  
  12 + @Value("${userSettings.id}")
  13 + private String id;
  14 +
9 15 @Value("${userSettings.record}")
10 16 private String record;
11 17  
... ... @@ -24,6 +30,14 @@ public class UserSettings {
24 30 @Value("${userSettings.threads:2}")
25 31 private int threads;
26 32  
  33 + public String getId() {
  34 + return id;
  35 + }
  36 +
  37 + public void setId(String id) {
  38 + this.id = id;
  39 + }
  40 +
27 41 public String getRecord() {
28 42 return record;
29 43 }
... ...
src/main/java/top/panll/assist/service/FFmpegExecUtils.java
... ... @@ -49,9 +49,6 @@ public class FFmpegExecUtils implements InitializingBean{
49 49 @Autowired
50 50 private UserSettings userSettings;
51 51  
52   - @Autowired
53   - private RedisUtil redisUtil;
54   -
55 52 private FFprobe ffprobe;
56 53 private FFmpeg ffmpeg;
57 54  
... ...
src/main/java/top/panll/assist/service/FileManagerTimer.java
... ... @@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
7 7 import org.springframework.scheduling.annotation.Scheduled;
8 8 import org.springframework.stereotype.Component;
9 9 import org.springframework.util.StringUtils;
  10 +import top.panll.assist.dto.AssistConstants;
10 11 import top.panll.assist.dto.MergeOrCutTaskInfo;
11 12 import top.panll.assist.dto.UserSettings;
12 13 import top.panll.assist.utils.RedisUtil;
... ... @@ -35,8 +36,6 @@ public class FileManagerTimer {
35 36 @Autowired
36 37 private RedisUtil redisUtil;
37 38  
38   - private final String keyStr = "MERGEORCUT";
39   -
40 39 // @Scheduled(fixedDelay = 2000) //测试 20秒执行一次
41 40 @Scheduled(cron = "0 0 0 * * ?") //每天的0点执行
42 41 public void execute(){
... ... @@ -128,7 +127,7 @@ public class FileManagerTimer {
128 127 }
129 128 }
130 129 // 清理redis记录
131   - String key = String.format("%S_*_*_*", keyStr);
  130 + String key = String.format("%S_%S_*_*_*", AssistConstants.MERGEORCUT, userSettings.getId());
132 131 List<Object> taskKeys = redisUtil.scan(key);
133 132 for (Object taskKeyObj : taskKeys) {
134 133 String taskKey = (String) taskKeyObj;
... ...
src/main/java/top/panll/assist/service/VideoFileService.java
... ... @@ -10,11 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired;
10 10 import org.springframework.data.redis.core.RedisTemplate;
11 11 import org.springframework.stereotype.Service;
12 12 import org.springframework.util.DigestUtils;
13   -import top.panll.assist.dto.SignInfo;
14   -import top.panll.assist.dto.SpaceInfo;
  13 +import top.panll.assist.dto.*;
15 14 import top.panll.assist.utils.RedisUtil;
16   -import top.panll.assist.dto.MergeOrCutTaskInfo;
17   -import top.panll.assist.dto.UserSettings;
18 15 import top.panll.assist.utils.DateUtils;
19 16  
20 17 import java.io.File;
... ... @@ -39,9 +36,6 @@ public class VideoFileService {
39 36 private RedisUtil redisUtil;
40 37  
41 38 @Autowired
42   - private RedisTemplate redisTemplate;
43   -
44   - @Autowired
45 39 private FFmpegExecUtils ffmpegExecUtils;
46 40  
47 41  
... ... @@ -49,8 +43,6 @@ public class VideoFileService {
49 43 private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
50 44 private final SimpleDateFormat simpleDateFormatForTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
51 45  
52   - private final String keyStr = "MERGEORCUT";
53   -
54 46 public List<File> getAppList(Boolean sort) {
55 47 File recordFile = new File(userSettings.getRecord());
56 48 if (recordFile.isDirectory()) {
... ... @@ -150,7 +142,7 @@ public class VideoFileService {
150 142 long endTime = startTime + durationLong;
151 143 endTime = endTime - endTime%1000;
152 144  
153   - String key = "Stream_Call_Info" + app + "_" + stream;
  145 + String key = AssistConstants.STREAM_CALL_INFO + userSettings.getId() + "_" + app + "_" + stream;
154 146 String callId = (String) redisUtil.get(key);
155 147 if (callId != null) {
156 148  
... ... @@ -396,9 +388,8 @@ public class VideoFileService {
396 388 mergeOrCutTaskInfo.setDownloadFile(remoteHost + "/download.html?url=" + relativize);
397 389 mergeOrCutTaskInfo.setPlayFile(remoteHost + "/" + relativize);
398 390 }
399   - String key = String.format("%S_%S_%S_%S", keyStr, mergeOrCutTaskInfo.getApp(), mergeOrCutTaskInfo.getStream(), mergeOrCutTaskInfo.getId());
  391 + String key = String.format("%S_%S_%S_%S_%S", AssistConstants.MERGEORCUT , userSettings.getId(), mergeOrCutTaskInfo.getApp(), mergeOrCutTaskInfo.getStream(), mergeOrCutTaskInfo.getId());
400 392 redisUtil.set(key, mergeOrCutTaskInfo);
401   - redisUtil.convertAndSend("topic_mergeorcut_end", mergeOrCutTaskInfo);
402 393 logger.info("[录像合并] 合并完成,APP:{}, STREAM: {}, 任务ID:{}", app, stream, taskId);
403 394 }else {
404 395 ffmpegExecUtils.mergeOrCutFile(filesInTime, recordFile, stream, (status, percentage, result)->{
... ... @@ -413,13 +404,11 @@ public class VideoFileService {
413 404 mergeOrCutTaskInfo.setDownloadFile(remoteHost + "/download.html?url=" + relativize);
414 405 mergeOrCutTaskInfo.setPlayFile(remoteHost + "/" + relativize);
415 406 }
416   - redisUtil.convertAndSend("topic_mergeorcut_end", mergeOrCutTaskInfo);
417 407 logger.info("[录像合并] 合并完成,APP:{}, STREAM: {}, 任务ID:{}", app, stream, taskId);
418 408 }else {
419 409 mergeOrCutTaskInfo.setPercentage(percentage + "");
420   - redisUtil.convertAndSend("topic_mergeorcut_continue", mergeOrCutTaskInfo);
421 410 }
422   - String key = String.format("%S_%S_%S_%S", keyStr, mergeOrCutTaskInfo.getApp(), mergeOrCutTaskInfo.getStream(), mergeOrCutTaskInfo.getId());
  411 + String key = String.format("%S_%S_%S_%S_%S", AssistConstants.MERGEORCUT, userSettings.getId(), mergeOrCutTaskInfo.getApp(), mergeOrCutTaskInfo.getStream(), mergeOrCutTaskInfo.getId());
423 412 redisUtil.set(key, mergeOrCutTaskInfo);
424 413 });
425 414 }
... ... @@ -500,7 +489,8 @@ public class VideoFileService {
500 489 if (taskId == null) {
501 490 taskId = "*";
502 491 }
503   - List<Object> taskCatch = redisUtil.scan(String.format("%S_%S_%S_%S", keyStr, app, stream, taskId));
  492 + List<Object> taskCatch = redisUtil.scan(String.format("%S_%S_%S_%S_%S", AssistConstants.MERGEORCUT,
  493 + userSettings.getId(), app, stream, taskId));
504 494 for (int i = 0; i < taskCatch.size(); i++) {
505 495 String keyItem = taskCatch.get(i).toString();
506 496 MergeOrCutTaskInfo mergeOrCutTaskInfo = (MergeOrCutTaskInfo)redisUtil.get(keyItem);
... ...
src/main/java/top/panll/assist/utils/RedisUtil.java
1 1 package top.panll.assist.utils;
2 2  
3   -import com.alibaba.fastjson.JSONObject;
4 3 import org.springframework.beans.factory.annotation.Autowired;
5 4 import org.springframework.data.redis.core.*;
6 5 import org.springframework.stereotype.Component;
... ... @@ -721,7 +720,4 @@ public class RedisUtil {
721 720 return new ArrayList<>(resultKeys);
722 721 }
723 722  
724   - public void convertAndSend(String channel, Object msg) {
725   -// redisTemplate.convertAndSend(channel, msg);
726   - }
727 723 }
... ...
src/main/resources/application-local.yml
... ... @@ -33,6 +33,8 @@ server:
33 33  
34 34 # [根据业务需求配置]
35 35 userSettings:
  36 + # [必选 ] 服务ID
  37 + id: 334533
36 38 # [必选 ] zlm配置的录像路径
37 39 record: /home/lin/server/test001/default/www/record/
38 40 # [可选 ] 录像保存时长(单位: 天)每天晚12点自动对过期文件执行清理
... ...