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 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 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); } } }