小编Car*_*oti的帖子

番石榴缓存在第二次击中时返回空结果

我对番石榴缓存有一种奇怪的行为(至少对我而言).第一次点击后,以下访问将返回一个空对象.我没有使用奇怪的证据,所以我无法弄清楚我做错了什么.我声明了以下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);
                        }
                    });
Run Code Online (Sandbox Code Playgroud)

我只在这种方法中使用它:

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;
}
Run Code Online (Sandbox Code Playgroud)

迭代输入字符串的集合,我得到以下输出:

tcons found, will …
Run Code Online (Sandbox Code Playgroud)

java caching guava

3
推荐指数
1
解决办法
898
查看次数

标签 统计

caching ×1

guava ×1

java ×1