package com.qiwu.inference.common.cache; import com.alibaba.fastjson.JSON; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.stereotype.Component; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; /** * 过期缓存检测线程 * */ @EnableAsync @Component public class ExpireThread implements Runnable{ @Override public void run() { while (true) { try { //每60秒检测一次 TimeUnit.SECONDS.sleep(60); expireCache(); } catch (Exception e) { e.printStackTrace(); } } } /** * 清除全局缓存对象中过期缓存 */ public void expireCache() { for (String key : CacheGlobal.concurrentMap.keySet()) { MyCache myCache = CacheGlobal.concurrentMap.get(key); long timeoutTime = System.currentTimeMillis() - myCache.getWriteTime(); if (timeoutTime < myCache.getExpireTime()) { continue; } //清除过期缓存 CacheGlobal.concurrentMap.remove(key); } } }