From abec19e5cddbb0196a5bc61bc955d5882aa3205e Mon Sep 17 00:00:00 2001 From: looly Date: Wed, 8 Dec 2021 11:25:45 +0800 Subject: [PATCH] add method --- CHANGELOG.md | 3 +- .../java/cn/hutool/cache/impl/CacheObj.java | 58 ++++++++++++++++--- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1a2644c7..2e6644bd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.7.17 (2021-12-07) +# 5.7.17 (2021-12-08) ### 🐣新特性 * 【core 】 增加AsyncUtil(pr#457@Gitee) @@ -34,6 +34,7 @@ * 【http 】 增加HttpGlobalConfig.setBoundary,删除MultipartBody.BOUNDARY和getContentType(issue#I4KSLY@Gitee) * 【core 】 DateTime增加setMinimalDaysInFirstWeek(issue#1988@Github) * 【db 】 Db增加query重载,可支持自定义PreparedStatement,从而支持游标(issue#I4JXWN@Gitee) +* 【cache 】 CacheObj增加getExpiredTime等方法(issue#I4LE80@Gitee) * ### 🐞Bug修复 * 【core 】 修复FileResource构造fileName参数无效问题(issue#1942@Github) diff --git a/hutool-cache/src/main/java/cn/hutool/cache/impl/CacheObj.java b/hutool-cache/src/main/java/cn/hutool/cache/impl/CacheObj.java index dc203cb9c..79e19442a 100644 --- a/hutool-cache/src/main/java/cn/hutool/cache/impl/CacheObj.java +++ b/hutool-cache/src/main/java/cn/hutool/cache/impl/CacheObj.java @@ -1,26 +1,35 @@ package cn.hutool.cache.impl; +import cn.hutool.core.date.DateUtil; + import java.io.Serializable; +import java.util.Date; import java.util.concurrent.atomic.AtomicLong; /** * 缓存对象 - * @author Looly * * @param Key类型 * @param Value类型 + * @author Looly */ -public class CacheObj implements Serializable{ +public class CacheObj implements Serializable { private static final long serialVersionUID = 1L; protected final K key; protected final V obj; - /** 上次访问时间 */ + /** + * 上次访问时间 + */ protected volatile long lastAccess; - /** 访问次数 */ + /** + * 访问次数 + */ protected AtomicLong accessCount = new AtomicLong(); - /** 对象存活时长,0表示永久存活*/ + /** + * 对象存活时长,0表示永久存活 + */ protected final long ttl; /** @@ -39,6 +48,7 @@ public class CacheObj implements Serializable{ /** * 获取键 + * * @return 键 * @since 4.0.10 */ @@ -48,6 +58,7 @@ public class CacheObj implements Serializable{ /** * 获取值 + * * @return 值 * @since 4.0.10 */ @@ -55,6 +66,39 @@ public class CacheObj implements Serializable{ return this.obj; } + /** + * 获取对象存活时长,即超时总时长,0表示无限 + * + * @return 对象存活时长 + * @since 5.7.17 + */ + public long getTtl() { + return this.ttl; + } + + /** + * 获取过期时间,返回{@code null}表示永不过期 + * + * @return 此对象的过期时间,返回{@code null}表示永不过期 + * @since 5.7.17 + */ + public Date getExpiredTime(){ + if(this.ttl > 0){ + return DateUtil.date(this.lastAccess + this.ttl); + } + return null; + } + + /** + * 获取上次访问时间 + * + * @return 上次访问时间 + * @since 5.7.17 + */ + public long getLastAccess() { + return this.lastAccess; + } + @Override public String toString() { return "CacheObj [key=" + key + ", obj=" + obj + ", lastAccess=" + lastAccess + ", accessCount=" + accessCount + ", ttl=" + ttl + "]"; @@ -66,7 +110,7 @@ public class CacheObj implements Serializable{ * @return 是否过期 */ protected boolean isExpired() { - if(this.ttl > 0) { + if (this.ttl > 0) { // 此处不考虑时间回拨 return (System.currentTimeMillis() - this.lastAccess) > this.ttl; } @@ -81,7 +125,7 @@ public class CacheObj implements Serializable{ * @since 4.0.10 */ protected V get(boolean isUpdateLastAccess) { - if(isUpdateLastAccess) { + if (isUpdateLastAccess) { lastAccess = System.currentTimeMillis(); } accessCount.getAndIncrement();