4

我对番石榴缓存有一个奇怪的(至少对我来说)行为。第一次命中后,以下访问将返回一个空对象。我没有使用奇怪的驱逐,所以我不知道我在哪里做错了。我声明了以下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 的回答,我可以立即弄清楚我在哪里编辑结果列表。不知道为什么,我确信番石榴缓存会返回值的副本。感谢您的回答,以及有关防御性编程的建议。(对不起,如果我还不能评价你的答案)。

4

1 回答 1

7

我相信你不小心清除了Vector代码中的某个地方。有两种可能:

  • Vector由从缓存中获取它的代码修改。

    可以通过制作防御性副本(尽管它破坏了缓存的想法)或返回不可变的集合视图来防止这种错误:

    LoadingCache<String, List<Location>> locations = CacheBuilder.newBuilder()
         .maximumSize(100000)
         .build(
                 new CacheLoader<String, List<Location>>() {
                     @Override
                     public List<Location> load(String key)  {
                         return Collections.unmodifiableList(
                             _getLocationListByTranscriptId(key));
                     }
                 }); 
    

    这样修改代码后,很容易发现非法修改集合的地方。

    请注意,没有不可修改的视图Vector,因此List应改为使用。

  • _getLocationListByTranscriptId()将其结果存储在一个字段中,可以通过其他方法(或同一方法的其他调用)访问它。因此,您应该检查_getLocationListByTranscriptId()是否不会在字段中留下对其结果的任何引用。

于 2012-04-24T13:37:55.210 回答