huangzhen
2023-10-19 1f69ad0de4a0abf9b50599146d8f9b6a22e803a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.gkhy.fourierSpecialGasMonitor.schedule;
 
import com.alibaba.fastjson.JSON;
import com.gkhy.fourierSpecialGasMonitor.entity.GasConcentration;
import com.gkhy.fourierSpecialGasMonitor.entity.GasFlux;
import com.gkhy.fourierSpecialGasMonitor.entity.req.UploadGasConcentrationReqDTO;
import com.gkhy.fourierSpecialGasMonitor.entity.req.UploadGasFluxReqDTO;
import com.gkhy.fourierSpecialGasMonitor.service.DataReceiveService;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
 
/**
 * @author Mr.huang
 * @decription
 * @date 2023/8/7 15:30
 */
@Component
public class GasConcentrationAutoCreateKeySchedule {
 
    @Autowired
    private RedissonClient redissonClient;
 
    @Autowired
    private DataReceiveService dataReceiveService;
 
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
 
    private static String gasConcentrationCachePrefix = "gas_concentration_cache_";
 
    @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨执行
    @Async(value = "SocketTaskExecutor")
    public void createRedisKey() {
        LocalDateTime time = LocalDateTime.now();
        String cacheName = time.format(formatter);
        List<GasConcentration> gasConcentrations = new ArrayList<>();
        String jsonString = JSON.toJSONString(gasConcentrations);
        RBucket<String> bucket = redissonClient.getBucket(gasConcentrationCachePrefix+cacheName);
        bucket.set(jsonString);
 
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            logger.info("自动清除历史气体对照表缓存key失败");
        }
        //清除前一天的缓存
        LocalDateTime yesterday = LocalDateTime.now().minusDays(1);
        String yesterdayCache = yesterday.format(formatter);
        RBucket<String> yesterdayBucket = redissonClient.getBucket(gasConcentrationCachePrefix+yesterdayCache);
        if (yesterdayBucket.isExists()) {
            yesterdayBucket.delete();
        }
    }
}