add method

This commit is contained in:
looly 2021-12-08 11:25:45 +08:00
parent 5009ee6de8
commit abec19e5cd
2 changed files with 53 additions and 8 deletions

View File

@ -3,7 +3,7 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.7.17 (2021-12-07) # 5.7.17 (2021-12-08)
### 🐣新特性 ### 🐣新特性
* 【core 】 增加AsyncUtilpr#457@Gitee * 【core 】 增加AsyncUtilpr#457@Gitee
@ -34,6 +34,7 @@
* 【http 】 增加HttpGlobalConfig.setBoundary删除MultipartBody.BOUNDARY和getContentTypeissue#I4KSLY@Gitee * 【http 】 增加HttpGlobalConfig.setBoundary删除MultipartBody.BOUNDARY和getContentTypeissue#I4KSLY@Gitee
* 【core 】 DateTime增加setMinimalDaysInFirstWeekissue#1988@Github * 【core 】 DateTime增加setMinimalDaysInFirstWeekissue#1988@Github
* 【db 】 Db增加query重载可支持自定义PreparedStatement从而支持游标issue#I4JXWN@Gitee * 【db 】 Db增加query重载可支持自定义PreparedStatement从而支持游标issue#I4JXWN@Gitee
* 【cache 】 CacheObj增加getExpiredTime等方法issue#I4LE80@Gitee
* *
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复FileResource构造fileName参数无效问题issue#1942@Github * 【core 】 修复FileResource构造fileName参数无效问题issue#1942@Github

View File

@ -1,26 +1,35 @@
package cn.hutool.cache.impl; package cn.hutool.cache.impl;
import cn.hutool.core.date.DateUtil;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
/** /**
* 缓存对象 * 缓存对象
* @author Looly
* *
* @param <K> Key类型 * @param <K> Key类型
* @param <V> Value类型 * @param <V> Value类型
* @author Looly
*/ */
public class CacheObj<K, V> implements Serializable{ public class CacheObj<K, V> implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
protected final K key; protected final K key;
protected final V obj; protected final V obj;
/** 上次访问时间 */ /**
* 上次访问时间
*/
protected volatile long lastAccess; protected volatile long lastAccess;
/** 访问次数 */ /**
* 访问次数
*/
protected AtomicLong accessCount = new AtomicLong(); protected AtomicLong accessCount = new AtomicLong();
/** 对象存活时长0表示永久存活*/ /**
* 对象存活时长0表示永久存活
*/
protected final long ttl; protected final long ttl;
/** /**
@ -39,6 +48,7 @@ public class CacheObj<K, V> implements Serializable{
/** /**
* 获取键 * 获取键
*
* @return * @return
* @since 4.0.10 * @since 4.0.10
*/ */
@ -48,6 +58,7 @@ public class CacheObj<K, V> implements Serializable{
/** /**
* 获取值 * 获取值
*
* @return * @return
* @since 4.0.10 * @since 4.0.10
*/ */
@ -55,6 +66,39 @@ public class CacheObj<K, V> implements Serializable{
return this.obj; 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 @Override
public String toString() { public String toString() {
return "CacheObj [key=" + key + ", obj=" + obj + ", lastAccess=" + lastAccess + ", accessCount=" + accessCount + ", ttl=" + ttl + "]"; return "CacheObj [key=" + key + ", obj=" + obj + ", lastAccess=" + lastAccess + ", accessCount=" + accessCount + ", ttl=" + ttl + "]";
@ -66,7 +110,7 @@ public class CacheObj<K, V> implements Serializable{
* @return 是否过期 * @return 是否过期
*/ */
protected boolean isExpired() { protected boolean isExpired() {
if(this.ttl > 0) { if (this.ttl > 0) {
// 此处不考虑时间回拨 // 此处不考虑时间回拨
return (System.currentTimeMillis() - this.lastAccess) > this.ttl; return (System.currentTimeMillis() - this.lastAccess) > this.ttl;
} }
@ -81,7 +125,7 @@ public class CacheObj<K, V> implements Serializable{
* @since 4.0.10 * @since 4.0.10
*/ */
protected V get(boolean isUpdateLastAccess) { protected V get(boolean isUpdateLastAccess) {
if(isUpdateLastAccess) { if (isUpdateLastAccess) {
lastAccess = System.currentTimeMillis(); lastAccess = System.currentTimeMillis();
} }
accessCount.getAndIncrement(); accessCount.getAndIncrement();