add method

This commit is contained in:
Looly
2020-09-15 15:17:42 +08:00
parent 37b051a46c
commit b2e1b903ab
4 changed files with 56 additions and 34 deletions

View File

@@ -14,6 +14,7 @@
* 【core 】 增加TransXXXissue#I1TU1Y@Gitee * 【core 】 增加TransXXXissue#I1TU1Y@Gitee
* 【core 】 增加Generator * 【core 】 增加Generator
* 【db 】 Column增加是否主键、保留位数等字段 * 【db 】 Column增加是否主键、保留位数等字段
* 【cache 】 Cache接口增加get重载issue#1080@Github
### Bug修复 ### Bug修复
* 【core 】 修复Dict.of错误issue#I1UUO5@Gitee * 【core 】 修复Dict.of错误issue#I1UUO5@Gitee

View File

@@ -1,39 +1,38 @@
package cn.hutool.cache; package cn.hutool.cache;
import java.io.Serializable;
import java.util.Iterator;
import cn.hutool.cache.impl.CacheObj; import cn.hutool.cache.impl.CacheObj;
import cn.hutool.core.lang.func.Func0; import cn.hutool.core.lang.func.Func0;
import java.io.Serializable;
import java.util.Iterator;
/** /**
* 缓存接口 * 缓存接口
*
* @author Looly,jodd
* *
* @param <K> 键类型 * @param <K> 键类型
* @param <V> 值类型 * @param <V> 值类型
* @author Looly, jodd
*/ */
public interface Cache<K, V> extends Iterable<V>, Serializable { public interface Cache<K, V> extends Iterable<V>, Serializable {
/** /**
* 返回缓存容量,<code>0</code>表示无大小限制 * 返回缓存容量,<code>0</code>表示无大小限制
* *
* @return 返回缓存容量,<code>0</code>表示无大小限制 * @return 返回缓存容量,<code>0</code>表示无大小限制
*/ */
int capacity(); int capacity();
/** /**
* 缓存失效时长, <code>0</code> 表示没有设置,单位毫秒 * 缓存失效时长, <code>0</code> 表示没有设置,单位毫秒
* *
* @return 缓存失效时长, <code>0</code> 表示没有设置,单位毫秒 * @return 缓存失效时长, <code>0</code> 表示没有设置,单位毫秒
*/ */
long timeout(); long timeout();
/** /**
* 将对象加入到缓存,使用默认失效时长 * 将对象加入到缓存,使用默认失效时长
* *
* @param key 键 * @param key
* @param object 缓存的对象 * @param object 缓存的对象
* @see Cache#put(Object, Object, long) * @see Cache#put(Object, Object, long)
*/ */
@@ -42,9 +41,9 @@ public interface Cache<K, V> extends Iterable<V>, Serializable {
/** /**
* 将对象加入到缓存,使用指定失效时长<br> * 将对象加入到缓存,使用指定失效时长<br>
* 如果缓存空间满了,{@link #prune()} 将被调用以获得空间来存放新对象 * 如果缓存空间满了,{@link #prune()} 将被调用以获得空间来存放新对象
* *
* @param key 键 * @param key
* @param object 缓存的对象 * @param object 缓存的对象
* @param timeout 失效时长,单位毫秒 * @param timeout 失效时长,单位毫秒
* @see Cache#put(Object, Object, long) * @see Cache#put(Object, Object, long)
*/ */
@@ -56,28 +55,50 @@ public interface Cache<K, V> extends Iterable<V>, Serializable {
* 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回<code>null</code>,否则返回值。 * 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回<code>null</code>,否则返回值。
* <p> * <p>
* 每次调用此方法会刷新最后访问时间,也就是说会重新计算超时时间。 * 每次调用此方法会刷新最后访问时间,也就是说会重新计算超时时间。
* *
* @param key 键 * @param key 键
* @return 键对应的对象 * @return 键对应的对象
* @see #get(Object, boolean) * @see #get(Object, boolean)
*/ */
V get(K key); default V get(K key) {
return get(key, true);
}
/** /**
* 从缓存中获得对象当对象不在缓存中或已经过期返回Func0回调产生的对象 * 从缓存中获得对象当对象不在缓存中或已经过期返回Func0回调产生的对象
* * <p>
* @param key 键 * 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回<code>null</code>,否则返回值。
* <p>
* 每次调用此方法会刷新最后访问时间,也就是说会重新计算超时时间。
*
* @param key 键
* @param supplier 如果不存在回调方法,用于生产值对象 * @param supplier 如果不存在回调方法,用于生产值对象
* @return 值对象 * @return 值对象
*/ */
V get(K key, Func0<V> supplier); default V get(K key, Func0<V> supplier) {
return get(key, true, supplier);
}
/**
* 从缓存中获得对象当对象不在缓存中或已经过期返回Func0回调产生的对象
* <p>
* 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回<code>null</code>,否则返回值。
* <p>
* 每次调用此方法会刷新最后访问时间,也就是说会重新计算超时时间。
*
* @param key 键
* @param isUpdateLastAccess 是否更新最后访问时间,即重新计算超时时间。
* @param supplier 如果不存在回调方法,用于生产值对象
* @return 值对象
*/
V get(K key, boolean isUpdateLastAccess, Func0<V> supplier);
/** /**
* 从缓存中获得对象,当对象不在缓存中或已经过期返回<code>null</code> * 从缓存中获得对象,当对象不在缓存中或已经过期返回<code>null</code>
* <p> * <p>
* 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回<code>null</code>,否则返回值。 * 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回<code>null</code>,否则返回值。
* *
* @param key 键 * @param key
* @param isUpdateLastAccess 是否更新最后访问时间,即重新计算超时时间。 * @param isUpdateLastAccess 是否更新最后访问时间,即重新计算超时时间。
* @return 键对应的对象 * @return 键对应的对象
*/ */
@@ -85,7 +106,7 @@ public interface Cache<K, V> extends Iterable<V>, Serializable {
/** /**
* 返回包含键和值得迭代器 * 返回包含键和值得迭代器
* *
* @return 缓存对象迭代器 * @return 缓存对象迭代器
* @since 4.0.10 * @since 4.0.10
*/ */
@@ -93,21 +114,21 @@ public interface Cache<K, V> extends Iterable<V>, Serializable {
/** /**
* 从缓存中清理过期对象,清理策略取决于具体实现 * 从缓存中清理过期对象,清理策略取决于具体实现
* *
* @return 清理的缓存对象个数 * @return 清理的缓存对象个数
*/ */
int prune(); int prune();
/** /**
* 缓存是否已满,仅用于有空间限制的缓存对象 * 缓存是否已满,仅用于有空间限制的缓存对象
* *
* @return 缓存是否已满,仅用于有空间限制的缓存对象 * @return 缓存是否已满,仅用于有空间限制的缓存对象
*/ */
boolean isFull(); boolean isFull();
/** /**
* 从缓存中移除对象 * 从缓存中移除对象
* *
* @param key 键 * @param key 键
*/ */
void remove(K key); void remove(K key);
@@ -119,21 +140,21 @@ public interface Cache<K, V> extends Iterable<V>, Serializable {
/** /**
* 缓存的对象数量 * 缓存的对象数量
* *
* @return 缓存的对象数量 * @return 缓存的对象数量
*/ */
int size(); int size();
/** /**
* 缓存是否为空 * 缓存是否为空
* *
* @return 缓存是否为空 * @return 缓存是否为空
*/ */
boolean isEmpty(); boolean isEmpty();
/** /**
* 是否包含key * 是否包含key
* *
* @param key KEY * @param key KEY
* @return 是否包含key * @return 是否包含key
*/ */

View File

@@ -125,13 +125,8 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
} }
@Override @Override
public V get(K key) { public V get(K key, boolean isUpdateLastAccess, Func0<V> supplier) {
return get(key, true); V v = get(key, isUpdateLastAccess);
}
@Override
public V get(K key, Func0<V> supplier) {
V v = get(key);
if (null == v && null != supplier) { if (null == v && null != supplier) {
final long stamp = lock.writeLock(); final long stamp = lock.writeLock();
try { try {

View File

@@ -52,6 +52,11 @@ public class NoCache<K, V> implements Cache<K, V> {
@Override @Override
public V get(K key, Func0<V> supplier) { public V get(K key, Func0<V> supplier) {
return get(key, true, supplier);
}
@Override
public V get(K key, boolean isUpdateLastAccess, Func0<V> supplier) {
try { try {
return (null == supplier) ? null : supplier.call(); return (null == supplier) ? null : supplier.call();
} catch (Exception e) { } catch (Exception e) {