From 4240bc65231f979843ac346a5ef8e41cd05d4716 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 18 Apr 2024 16:34:48 +0800 Subject: [PATCH] =?UTF-8?q?CacheUtil.newTimedCache=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=9C=89schedulePruneDelay=E5=8F=82=E6=95=B0=E7=9A=84=E9=87=8D?= =?UTF-8?q?=E8=BD=BD=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dromara/hutool/core/cache/CacheUtil.java | 15 +++++++++++++++ .../hutool/core/cache/impl/TimedCache.java | 4 +++- 2 files changed, 18 insertions(+), 1 deletion(-) 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; } /**