我对番石榴缓存有一个奇怪的(至少对我来说)行为。第一次命中后,以下访问将返回一个空对象。我没有使用奇怪的驱逐,所以我不知道我在哪里做错了。我声明了以下LoadingCache:
LoadingCache<String, Vector<Location>> locations = CacheBuilder.newBuilder()
.maximumSize(100000)
.build(
new CacheLoader<String,Vector<Location>>() {
@Override
public Vector<Location> load(String key) {
return _getLocationListByTranscriptId(key);
}
});
我只在这种方法中使用它:
public Vector<Location> getLocationListByTranscriptId (String transcriptid) {
if (transcriptid.equals("TCONS_00000046")) System.out.println("tcons found, will this work?");
Vector<Location> result;
try {
result = locations.get(transcriptid);
} catch (ExecutionException e) {
System.err.println("Error accessing cache, doing the hard way");
result = _getLocationListByTranscriptId(transcriptid);
}
if (transcriptid.equals("TCONS_00000046")){
if (result.size()==0){
System.out.println("this is a problem");
return null;
}
System.out.println("this is good!");
}
return result;
}
迭代输入字符串的集合,我得到以下输出:
tcons found, will this work?
this is good!
tcons found, will this work?
this is a problem
因此,我第一次使用缓存时,它可以工作,但是 A)该值未正确存储以供将来访问;B)该值被重置为一些奇怪的行为。我能做些什么?感谢大家阅读本文!
编辑:感谢 axtavt 的回答,我可以立即弄清楚我在哪里编辑结果列表。不知道为什么,我确信番石榴缓存会返回值的副本。感谢您的回答,以及有关防御性编程的建议。(对不起,如果我还不能评价你的答案)。