#
Junjie
2025-11-18 42fd6bd4095414f7a78a2bb5d2c692c04119da2c
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
package com.zy.core.task;
 
 
import com.zy.asrs.entity.DeviceDataLog;
import com.zy.asrs.service.DeviceDataLogService;
import com.zy.common.utils.RedisUtil;
import com.zy.core.enums.RedisKeyType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
 
@Slf4j
@Component
public class DeviceLogScheduler {
 
    @Autowired
    private DeviceDataLogService deviceDataLogService;
    @Autowired
    private RedisUtil redisUtil;
 
    @Scheduled(cron = "0/3 * * * * ? ")
    public void execute() {
        int maxCount = 100;
        Set<String> keys = redisUtil.scanKeys(RedisKeyType.DEVICE_LOG_KEY.key, maxCount);
        if (keys == null || keys.isEmpty()) {
            return;
        }
        List<Object> values = redisUtil.multiGet(keys);
        List<DeviceDataLog> list = new ArrayList<>();
        for (Object object : values) {
            if (object instanceof DeviceDataLog) {
                list.add((DeviceDataLog) object);
            }
        }
        if (!list.isEmpty()) {
            if (deviceDataLogService.saveBatch(list)) {
                redisUtil.del(keys.toArray(new String[0]));
            }
        }
    }
 
}