diff --git a/hutool-cache/src/main/java/cn/hutool/cache/Cache.java b/hutool-cache/src/main/java/cn/hutool/cache/Cache.java index 19f93046c..aff79df90 100755 --- a/hutool-cache/src/main/java/cn/hutool/cache/Cache.java +++ b/hutool-cache/src/main/java/cn/hutool/cache/Cache.java @@ -186,4 +186,21 @@ public interface Cache extends Iterable, Serializable { default Cache setListener(CacheListener listener){ return this; } + + /** + * 获取缓存对象 + * + * @param key 键 + * @return 值或null + */ + CacheObj getObj(K key); + + /** + * 获取缓存对象 + * + * @param isUpdateLastAccess 是否更新最后访问时间 + * @param key 键 + * @return 值或null + */ + CacheObj getObj(K key, boolean isUpdateLastAccess); } diff --git a/hutool-cache/src/main/java/cn/hutool/cache/impl/NoCache.java b/hutool-cache/src/main/java/cn/hutool/cache/impl/NoCache.java index 96bc03b98..dc1848847 100755 --- a/hutool-cache/src/main/java/cn/hutool/cache/impl/NoCache.java +++ b/hutool-cache/src/main/java/cn/hutool/cache/impl/NoCache.java @@ -120,4 +120,13 @@ public class NoCache implements Cache { return false; } + @Override + public CacheObj getObj(K key) { + return null; + } + + @Override + public CacheObj getObj(K key, boolean isUpdateLastAccess) { + return null; + } } diff --git a/hutool-cache/src/main/java/cn/hutool/cache/impl/ReentrantCache.java b/hutool-cache/src/main/java/cn/hutool/cache/impl/ReentrantCache.java index 51e044b1c..9d0e93315 100755 --- a/hutool-cache/src/main/java/cn/hutool/cache/impl/ReentrantCache.java +++ b/hutool-cache/src/main/java/cn/hutool/cache/impl/ReentrantCache.java @@ -105,11 +105,27 @@ public abstract class ReentrantCache extends AbstractCache { * @return 值或null */ private V getOrRemoveExpired(final K key, final boolean isUpdateLastAccess, final boolean isUpdateCount) { + CacheObj co = getOrRemoveExpiredObj(key, isUpdateLastAccess, isUpdateCount); + if (co != null) { + return co.get(isUpdateLastAccess); + } + return null; + } + + /** + * 获得值或清除过期值 + * + * @param key 键 + * @param isUpdateLastAccess 是否更新最后访问时间 + * @param isUpdateCount 是否更新计数器 + * @return 值或null + */ + private CacheObj getOrRemoveExpiredObj(final K key, final boolean isUpdateLastAccess, final boolean isUpdateCount) { CacheObj co; lock.lock(); try { co = getWithoutLock(key); - if(null != co && co.isExpired()){ + if (null != co && co.isExpired()) { //过期移除 removeWithoutLock(key); onRemove(co.key, co.obj); @@ -121,15 +137,38 @@ public abstract class ReentrantCache extends AbstractCache { // 未命中 if (null == co) { - if(isUpdateCount){ + if (isUpdateCount) { missCount.increment(); } return null; } - if(isUpdateCount){ + if (isUpdateCount) { hitCount.increment(); } - return co.get(isUpdateLastAccess); + return co; + } + + /** + * 获取缓存对象 + * + * @param key 键 + * @return 值或null + */ + @Override + public CacheObj getObj(K key) { + return getOrRemoveExpiredObj(key, false, true); + } + + /** + * 获取缓存对象 + * + * @param isUpdateLastAccess 是否更新最后访问时间 + * @param key 键 + * @return 值或null + */ + @Override + public CacheObj getObj(K key, boolean isUpdateLastAccess) { + return getOrRemoveExpiredObj(key, isUpdateLastAccess, true); } }