mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -83,14 +83,6 @@ public interface Cache<K, V> extends Iterable<V>, Serializable {
|
||||
*/
|
||||
V get(K key, boolean isUpdateLastAccess);
|
||||
|
||||
/**
|
||||
* 返回缓存迭代器
|
||||
*
|
||||
* @return 缓存迭代器
|
||||
*/
|
||||
@Override
|
||||
Iterator<V> iterator();
|
||||
|
||||
/**
|
||||
* 返回包含键和值得迭代器
|
||||
*
|
||||
|
@@ -45,20 +45,19 @@ public class LFUFileCache extends AbstractFileCache{
|
||||
|
||||
@Override
|
||||
protected Cache<File, byte[]> initCache() {
|
||||
Cache<File, byte[]> cache = new LFUCache<File, byte[]>(this.capacity, this.timeout) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
return new LFUCache<File, byte[]>(LFUFileCache.this.capacity, LFUFileCache.this.timeout) {
|
||||
private static final long serialVersionUID1 = 1L;
|
||||
|
||||
@Override
|
||||
public boolean isFull() {
|
||||
return LFUFileCache.this.usedSize > this.capacity;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onRemove(File key, byte[] cachedObject) {
|
||||
usedSize -= cachedObject.length;
|
||||
}
|
||||
};
|
||||
return cache;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -45,20 +45,19 @@ public class LRUFileCache extends AbstractFileCache{
|
||||
|
||||
@Override
|
||||
protected Cache<File, byte[]> initCache() {
|
||||
Cache<File, byte[]> cache = new LRUCache<File, byte[]>(this.capacity, super.timeout) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
return new LRUCache<File, byte[]>(LRUFileCache.this.capacity, super.timeout) {
|
||||
private static final long serialVersionUID1 = 1L;
|
||||
|
||||
@Override
|
||||
public boolean isFull() {
|
||||
return LRUFileCache.this.usedSize > this.capacity;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onRemove(File key, byte[] cachedObject) {
|
||||
usedSize -= cachedObject.length;
|
||||
}
|
||||
};
|
||||
return cache;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -20,7 +20,7 @@ public class CacheObj<K, V> implements Serializable{
|
||||
/** 访问次数 */
|
||||
protected long accessCount;
|
||||
/** 对象存活时长,0表示永久存活*/
|
||||
private long ttl;
|
||||
private final long ttl;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
|
@@ -23,7 +23,6 @@ public class CacheObjIterator<K, V> implements Iterator<CacheObj<K, V>>, Seriali
|
||||
* 构造
|
||||
*
|
||||
* @param iterator 原{@link Iterator}
|
||||
* @param readLock 读锁
|
||||
*/
|
||||
CacheObjIterator(Iterator<CacheObj<K, V>> iterator) {
|
||||
this.iterator = iterator;
|
||||
|
@@ -17,7 +17,6 @@ public class CacheValuesIterator<V> implements Iterator<V>, Serializable {
|
||||
/**
|
||||
* 构造
|
||||
* @param iterator 原{@link CacheObjIterator}
|
||||
* @param readLock 读锁
|
||||
*/
|
||||
CacheValuesIterator(CacheObjIterator<?, V> iterator) {
|
||||
this.cacheObjIter = iterator;
|
||||
|
@@ -1,14 +1,12 @@
|
||||
package cn.hutool.cache.test;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.cache.Cache;
|
||||
import cn.hutool.cache.CacheUtil;
|
||||
import cn.hutool.cache.impl.TimedCache;
|
||||
import cn.hutool.core.date.DateUnit;
|
||||
import cn.hutool.core.lang.func.Func0;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* 缓存测试用例
|
||||
@@ -27,7 +25,7 @@ public class CacheTest {
|
||||
|
||||
//由于缓存容量只有3,当加入第四个元素的时候,根据FIFO规则,最先放入的对象将被移除
|
||||
String value1 = fifoCache.get("key1");
|
||||
Assert.assertTrue(null == value1);
|
||||
Assert.assertNull(value1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -44,9 +42,9 @@ public class CacheTest {
|
||||
String value1 = lfuCache.get("key1");
|
||||
String value2 = lfuCache.get("key2");
|
||||
String value3 = lfuCache.get("key3");
|
||||
Assert.assertTrue(null != value1);
|
||||
Assert.assertTrue(null == value2);
|
||||
Assert.assertTrue(null == value3);
|
||||
Assert.assertNotNull(value1);
|
||||
Assert.assertNull(value2);
|
||||
Assert.assertNull(value3);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -84,21 +82,15 @@ public class CacheTest {
|
||||
|
||||
//5毫秒后由于value2设置了5毫秒过期,因此只有value2被保留下来
|
||||
String value1 = timedCache.get("key1");
|
||||
Assert.assertTrue(null == value1);
|
||||
Assert.assertNull(value1);
|
||||
String value2 = timedCache.get("key2");
|
||||
Assert.assertEquals("value2", value2);
|
||||
|
||||
//5毫秒后,由于设置了默认过期,key3只被保留4毫秒,因此为null
|
||||
String value3 = timedCache.get("key3");
|
||||
Assert.assertTrue(null == value3);
|
||||
Assert.assertNull(value3);
|
||||
|
||||
String value3Supplier = timedCache.get("key3", new Func0<String>() {
|
||||
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return "Default supplier";
|
||||
}
|
||||
});
|
||||
String value3Supplier = timedCache.get("key3", () -> "Default supplier");
|
||||
Assert.assertEquals("Default supplier", value3Supplier);
|
||||
|
||||
// 永不过期
|
||||
|
Reference in New Issue
Block a user