java 缓存 过期_java 基于内存 轻量级 非阻塞 时效(可过期)缓存类

/** * 轻量级非阻塞 延迟,定时,过期缓存类
* v2.0
* 1.更变为 非阻塞
* 2.修改删除过期value 策略
* 4.修复一个客户端代码不同步的可能
* 3.其他优化 * * @author lijinhua * @version 2.0 * @param * @param */ public class MyCache { /* * 非阻塞 延迟,定时,过期缓存类 */ private long expireTime = 10; Map map = new ConcurrentHashMap(); final ScheduledExecutorService exec = Executors.newScheduledThreadPool(1); private int maxNum = 99999; private AtomicInteger hits = new AtomicInteger(); public int getMaxNum() { return maxNum; } public void setMaxNum(int maxNum) { this.maxNum = maxNum; } // private Object expireKey = null; /** * 创建一个默认的缓存类, 失效时间为10分钟,缓存大小为10000 */ public MyCache() { super(); exec.scheduleWithFixedDelay(new cleanTask(), 0, expireTime, TimeUnit.MINUTES); } // /** // * 创建一个最大缓存N的缓存类,失效时间为10分钟。 // * @param maxNum 最大缓存 // */ // public MyCache(int maxNum) { // super(); // this.maxNum=maxNum; // exec.scheduleWithFixedDelay(new cleanTask(), 0, expireTime, // TimeUnit.MINUTES); // } // /** // * 创建一个最大缓存N的缓存类,失效时间为n分钟。 // * @param maxNum 最大缓存 // * @param expireTime 失效分钟 // */ // public MyCache(int maxNum,long expireTime) { // super(); // this.maxNum=maxNum; // this.expireTime=expireTime; // exec.scheduleWithFixedDelay(new cleanTask(), 0, expireTime, // TimeUnit.MINUTES); // } /** * 生成一个共用 超时时间的缓存类 expireTime :超时分钟 * * @param expireTime * 超时分钟 */ public MyCache(long expireTime) { super(); this.expireTime = expireTime; exec.scheduleWithFixedDelay(new cleanTask(), 0, expireTime, TimeUnit.MINUTES); } public boolean containsKey(K key) { // if (expireKey != null && key == expireKey) { // return false; // } Cache cache = map.get(key); if (cache != null && (System.currentTimeMillis() - cache.getExpireTime() > 0)) { // 已过期 return false; } else if (cache != null && (System.currentTimeMillis() - cache.getExpireTime() < 0)) { return true; } return false; // 不存在 } public V getCache(K key) { // if (expireKey != null && key == expireKey) { // return null; // } try { Cache cache = map.get(key); if (System.currentTimeMillis() - cache.getExpireTime() > 0) { // expireKey = key; return null; } if (hits.get() >= Integer.MAX_VALUE) { hits.set(0); } hits.incrementAndGet(); return cache.getValue(); } catch (Exception e) { return null; } } /** * * @param key * @param value */ public void putCache(K key, V value) { save(key, value, expireTime); } public int size() { return map.size(); } public void remove(Object key) { map.remove(key); } public void clear() { map.clear(); } public int getHits() { return hits.get(); } /** * * @param key * @param value * @param expireTime * 超时分钟 */ public void putCache(K key, V value, long expireTime) { save(key, value, expireTime); } private void save(K key, V value, long expireTime) { if (map.size() > maxNum) { return; } map.put(key, new Cache(value, expireTime)); } private class Cache { private V value; /** * 传入的是分钟,要转成毫秒。 ms */ private long expireTime; public Cache(V value, long expireTime) { this.value = value; this.expireTime = System.currentTimeMillis() + (expireTime * 60 * 1000); } public V getValue() { return value; } public long getExpireTime() { return expireTime; } } class cleanTask implements Runnable { public void run() { System.out.println(" cleanbuff task is run."); cleanExpire(); // expireKey = null; } // notsafe private void cleanExpire() { long now = System.currentTimeMillis(); Set keys = map.keySet(); for (K key : keys) { long expireTime = map.get(key).getExpireTime(); if (now - expireTime > 0) { // expireKey = key; map.remove(key); } } } } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值