All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tio.utils.cache.TimedCache Maven / Gradle / Ivy

There is a newer version: 1.0.8
Show newest version
package org.tio.utils.cache;

import org.tio.utils.timer.DefaultTimerTaskService;
import org.tio.utils.timer.TimerTask;
import org.tio.utils.timer.TimerTaskService;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 定时缓存
* 此缓存没有容量限制,对象只有在过期后才会被移除 * * @param 键类型 * @param 值类型 * @author Looly, L.cm */ public class TimedCache extends StampedCache { private static final long serialVersionUID = 1L; /** * 时间轮 */ private final transient TimerTaskService timerTaskService; /** * 正在执行的定时任务 */ private transient TimerTask timerTask; /** * 构造 * * @param timeout 超时(过期)时长,单位毫秒 */ public TimedCache(long timeout) { this(timeout, new HashMap<>()); } /** * 构造 * * @param timeout 过期时长 * @param map 存储缓存对象的map */ public TimedCache(long timeout, Map> map) { super(map, 0, timeout); this.timerTaskService = new DefaultTimerTaskService(); this.timerTaskService.start(); this.schedulePrune(timeout); } // ---------------------------------------------------------------- prune /** * 清理过期对象 * * @return 清理数 */ @Override protected int pruneCache() { int count = 0; final Iterator> values = cacheObjIter(); CacheObj co; while (values.hasNext()) { co = values.next(); if (co.isExpired()) { values.remove(); onRemove(co.key, co.obj); count++; } } return count; } // ---------------------------------------------------------------- auto prune /** * 定时清理 * * @param delay 间隔时长,单位毫秒 */ protected void schedulePrune(long delay) { this.timerTask = timerTaskService.addTask(timer -> new TimerTask(delay) { @Override public void run() { timer.add(this); TimedCache.this.pruneCache(); } }); } /** * 取消定时清理 */ public void cancelPruneSchedule() { if (null != this.timerTask) { this.timerTask.cancel(); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy