直接上代码
package com.dyh.utils;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.util.PriorityQueue;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;
public class LocalCache {
private static ScheduledExecutorService swapExpiredPool
= new ScheduledThreadPoolExecutor(10);
private ReentrantLock lock = new ReentrantLock();
private ConcurrentHashMap<String, Node> cache = new ConcurrentHashMap<>(1024);
/**
* 让过期时间最小的数据排在队列前,在清除过期数据时
* ,只需查看缓存最近的过期数据,而不用扫描全部缓存
*
* @see Node#compareTo(Node)
* @see SwapExpiredNodeWork#run()
*/
private PriorityQueue<Node> expireQueue = new PriorityQueue<>(1024);
public LocalCache() {
//使用默认的线程池,每5秒清除一次过期数据
//线程池和调用频率 最好是交给调用者去设置。
swapExpiredPool.scheduleWithFixedDelay(