我面临番石榴缓存的问题。当我在缓存中只有一个元素时,一切都很好。但是当我加载第二个元素时,它试图用较早进入的键来选择
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)的键
我哪里错了?