吉安感知网项目-后端
xiebin
2026-01-06 d207a86cdf1ab52ef8cb7cd83bad8fceab8038cf
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
package org.sxkj.common.cache;
 
import org.springframework.util.StringUtils;
 
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
 
/**
 * 缓存设备状态
 */
public class DeviceStatusCacheMapUtils {
 
    /**
     * 设备状态值缓存
     * Key: 用户ID, Value: 设备状态列表
     */
    private static final ConcurrentHashMap<String, StatusBo> CACHE_MAP = new ConcurrentHashMap<>();
 
 
    //最大多少秒刷新数据
    private static final Integer MAX_SECONDS = 30;
 
 
    public static void put(String deviceSn, String status) {
        StatusBo statusBo = new StatusBo();
        statusBo.setKey(deviceSn);
        statusBo.setTime(System.currentTimeMillis());
        statusBo.setValue(status);
        CACHE_MAP.put(deviceSn, statusBo);
    }
 
    /**
     * @param intervalTime 过期时间
     * @return key value
     */
    public static Integer getDeviceStatusCache(String sn, Integer intervalTime) {
        StatusBo statusBo = CACHE_MAP.get(sn);
        if (StringUtils.isEmpty(sn) || Objects.isNull(statusBo)) {
            return 0;
        }
        long timeDiff = Math.abs(System.currentTimeMillis() - statusBo.getTime());
        boolean timeOut = timeDiff >= Optional.ofNullable(intervalTime).orElse(MAX_SECONDS) * 1000L;
        if (timeOut) {
            return 0;
        } else {
            return Integer.valueOf(statusBo.getValue());
        }
    }
 
}