diff --git a/CHANGELOG.md b/CHANGELOG.md index 04b523e3f..334c8a47c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * 【core 】 增加TransXXX(issue#I1TU1Y@Gitee) * 【core 】 增加Generator * 【db 】 Column增加是否主键、保留位数等字段 +* 【cache 】 Cache接口增加get重载(issue#1080@Github) ### Bug修复 * 【core 】 修复Dict.of错误(issue#I1UUO5@Gitee) 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 52bc97a83..1478b192e 100644 --- a/hutool-cache/src/main/java/cn/hutool/cache/Cache.java +++ b/hutool-cache/src/main/java/cn/hutool/cache/Cache.java @@ -1,39 +1,38 @@ package cn.hutool.cache; -import java.io.Serializable; -import java.util.Iterator; - import cn.hutool.cache.impl.CacheObj; import cn.hutool.core.lang.func.Func0; +import java.io.Serializable; +import java.util.Iterator; + /** * 缓存接口 - * - * @author Looly,jodd * * @param 键类型 * @param 值类型 + * @author Looly, jodd */ public interface Cache extends Iterable, Serializable { /** * 返回缓存容量,0表示无大小限制 - * + * * @return 返回缓存容量,0表示无大小限制 */ int capacity(); /** * 缓存失效时长, 0 表示没有设置,单位毫秒 - * + * * @return 缓存失效时长, 0 表示没有设置,单位毫秒 */ long timeout(); /** * 将对象加入到缓存,使用默认失效时长 - * - * @param key 键 + * + * @param key 键 * @param object 缓存的对象 * @see Cache#put(Object, Object, long) */ @@ -42,9 +41,9 @@ public interface Cache extends Iterable, Serializable { /** * 将对象加入到缓存,使用指定失效时长
* 如果缓存空间满了,{@link #prune()} 将被调用以获得空间来存放新对象 - * - * @param key 键 - * @param object 缓存的对象 + * + * @param key 键 + * @param object 缓存的对象 * @param timeout 失效时长,单位毫秒 * @see Cache#put(Object, Object, long) */ @@ -56,28 +55,50 @@ public interface Cache extends Iterable, Serializable { * 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回null,否则返回值。 *

* 每次调用此方法会刷新最后访问时间,也就是说会重新计算超时时间。 - * + * * @param key 键 * @return 键对应的对象 * @see #get(Object, boolean) */ - V get(K key); - + default V get(K key) { + return get(key, true); + } + /** * 从缓存中获得对象,当对象不在缓存中或已经过期返回Func0回调产生的对象 - * - * @param key 键 + *

+ * 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回null,否则返回值。 + *

+ * 每次调用此方法会刷新最后访问时间,也就是说会重新计算超时时间。 + * + * @param key 键 * @param supplier 如果不存在回调方法,用于生产值对象 * @return 值对象 */ - V get(K key, Func0 supplier); + default V get(K key, Func0 supplier) { + return get(key, true, supplier); + } + + /** + * 从缓存中获得对象,当对象不在缓存中或已经过期返回Func0回调产生的对象 + *

+ * 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回null,否则返回值。 + *

+ * 每次调用此方法会刷新最后访问时间,也就是说会重新计算超时时间。 + * + * @param key 键 + * @param isUpdateLastAccess 是否更新最后访问时间,即重新计算超时时间。 + * @param supplier 如果不存在回调方法,用于生产值对象 + * @return 值对象 + */ + V get(K key, boolean isUpdateLastAccess, Func0 supplier); /** * 从缓存中获得对象,当对象不在缓存中或已经过期返回null *

* 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回null,否则返回值。 - * - * @param key 键 + * + * @param key 键 * @param isUpdateLastAccess 是否更新最后访问时间,即重新计算超时时间。 * @return 键对应的对象 */ @@ -85,7 +106,7 @@ public interface Cache extends Iterable, Serializable { /** * 返回包含键和值得迭代器 - * + * * @return 缓存对象迭代器 * @since 4.0.10 */ @@ -93,21 +114,21 @@ public interface Cache extends Iterable, Serializable { /** * 从缓存中清理过期对象,清理策略取决于具体实现 - * + * * @return 清理的缓存对象个数 */ int prune(); /** * 缓存是否已满,仅用于有空间限制的缓存对象 - * + * * @return 缓存是否已满,仅用于有空间限制的缓存对象 */ boolean isFull(); /** * 从缓存中移除对象 - * + * * @param key 键 */ void remove(K key); @@ -119,21 +140,21 @@ public interface Cache extends Iterable, Serializable { /** * 缓存的对象数量 - * + * * @return 缓存的对象数量 */ int size(); /** * 缓存是否为空 - * + * * @return 缓存是否为空 */ boolean isEmpty(); /** * 是否包含key - * + * * @param key KEY * @return 是否包含key */ diff --git a/hutool-cache/src/main/java/cn/hutool/cache/impl/AbstractCache.java b/hutool-cache/src/main/java/cn/hutool/cache/impl/AbstractCache.java index 464ab44b7..f46009980 100644 --- a/hutool-cache/src/main/java/cn/hutool/cache/impl/AbstractCache.java +++ b/hutool-cache/src/main/java/cn/hutool/cache/impl/AbstractCache.java @@ -125,13 +125,8 @@ public abstract class AbstractCache implements Cache { } @Override - public V get(K key) { - return get(key, true); - } - - @Override - public V get(K key, Func0 supplier) { - V v = get(key); + public V get(K key, boolean isUpdateLastAccess, Func0 supplier) { + V v = get(key, isUpdateLastAccess); if (null == v && null != supplier) { final long stamp = lock.writeLock(); try { 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 55d6ec896..7814d9679 100644 --- a/hutool-cache/src/main/java/cn/hutool/cache/impl/NoCache.java +++ b/hutool-cache/src/main/java/cn/hutool/cache/impl/NoCache.java @@ -52,6 +52,11 @@ public class NoCache implements Cache { @Override public V get(K key, Func0 supplier) { + return get(key, true, supplier); + } + + @Override + public V get(K key, boolean isUpdateLastAccess, Func0 supplier) { try { return (null == supplier) ? null : supplier.call(); } catch (Exception e) {