cl
19 小时以前 ab08f2e28057e226ba42b3268ed36ac489cef34e
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
71
72
package com.zy.asrs.task;
 
import com.zy.asrs.mapper.ReportQueryMapper;
import com.zy.common.utils.RedisUtil;
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.HashSet;
import java.util.List;
import java.util.Set;
 
@Component
@Slf4j
public class ClearMonitorLocMapCacheScheduler {
 
    private static final String LOC_MAP_CACHE_KEY_PREFIX = "monitor:loc:map:row:";
 
    private static final String[] LOC_MAP_CACHE_PATTERNS = {
            "monitor:loc:map:*",
            "*monitor:loc:map:*",
    };
 
    @Autowired(required = false)
    private RedisUtil redisUtil;
    @Autowired(required = false)
    private ReportQueryMapper reportQueryMapper;
 
    /**
     * 每小时清理一次热力图缓存
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void clearLocMapCache() {
        if (redisUtil == null) {
            return;
        }
        try {
            Set<String> toDelete = new HashSet<>();
            for (String pattern : LOC_MAP_CACHE_PATTERNS) {
                Set<?> keys = redisUtil.keys(pattern);
                if (keys == null || keys.isEmpty()) {
                    continue;
                }
                for (Object key : keys) {
                    if (key != null) {
                        toDelete.add(String.valueOf(key));
                    }
                }
            }
            toDelete.add(LOC_MAP_CACHE_KEY_PREFIX + "1");
            if (reportQueryMapper != null) {
                List<Integer> rows = reportQueryMapper.getViewLocRowTotal();
                if (rows != null) {
                    for (Integer row : rows) {
                        if (row != null) {
                            toDelete.add(LOC_MAP_CACHE_KEY_PREFIX + row);
                        }
                    }
                }
            }
            if (toDelete.isEmpty()) {
                return;
            }
            String[] cacheKeys = toDelete.toArray(new String[0]);
            redisUtil.del(cacheKeys);
            log.info("清理库位热力图缓存数量={}", cacheKeys.length);
        } catch (Exception e) {
            log.error("清理库位热力图缓存失败", e);
        }
    }
}