mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
[cleanup] erefactor/EclipseJdt - Remove trailing whitespace - All lines
EclipseJdt cleanup 'RemoveAllTrailingWhitespace' applied by erefactor. For EclipseJdt see https://www.eclipse.org/eclipse/news/4.18/jdt.php For erefactor see https://github.com/cal101/erefactor
This commit is contained in:
@@ -13,10 +13,10 @@ import cn.hutool.cache.impl.WeakCache;
|
||||
*@since 3.0.1
|
||||
*/
|
||||
public class CacheUtil {
|
||||
|
||||
|
||||
/**
|
||||
* 创建FIFO(first in first out) 先进先出缓存.
|
||||
*
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param capacity 容量
|
||||
@@ -26,10 +26,10 @@ public class CacheUtil {
|
||||
public static <K, V> FIFOCache<K, V> newFIFOCache(int capacity, long timeout){
|
||||
return new FIFOCache<>(capacity, timeout);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建FIFO(first in first out) 先进先出缓存.
|
||||
*
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param capacity 容量
|
||||
@@ -38,10 +38,10 @@ public class CacheUtil {
|
||||
public static <K, V> FIFOCache<K, V> newFIFOCache(int capacity){
|
||||
return new FIFOCache<>(capacity);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建LFU(least frequently used) 最少使用率缓存.
|
||||
*
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param capacity 容量
|
||||
@@ -51,10 +51,10 @@ public class CacheUtil {
|
||||
public static <K, V> LFUCache<K, V> newLFUCache(int capacity, long timeout){
|
||||
return new LFUCache<>(capacity, timeout);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建LFU(least frequently used) 最少使用率缓存.
|
||||
*
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param capacity 容量
|
||||
@@ -63,11 +63,11 @@ public class CacheUtil {
|
||||
public static <K, V> LFUCache<K, V> newLFUCache(int capacity){
|
||||
return new LFUCache<>(capacity);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 创建LRU (least recently used)最近最久未使用缓存.
|
||||
*
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param capacity 容量
|
||||
@@ -77,10 +77,10 @@ public class CacheUtil {
|
||||
public static <K, V> LRUCache<K, V> newLRUCache(int capacity, long timeout){
|
||||
return new LRUCache<>(capacity, timeout);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建LRU (least recently used)最近最久未使用缓存.
|
||||
*
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param capacity 容量
|
||||
@@ -89,10 +89,10 @@ public class CacheUtil {
|
||||
public static <K, V> LRUCache<K, V> newLRUCache(int capacity){
|
||||
return new LRUCache<>(capacity);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建定时缓存.
|
||||
*
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param timeout 过期时长,单位:毫秒
|
||||
@@ -101,10 +101,10 @@ public class CacheUtil {
|
||||
public static <K, V> TimedCache<K, V> newTimedCache(long timeout){
|
||||
return new TimedCache<>(timeout);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建弱引用缓存.
|
||||
*
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @param timeout 过期时长,单位:毫秒
|
||||
@@ -114,10 +114,10 @@ public class CacheUtil {
|
||||
public static <K, V> WeakCache<K, V> newWeakCache(long timeout){
|
||||
return new WeakCache<>(timeout);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建无缓存实现.
|
||||
*
|
||||
*
|
||||
* @param <K> Key类型
|
||||
* @param <V> Value类型
|
||||
* @return {@link NoCache}
|
||||
|
@@ -23,7 +23,7 @@ public abstract class AbstractFileCache implements Serializable{
|
||||
protected final long timeout;
|
||||
/** 缓存实现 */
|
||||
protected final Cache<File, byte[]> cache;
|
||||
|
||||
|
||||
/** 已使用缓存空间 */
|
||||
protected int usedSize;
|
||||
|
||||
@@ -122,7 +122,7 @@ public abstract class AbstractFileCache implements Serializable{
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------- protected method start
|
||||
/**
|
||||
* 初始化实现文件缓存的缓存对象
|
||||
|
@@ -12,7 +12,7 @@ import cn.hutool.cache.impl.LFUCache;
|
||||
*/
|
||||
public class LFUFileCache extends AbstractFileCache{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
/**
|
||||
* 构造<br>
|
||||
* 最大文件大小为缓存容量的一半<br>
|
||||
|
@@ -12,7 +12,7 @@ import cn.hutool.cache.impl.LRUCache;
|
||||
*/
|
||||
public class LRUFileCache extends AbstractFileCache{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
/**
|
||||
* 构造<br>
|
||||
* 最大文件大小为缓存容量的一半<br>
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* 提供针对文件的缓存实现
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
|
@@ -12,20 +12,20 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
*/
|
||||
public class CacheObj<K, V> implements Serializable{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
protected final K key;
|
||||
protected final V obj;
|
||||
|
||||
|
||||
/** 上次访问时间 */
|
||||
private volatile long lastAccess;
|
||||
/** 访问次数 */
|
||||
protected AtomicLong accessCount = new AtomicLong();
|
||||
/** 对象存活时长,0表示永久存活*/
|
||||
private final long ttl;
|
||||
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param key 键
|
||||
* @param obj 值
|
||||
* @param ttl 超时时长
|
||||
@@ -36,10 +36,10 @@ public class CacheObj<K, V> implements Serializable{
|
||||
this.ttl = ttl;
|
||||
this.lastAccess = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断是否过期
|
||||
*
|
||||
*
|
||||
* @return 是否过期
|
||||
*/
|
||||
boolean isExpired() {
|
||||
@@ -49,10 +49,10 @@ public class CacheObj<K, V> implements Serializable{
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取值
|
||||
*
|
||||
*
|
||||
* @param isUpdateLastAccess 是否更新最后访问时间
|
||||
* @return 获得对象
|
||||
* @since 4.0.10
|
||||
@@ -64,7 +64,7 @@ public class CacheObj<K, V> implements Serializable{
|
||||
accessCount.getAndIncrement();
|
||||
return this.obj;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取键
|
||||
* @return 键
|
||||
@@ -73,7 +73,7 @@ public class CacheObj<K, V> implements Serializable{
|
||||
public K getKey() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取值
|
||||
* @return 值
|
||||
@@ -82,7 +82,7 @@ public class CacheObj<K, V> implements Serializable{
|
||||
public V getValue() {
|
||||
return this.obj;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CacheObj [key=" + key + ", obj=" + obj + ", lastAccess=" + lastAccess + ", accessCount=" + accessCount + ", ttl=" + ttl + "]";
|
||||
|
@@ -6,7 +6,7 @@ import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* {@link cn.hutool.cache.impl.AbstractCache} 的CacheObj迭代器.
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
* @param <K> 键类型
|
||||
@@ -21,7 +21,7 @@ public class CacheObjIterator<K, V> implements Iterator<CacheObj<K, V>>, Seriali
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param iterator 原{@link Iterator}
|
||||
*/
|
||||
CacheObjIterator(Iterator<CacheObj<K, V>> iterator) {
|
||||
|
@@ -7,7 +7,7 @@ import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* 无缓存实现,用于快速关闭缓存
|
||||
*
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @author Looly,jodd
|
||||
@@ -49,7 +49,7 @@ public class NoCache<K, V> implements Cache<K, V> {
|
||||
public V get(K key, boolean isUpdateLastAccess) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V get(K key, Func0<V> supplier) {
|
||||
return get(key, true, supplier);
|
||||
@@ -78,7 +78,7 @@ public class NoCache<K, V> implements Cache<K, V> {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterator<CacheObj<K, V>> cacheObjIterator() {
|
||||
return null;
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* 提供各种缓存实现
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* 提供简易的缓存实现,此模块参考了jodd工具中的Cache模块
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
|
@@ -14,7 +14,7 @@ import org.junit.Test;
|
||||
*
|
||||
*/
|
||||
public class CacheTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void fifoCacheTest(){
|
||||
Cache<String,String> fifoCache = CacheUtil.newFIFOCache(3);
|
||||
@@ -28,22 +28,22 @@ public class CacheTest {
|
||||
fifoCache.put("key2", "value2", DateUnit.SECOND.getMillis() * 3);
|
||||
fifoCache.put("key3", "value3", DateUnit.SECOND.getMillis() * 3);
|
||||
fifoCache.put("key4", "value4", DateUnit.SECOND.getMillis() * 3);
|
||||
|
||||
|
||||
//由于缓存容量只有3,当加入第四个元素的时候,根据FIFO规则,最先放入的对象将被移除
|
||||
String value1 = fifoCache.get("key1");
|
||||
Assert.assertNull(value1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void lfuCacheTest(){
|
||||
Cache<String, String> lfuCache = CacheUtil.newLFUCache(3);
|
||||
lfuCache.put("key1", "value1", DateUnit.SECOND.getMillis() * 3);
|
||||
//使用次数+1
|
||||
lfuCache.get("key1");
|
||||
lfuCache.get("key1");
|
||||
lfuCache.put("key2", "value2", DateUnit.SECOND.getMillis() * 3);
|
||||
lfuCache.put("key3", "value3", DateUnit.SECOND.getMillis() * 3);
|
||||
lfuCache.put("key4", "value4", DateUnit.SECOND.getMillis() * 3);
|
||||
|
||||
|
||||
//由于缓存容量只有3,当加入第四个元素的时候,根据LFU规则,最少使用的将被移除(2,3被移除)
|
||||
String value1 = lfuCache.get("key1");
|
||||
String value2 = lfuCache.get("key2");
|
||||
@@ -52,7 +52,7 @@ public class CacheTest {
|
||||
Assert.assertNull(value2);
|
||||
Assert.assertNull(value3);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void lruCacheTest(){
|
||||
Cache<String, String> lruCache = CacheUtil.newLRUCache(3);
|
||||
@@ -71,7 +71,7 @@ public class CacheTest {
|
||||
String value2 = lruCache.get("key2");
|
||||
Assert.assertNull(value2);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void timedCacheTest(){
|
||||
TimedCache<String, String> timedCache = CacheUtil.newTimedCache(4);
|
||||
@@ -80,29 +80,29 @@ public class CacheTest {
|
||||
timedCache.put("key2", "value2", DateUnit.SECOND.getMillis() * 5);//5秒过期
|
||||
timedCache.put("key3", "value3");//默认过期(4毫秒)
|
||||
timedCache.put("key4", "value4", Long.MAX_VALUE);//永不过期
|
||||
|
||||
|
||||
//启动定时任务,每5毫秒秒检查一次过期
|
||||
timedCache.schedulePrune(5);
|
||||
//等待5毫秒
|
||||
ThreadUtil.sleep(5);
|
||||
|
||||
|
||||
//5毫秒后由于value2设置了5毫秒过期,因此只有value2被保留下来
|
||||
String value1 = timedCache.get("key1");
|
||||
Assert.assertNull(value1);
|
||||
String value2 = timedCache.get("key2");
|
||||
Assert.assertEquals("value2", value2);
|
||||
|
||||
|
||||
//5毫秒后,由于设置了默认过期,key3只被保留4毫秒,因此为null
|
||||
String value3 = timedCache.get("key3");
|
||||
Assert.assertNull(value3);
|
||||
|
||||
|
||||
String value3Supplier = timedCache.get("key3", () -> "Default supplier");
|
||||
Assert.assertEquals("Default supplier", value3Supplier);
|
||||
|
||||
|
||||
// 永不过期
|
||||
String value4 = timedCache.get("key4");
|
||||
Assert.assertEquals("value4", value4);
|
||||
|
||||
|
||||
//取消定时清理
|
||||
timedCache.cancelPruneSchedule();
|
||||
}
|
||||
|
Reference in New Issue
Block a user