測試的時候發現項目中的LoadingCache沒有刷新,但是明明調用了refresh方法了。後來發現LoadingCache是不支持緩存null值的,如果load回調方法返回null,則在get的時候會拋出異常。
通過幾個例子開看這個問題:
public void test_loadNull() {
LoadingCache<String, String> stringCache = CacheBuilder.newBuilder()
.maximumSize(10)
.build(new CacheLoader<String, String>() {
@Override
public String load(String s) throws Exception {
System.out.println("xx");
if (s.equals("hello"))
return "world";
else
return null;
}
});
try {
System.out.println(stringCache.get("hello"));
// get觸發load,load返回null則拋出異常:
// com.google.common.cache.CacheLoader$InvalidCacheLoadException: CacheLoader returned null for key other_key.
System.out.println(stringCache.get("other_key"));
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public void test_loadNullWhenRefresh() {
LoadingCache<String, String> stringCache = CacheBuilder.newBuilder()
.maximumSize(10)
.build(new CacheLoader<String, String>() {
int i = 0;
@Override
public String load(String s) throws Exception {
if (i == 0) {
i++;
return "world";
}
return null;
}
});
try {
System.out.println(stringCache.get("hello"));
System.out.println(stringCache.get("hello"));
// refresh的時候,如果load函數返回null,則refresh拋出異常:
// Exception thrown during refresh
// com.google.common.cache.CacheLoader$InvalidCacheLoadException: CacheLoader returned null for key hello.
stringCache.refresh("hello");
System.out.println(stringCache.get("hello"));
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public void test_loadNullAfterInvalidate() {
LoadingCache<String, String> stringCache = CacheBuilder.newBuilder()
.maximumSize(10)
.build(new CacheLoader<String, String>() {
int i = 0;
@Override
public String load(String s) throws Exception {
if (i == 0) {
i++;
return "world";
}
return null;
}
});
try {
System.out.println(stringCache.get("hello"));
System.out.println(stringCache.get("hello"));
// invalidate不會觸發load
stringCache.invalidate("hello");
// invalidate後,再次get,觸發load,拋出異常:
// com.google.common.cache.CacheLoader$InvalidCacheLoadException: CacheLoader returned null for key hello.
System.out.println(stringCache.get("hello"));
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public void test_loadThrowException() {
LoadingCache<String, String> stringCache = CacheBuilder.newBuilder()
.maximumSize(10)
.build(new CacheLoader<String, String>() {
@Override
public String load(String s) throws Exception {
if (s.equals("hello"))
return "world";
else
throw new IllegalArgumentException("only_hello");
}
});
try {
System.out.println(stringCache.get("hello"));
// get觸發load,load拋出異常,get也會拋出封裝後的異常:
// com.google.common.util.concurrent.UncheckedExecutionException: java.lang.IllegalArgumentException: only_hello
System.out.println(stringCache.get("other_key"));
} catch (ExecutionException e) {
e.printStackTrace();
}
}
所以如果你需要緩存“空”值,推薦的做法是使用Optional對象來封裝結果:
public void test_loadUseOptional() {
LoadingCache<String, Optional<String>> stringCache = CacheBuilder.newBuilder()
.maximumSize(10)
.build(new CacheLoader<String, Optional<String>>() {
@Override
public Optional<String> load(String s) throws Exception {
if (s.equals("hello"))
return Optional.of("world");
else
return Optional.absent();
}
});
try {
Optional<String> hello = stringCache.get("hello");
if(hello.isPresent()) {
System.out.println(hello.get());
}
Optional<String> otherKey = stringCache.get("other_key");
if(otherKey.isPresent()){
System.out.println(otherKey.get());
}
} catch (ExecutionException e) {
e.printStackTrace();
}
}
如果你的場景中認爲null是不存在的,那麼你可以在load函數中拋出異常,這個異常會通過get拋出。
另外還有一個問題,如果是key==null呢?答案是直接拋出java.lang.NullPointerException
。Guava對於null是很不待見的。
參考資料
- [Google Guava] 3-緩存 | 併發編程網 – ifeve.com
http://ifeve.com/google-guava-cachesexplained/ - Guava Cache使用筆記 - 代碼說-Let code talk - ITeye博客
http://bylijinnan.iteye.com/blog/2225074 - guava - How to avoid caching when values are null? - Stack Overflow
https://stackoverflow.com/questions/13379071/how-to-avoid-caching-when-values-are-null
本文獨立博客地址:Guava LoadingCache不能緩存null值 | 木杉的博客