This commit is contained in:
Looly 2019-10-17 11:26:19 +08:00
parent 1b3a19f07e
commit 06e8e05051
4 changed files with 29 additions and 31 deletions

View File

@ -24,7 +24,7 @@ public class CacheUtil {
* @return {@link FIFOCache}
*/
public static <K, V> FIFOCache<K, V> newFIFOCache(int capacity, long timeout){
return new FIFOCache<K, V>(capacity, timeout);
return new FIFOCache<>(capacity, timeout);
}
/**
@ -36,7 +36,7 @@ public class CacheUtil {
* @return {@link FIFOCache}
*/
public static <K, V> FIFOCache<K, V> newFIFOCache(int capacity){
return new FIFOCache<K, V>(capacity);
return new FIFOCache<>(capacity);
}
/**
@ -49,7 +49,7 @@ public class CacheUtil {
* @return {@link LFUCache}
*/
public static <K, V> LFUCache<K, V> newLFUCache(int capacity, long timeout){
return new LFUCache<K, V>(capacity, timeout);
return new LFUCache<>(capacity, timeout);
}
/**
@ -61,7 +61,7 @@ public class CacheUtil {
* @return {@link LFUCache}
*/
public static <K, V> LFUCache<K, V> newLFUCache(int capacity){
return new LFUCache<K, V>(capacity);
return new LFUCache<>(capacity);
}
@ -75,7 +75,7 @@ public class CacheUtil {
* @return {@link LRUCache}
*/
public static <K, V> LRUCache<K, V> newLRUCache(int capacity, long timeout){
return new LRUCache<K, V>(capacity, timeout);
return new LRUCache<>(capacity, timeout);
}
/**
@ -87,7 +87,7 @@ public class CacheUtil {
* @return {@link LRUCache}
*/
public static <K, V> LRUCache<K, V> newLRUCache(int capacity){
return new LRUCache<K, V>(capacity);
return new LRUCache<>(capacity);
}
/**
@ -99,7 +99,7 @@ public class CacheUtil {
* @return {@link TimedCache}
*/
public static <K, V> TimedCache<K, V> newTimedCache(long timeout){
return new TimedCache<K, V>(timeout);
return new TimedCache<>(timeout);
}
/**
@ -112,7 +112,7 @@ public class CacheUtil {
* @since 3.0.7
*/
public static <K, V> WeakCache<K, V> newWeakCache(long timeout){
return new WeakCache<K, V>(timeout);
return new WeakCache<>(timeout);
}
/**
@ -123,7 +123,7 @@ public class CacheUtil {
* @return {@link NoCache}
*/
public static <K, V> NoCache<K, V> newNoCache(){
return new NoCache<K, V>();
return new NoCache<>();
}
}

View File

@ -1,30 +1,34 @@
package cn.hutool.cache;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
/**
* 全局缓存清理定时器池用于在需要过期支持的缓存对象中超时任务池
*
* @author looly
*
* @author looly
*/
public enum GlobalPruneTimer {
/** 单例对象 */
/**
* 单例对象
*/
INSTANCE;
/** 缓存任务计数 */
/**
* 缓存任务计数
*/
private AtomicInteger cacheTaskNumber = new AtomicInteger(1);
/** 定时器 */
/**
* 定时器
*/
private ScheduledExecutorService pruneTimer;
/**
@ -36,8 +40,8 @@ public enum GlobalPruneTimer {
/**
* 启动定时任务
*
* @param task 任务
*
* @param task 任务
* @param delay 周期
* @return {@link ScheduledFuture}对象可手动取消此任务
*/
@ -52,12 +56,7 @@ public enum GlobalPruneTimer {
if (null != pruneTimer) {
shutdownNow();
}
this.pruneTimer = new ScheduledThreadPoolExecutor(16, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return ThreadUtil.newThread(r, StrUtil.format("Pure-Timer-{}", cacheTaskNumber.getAndIncrement()));
}
});
this.pruneTimer = new ScheduledThreadPoolExecutor(16, r -> ThreadUtil.newThread(r, StrUtil.format("Pure-Timer-{}", cacheTaskNumber.getAndIncrement())));
}
/**
@ -71,7 +70,7 @@ public enum GlobalPruneTimer {
/**
* 销毁全局定时器
*
*
* @return 销毁时未被执行的任务列表
*/
public List<Runnable> shutdownNow() {

View File

@ -71,7 +71,7 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
* @since 4.5.16
*/
private void putWithoutLock(K key, V object, long timeout) {
CacheObj<K, V> co = new CacheObj<K, V>(key, object, timeout);
CacheObj<K, V> co = new CacheObj<>(key, object, timeout);
if (timeout != 0) {
existCustomTimeout = true;
}
@ -190,10 +190,9 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
// ---------------------------------------------------------------- get end
@Override
@SuppressWarnings("unchecked")
public Iterator<V> iterator() {
CacheObjIterator<K, V> copiedIterator = (CacheObjIterator<K, V>) this.cacheObjIterator();
return new CacheValuesIterator<V>(copiedIterator);
return new CacheValuesIterator<>(copiedIterator);
}
@Override

View File

@ -42,7 +42,7 @@ public class FIFOCache<K, V> extends AbstractCache<K, V> {
this.capacity = capacity;
this.timeout = timeout;
cacheMap = new LinkedHashMap<K, CacheObj<K, V>>(capacity + 1, 1.0f, false);
cacheMap = new LinkedHashMap<>(capacity + 1, 1.0f, false);
}
/**