我使用 EhCache 来存储应用程序对象的有限大小的缓存,但在升级其版本后,我在使用 cache.get(...) 时遇到此异常:
java.lang.ClassCastException: net.sf.ehcache.store.chm.SelectableConcurrentHashMap$DummyPinnedKey cannot be cast to br.com.project.util.VttcElement
有人知道我们的缓存 key 中是否应该实现任何新接口(interface)吗?
这是我改编的代码:
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
private VttcElementCache() {
CacheManager cacheManager = CacheManager.create(METAINFFileLoader.getInputStream("ehcache.xml"));
cache = cacheManager.getCache("br.com.project.util.VttcElement");
}
private void load(int id, long idf) {
cache.put(new Element(new VttcElement(id, idf), "1"));
}
private String get(VttcElement vttcElement) {
Element element = cache.get(vttcElement); //The exception is thrown here!
(String) return elemente.getValue();
}
最佳答案
我找到了。
由于某种原因,我的关键类 VttcElement 没有很好地存储在缓存中,因为异常是在其 equals 方法内部抛出的,就在它生成的行 (VttcElement) obj.
我将所有键更改为字符串,而不是使用其属性与逗号连接VttcElement,并且它完美地用作键。
非常感谢。
关于java - EhCache 2.6.0 的 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12605860/