1

Guava キャッシュの問題に直面しています。キャッシュに要素が 1 つしかない場合は問題ありません。しかし、2番目の要素をロードすると、以前のエントリのキーで選択しようとしています

private static LoadingCache<String, MyClass> cache = null;
....
public MyClass method(final String id1, final long id2)  {
    log.error("inside with "+id1);
    final String cacheKey = id1+"-"+id2;
    if(cache == null){
        cache = CacheBuilder.newBuilder()
       .maximumSize(1000)
       .build(
            new CacheLoader<String, MyClass>() {
                @Override
                public MyClass load(String key) {
                    return getValue(cacheKey);
                }
           }
        );
    }
    try {
        return cache.get(cacheKey);
    } catch (ExecutionException ex) {
        log.error("EEE missing entry",ex);
    }
}

private MyClass getValue(String cacheKey){
    log.error("not from cache "+cacheKey);
    ...

}

ログには次のように記載されています。

inside with 129890038707408035563943963861595603358
not from cache 1663659699-315839912047403113610285801857400882820 // This is key for the earlier entry

たとえば、method("1", 2) を呼び出すと、値がキャッシュに読み込まれ、その後キャッシュから取得できます。メソッド ("3", 4) を呼び出しますが、これはキャッシュにないため、getValue() が呼び出され、ログにメソッド ("1", 2) のキーが出力されます。

どこが間違っていますか?

4

1 に答える 1