diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/cache/CacheUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/cache/CacheUtil.java index b9aa2c8f4..252c3bedc 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/cache/CacheUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/cache/CacheUtil.java @@ -102,6 +102,21 @@ public class CacheUtil { return new LRUCache<>(capacity); } + /** + * 创建定时缓存,通过定时任务自动清除过期缓存对象 + * + * @param Key类型 + * @param Value类型 + * @param timeout 过期时长,单位:毫秒 + * @param schedulePruneDelay 间隔时长,单位毫秒 + * @return {@link TimedCache} + * @since 5.8.28 + */ + public static TimedCache newTimedCache(final long timeout, final long schedulePruneDelay) { + final TimedCache cache = newTimedCache(timeout); + return cache.schedulePrune(schedulePruneDelay); + } + /** * 创建定时缓存. * diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/cache/impl/TimedCache.java b/hutool-core/src/main/java/org/dromara/hutool/core/cache/impl/TimedCache.java index ab9343485..e7e15ba1d 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/cache/impl/TimedCache.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/cache/impl/TimedCache.java @@ -83,9 +83,11 @@ public class TimedCache extends StampedCache { * 定时清理 * * @param delay 间隔时长,单位毫秒 + * @return this */ - public void schedulePrune(final long delay) { + public TimedCache schedulePrune(final long delay) { this.pruneJobFuture = GlobalPruneTimer.INSTANCE.schedule(this::prune, delay); + return this; } /**