This commit is contained in:
Looly
2019-10-29 19:05:23 +08:00
parent 3cef7fa9a7
commit bf03aebcef
107 changed files with 283 additions and 456 deletions

View File

@@ -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);
// 永不过期