구아바 캐시에 문제가 있습니다. 캐시에 하나의 요소 만있는 경우 문제가 없습니다. 나는 두 번째 요소를로드 할 때, 그것의 로그는 말한다구버 키를 고려한 구아바 캐시
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
방법 ("1", 2), 캐시에 값을로드하고 이후에 캐시에서 값을 가져올 수 있습니다. 이제 메서드 ("3", 4)를 호출합니다. 캐시에 없으므로 getValue()가 호출되고 메서드 ("1", 2)의 키가 출력됩니다.
어디서 잘못 되었나요?
어떤 키? 재현하기위한 모든 단계를 제공하십시오. –
캐시를 초기화하는 방법은 스레드로부터 안전하지 않습니다. 또한 매우 정적 인 방법을 기반으로하는 CacheLoader를 사용하는 정적 캐시이기 때문에 오류가 발생하기 쉽습니다. 나는 당신의 실수가 그 중 하나 때문일 것이라고 생각한다. –
이 규칙 (http://stackoverflow.com/help/mcve)을 읽어주십시오. –